An introduction to Python for C programmers

Introduction

Python is a convenient tool that allows you to easily perform calculations like a calculator, and to quickly program and execute small processes. There is also Python that works inside a real calculator. (Reference: CASIO Color Graph Scientific Calculator fx-CG50) A process that takes dozens of lines to write in C can be written in a few lines in Python. Python is an object-oriented language, but since it is basically a function definition, it is easy for C programmers to use. Let's use both C and Python swords so that simple processing can be done with Python.

Background

I came across Python when uploading data collected from IoT sensors etc. to the cloud in M2M business, it is necessary to perform communication volume reduction data processing and upload processing, and I used it in M2M communication [Digi International] The company's ** cellular router ](https://www.digi.com/products/cellular-solutions/cellular-routers) has [ built-in Python interpreter **](https: // www) It was when I knew and used .digi.com/resources/documentation/digidocs/90001537/default.htm). It was ** built-in Python ** that couldn't define a class. Before M2M work, I used to write programs in C and Java, but after learning about Python, I fell in love with its ease of use, and now my hobby is playing with Python. However, there were many things that I had to think differently from the C language programming language (C / C ++ / Java / JavaScript), so I will write an introductory article on Python for those who know the C language programming language. I decided to.

Official site, official document

Official Python site: https://www.python.org/ Japanese documentation: https://docs.python.org/ja/3/ ・ Language specifications: https://docs.python.org/ja/3/reference/index.html ・ Tutorial: https://docs.python.org/ja/3/tutorial/index.html ・ FAQ: https://docs.python.org/ja/3/faq/index.html

Digression

Currently, I am engaged in basic research on cyber security apart from M2M work. I was happy to hear the following in the essay "** Become a hacker **" that I met in cyber security.

If you don't know any computer language, we recommend starting with Python. The design is clean, the documentation is solid, and it's fairly easy for beginners to get along with. But even if it's the best introductory language, it's not a toy. It's powerful, flexible, and well-suited for large projects.

Note that "** hacker **" does not mean bad.

There is a community or shared culture of experienced programmers and network geniuses. Its history can be traced back to early time-sharing minicomputers and early ARPAnet experiments. People in this culture coined the term "hacker hacker". Hackers have built the internet. Hackers have made the UNIX operating system as it is today. Hackers run Usenet to enable the World Wide Web.

Preparation

First, let's prepare a Python execution environment that enables you to use Python. The Python execution environment includes a Python interpreter and libraries that execute Python scripts.

Python version

There are two main versions of Python, 2 series (Python2.x) and 3 series (Python3.x). It has been announced that support for Python 2 will end in 2020. Language specifications have been changed and functions have been added in the Python3 series. If you want to learn from now on, use Python3. The newer version of Python3 has some useful features, so use the newest version as much as possible.

Python runtime environment type

There are multiple implementations of the Python runtime environment (mainly the Python interpreter). The standard is Python provided by python.org. It is called CPython because it is a Python interpreter written in C language. Others are Jython implemented in Java and PyPy that compiles Python scripts into CPU instructions and then executes them. There are also environments such as Jupyter notebook and anaconda that allow you to easily install various libraries and keep track of execution as a notebook. This introduction uses standard CPython for explanation.

If you are using a Windows OS, you need to install Python. Please download and install from https://www.python.org/downloads/. Many people have written about the installation procedure, so please refer to that article.

If you are using Linux OS or Mac OS, Python is installed by default. However, it may be version 2. Alternatively, you may have both version 2 and version 3 installed. Please execute the command as follows to check the version.

$ python --version
Python 2.7.15rc1
$ python2 --version
Python 2.7.15rc1
$ python3 --version
Python 3.6.5

Python virtual environment

If you use Python for business, you will probably maintain the environment of the Python version and library version at the time you provided it to the customer. It's a good idea to introduce a Python virtual environment so that you can easily switch to a Python environment that suits you. There are implementations of Python virtual environments such as venv, pyenv, pipenv, so please install and use the virtual environment according to the range that needs to be switched. See other people's articles for installation instructions.

Let's use it as a calculator

Let's get started with Python briefly.

Launch the Python interpreter

First, launch the interactive Python interpreter. If you are using Windows, start the command line version of Python3.x in Python3.x from the Start menu. (You can also choose IDLE, but it's a GUI version of the Python interpreter.) For Linux or MacOS, execute the python3 command or python command with no arguments.

Python 3 for Windows(32bit)


Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

Python3 for Linux(64bit)


$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

>>> is an input prompt to prompt for input. If you need multi-line input, the input prompt changes to ....

Arithmetic calculation

Let's start with a simple math calculation.

>>> 1 + 2 * 3
7
>>> (1 + 2) * 3
9
>>> 9 * 2
18
>>> 9 / 2
4.5

Unlike C language, the calculation result of / is a floating point number. Use // to find the quotient of an integer, similar to / in C. The quotient of a floating point number is a floating point number. There is also a % that asks for the remainder.

>>> 9 // 2
4
>>> 9.0 // 2
4.0
>>> 9 % 2
1
>>> 9.0 % 2
1.0

You can perform power calculations that should not be available in C language. You can also calculate the route by reciprocal.

>>> 9 ** 2
81
>>> 81 ** (1/2)
9.0
>>> 81 ** 0.5
9.0
>>> 9 ** 3
729
>>> 729 ** (1/3)
8.999999999999998

Floating point numbers no longer revert to their original 9 due to errors.

Assign a value to a variable

The calculation result can be stored in a variable.

>>> x = 1 + 2 * 3
>>> x
7
>>> y = 3 * x + 5
>>> y
26

You do not need to specify the type of the variable. Rather than assigning it to a variable, it's better to think of it as naming and holding the value. Variables will be explained later.

Scientific calculator

Let's define an arithmetic function.

>>> def f(x):
...     y = 3 * x + 5
...     return y
...
>>> f(0)
5
>>> f(1)
8
>>> f(2)
11

The function is defined by the def statement. Writing the function name and argument is the same as in C language, but it differs from C language in that it does not write the argument type and return value type. Python knows the type of the value, so you don't have to specify the type of the argument or variable. The argument x may be a number or a string. This function is defined by believing that the caller will pass a number.

In C language, the function processing block is surrounded by {}, but in Python, the range where the indent is lowered is the processing block. If the indents are not aligned, an error will occur. It doesn't matter if the if statement or for statement further lowers the indentation. In addition to the def statement, write a block with indentation after ending the statement with : such as if statement, for statement, while statement.

If you want to play more

The official documentation also has a tutorial to use as a calculator. It also describes data other than integers, so please refer to that as well. https://docs.python.org/ja/3/tutorial/introduction.html

Exiting the Python interpreter

You can exit the Python interpreter by typing Ctrl + Z on Windows or Ctrl + D on Linux / MacOS. Alternatively, execute the function ʻexit ()orquit () to exit. Executing ʻexit (0); in C is the same as terminating the command.

>>> exit()
$ 

Difference between C language and Python

Compiler and interpreter

For C language, write the C language source code, convert it to machine language using the C language compiler and assembler, create an executable file using the linker, and when the executable file is started, the CPU executes the machine language. The execution result is obtained. For Python, write a Python script, specify the script file name, start the Python interpreter, and the Python interpreter will execute the Python script. Python seems to run without compiling, but it actually compiles the script into virtual machine language (intermediate code) when the Python interpreter starts and then runs it. The Python interpreter becomes a virtual machine (VM) and executes virtual machine language.

image.png

Data type

Model name C language Python
int 16/32-bit fixed length data
upper limit/There is a lower limit
Variable length data
upper limit/No lower limit
long
long long
32/64-bit fixed length data
upper limit/There is a lower limit
Unified with int
short 16-bit fixed length data Unified with int
char 8-bit fixed length data
Enclose characters in single quotes
Substitute with a single character string
bytes None 8-bit fixed length data array
A string needs to be converted
str (string) array of char
'\0'End with
Surround with double quotes
Array of 16-bit unicode characters
シングルまたはSurround with double quotes(違いなし)
Special character escape is the same as C language
\x00Specify hexadecimal ASCII code with
\u0000Specify hexadecimal unicode with
row string None String with special character escaping disabled
You can write backticks such as Windows file paths as one
Example: r"C:\User\someone"
bool (true / false value) 0 is false
True except 0
False is false, True is true
(Actually, it is a subclass of int type, False can be calculated as 0, and True entity can be calculated as 1.)
enum Enumerated data Substitute with class
Make it a subclass of the Enum class
struct User-defined data type Substitute with class
class None Struct that can also write functions
Array/list Sequence of data of the same type
int data[] = {1, 2, 3};
Sequence of data
You can arrange different types of data
Arrayの中にArrayも入れられる
data = [1, 'abc', [3.14, True]]
tuple None List that cannot be changed
(The contents of the list in the tuple can be changed)
data = (1, 'abc', 3.14)
dict None Data that is a set of key and value
You can use strings, numbers, and tuples as keys
data = {1: 'one', 'pi': 3.14}
set None A collection of non-overlapping data
Can perform set operations
data = {1, 'abc', 3.14}

Data / value

In C language, the integer value 1 written in the source code is compiled and embedded in the operand of the machine language, or stored in an int type variable (memory area). In Python, the integer value 1 written in the script is interpreted as an int type value by the interpreter, the memory area for the int type is allocated (malloc), and the int type object with the value 1 is created. Will be generated. The value of an int type object cannot be rewritten, and every time various values appear during program execution, a memory area is secured and a value object is generated, and objects that are no longer used are automatically returned to memory. I will.

In CPython, objects are defined by structures.

Python object header structure


typedef struct _object{
        Py_ssize_t ob_refcnt;            //Reference counting(8 bytes), Memory is released when it reaches 0
        struct _typeobject* ob_type;     //Pointer to type information(8 bytes)
}PyObject;

Int type object is followed by data size (8 bytes) and value data (arbitrary bytes) in addition to the object header. The data area is a variable length array and can handle large integer values. If the value is 0, the above header is 16 bytes and the data size area of size 0 is 24 bytes in total. A value of 1 will increase the size a bit because it requires a data area. In Python you can look up the address and size of an object.

>>> type(0)            #The type of the value can be checked with the type function
<class'int'>           #I know that a value object of 0 is of type int
>>> id(0)              #The address of the object can be found with the id command
10910368               #Decimal number display
>>> hex(id(0))         #Hexadecimal display with hex function
'0xa67aa0'
>>> import sys         #Use sys library
>>> sys.getsizeof(0)   # sys.You can check the size of an object with the getsizeof function
24                     #The int type object 0 uses 24 bytes
>>> sys.getsizeof(1)   #Int type object called 1 uses 28 bytes
28

Note that int values in the range of -5 to 256, which are often used, have objects prepared in advance and are implemented so that they can be reused without returning memory.

variable

Variables in C language can be likened to a container, and it feels like "putting a value in a variable". All Python variables are pointer variables. Variables only point to values. The feeling of "name the value". Python variables are registered in the variable dictionary, and variable names are just strings used as dictionary keys to retrieve values. Variable dictionaries are roughly divided into global variable dictionaries (globals ()) and local variable dictionaries (locals ()). You can also change variables by directly manipulating the global variable dictionary.

>>> a = 123
>>> a
123
>>> globals()
{'a': 123, '__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class'_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>}
>>> globals()['a']
123
>>> globals()['a'] = 'abc'
{'a': 'abc', '__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class'_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>}
>>> a
'abc'

image.png

It's just a pointer array that you can put various data in a list or tuple, and the object at the point destination is int type or str type.

Formatted output

In C language, the printf function takes a format string as an argument and displays it on the console. In Python, you create a formatted string using a format string and print it to the console with the print function. There are several ways to format it.

>>> print("%08x: %02x %02x %02x" % (0x138, 9, 12, 0xa))
00000138: 09 0c 0a
>>> print("{:08x}: {:02x} {:02x} {:02x}".format(0x138, 9, 12, 0xa))
00000138: 09 0c 0a
>>> addr = 0x138
>>> data = [9, 12, 0xa]
>>> print(f"{addr:08x}: {data[0]:02x} {data[1]:02x} {data[2]:02x}")
00000138: 09 0c 0a
>>> print(f"{addr:08x}:", *[f"{item:02x}" for item in data])
00000138: 09 0c 0a

Local variables

In C language, if you declare a variable in a function, a local variable is created, and if you do not declare a variable, a global variable is used. In Python, if you assign a variable without declaring global in a function, a local variable is created, and if you declare global and assign a variable, it is assigned to a global variable. However, when referencing a variable (when using it for display etc.), if it is not in a local variable, it searches for a global variable.

456 is displayed in C language


#include <stdio.h>

int data = 123;

void func1() {
    data = 456;  //Assign to global variable if variable is not declared
}

void func2() {
    int data;    //When you declare a variable
    data = 789;  //Assign to a local variable
}

int main(void) {
    func1();
    printf("%d\n", data);
    func2();
    printf("%d\n", data);
}

Execution result


456
456

789 is displayed in Python


data = 123

def func1():
    data = 456    #Assigning to a local variable when assigning without declaring a variable

def func2():
    global data   #When you declare global
    data = 789    #Assign to global variable

func1()
print(data)
func2()
print(data)

Execution result


123
789

Sentence

In C language, the expression immediately after if, for, while, etc. must be enclosed in parentheses. In Python, you don't have to enclose it in parentheses.

C language


if (data == 123) {
    printf("OK\n");
}

Python


if data == 123:
    print("OK")
Sentence C language Python
if if-else
You can write an if statement continuously in the processing of else
If true{ }Enclose in and lower the indent
if-elif-else
elif makes it clear that they have the same indentation level
for Turn the index number Turn elements such as arrays
do-while First unconditionally run once None
switch-case Divided case None
辞書を使ってDivided caseできる
del None Variables can be deleted
Library import #include import

function

In C language, writing only the function name is the address of the function, and adding () is the function call. In Python, writing only a function name is a function object, and adding () is a function call. In Python, a function definition is compiled into a function object, which contains the machine language instructions for the virtual machine. You can disassemble virtual machine instruction in a function object. The Python interpreter becomes a virtual machine and executes virtual machine language instructions.

>>> def hello(name):
...     print("Hello,", name)
...
>>> hello
<function hello at 0x6fffff21e18>
>>> hello("World!")
Hello, World!
>>> import dis
>>> dis.dis(hello)
  2           0 LOAD_GLOBAL              0 (print)
              2 LOAD_CONST               1 ('Hello,')
              4 LOAD_FAST                0 (name)
              6 CALL_FUNCTION            2
              8 POP_TOP
             10 LOAD_CONST               0 (None)
             12 RETURN_VALUE

In Python, you can also define a function inside a function.

>>> def area(x, y):
...     def triangle():
...         return x * y / 2
...     def rectangle():
...         return x * y
...     print("The area of the triangle is", triangle(), "The area of the rectangle is", rectangle())
...
>>> area(3, 4)
The area of the triangle is 6.0 The area of the rectangle is 12

User-defined data

In C language, a set of data is defined as a structure. In Python, you can use a class to define a chunk of data.

C language


#include <stdio.h>

struct PhoneData {
    char *name;
    char *number;
};

struct PhoneData phone_book[] = {
    { "Hanako Yamada", "03-1234-56789" },
    { "Taro Suzuki", "045-9876-54321" },
};

int main() {
    for (int i = 0; i < sizeof(phone_book) / sizeof(phone_book[0]); i++) {
        printf("%s %s\n", phone_book[i].name, phone_book[i].number);
    }
}

Python



class PhoneData:
    def __init__(self, name, number):
        self.name = name
        self.number = number

phone_book = [
    PhoneData("Hanako Yamada", "03-1234-56789"),
    PhoneData("Taro Suzuki", "045-9876-54321"),
]

if __name__ == '__main__':
    for data in phone_book:
        print(data.name, data.number)

The content is not organized, so I will add and correct it little by little. As the second step, I would like to write in-depth content for those who have a deep understanding of C language. If you have any questions, please comment.

Sample program

You can compare how to write a functional program in C and an object-oriented program in Python.

I make full use of the Python-like writing style.

Recommended Posts

An introduction to Python for C programmers
An introduction to Python for non-engineers
[Introduction to python] A high-speed introduction to Python for busy C ++ programmers
An introduction to Python for machine learning
An introduction to Python Programming
Introduction to Python For, While
Introduction to Protobuf-c (C language ⇔ Python)
[Python Tutorial] An Easy Introduction to Python
An introduction to OpenCV for machine learning
Introduction to Python language
Introduction to OpenCV (python)-(2)
[What is an algorithm? Introduction to Search Algorithm] ~ Python ~
An introduction to object-oriented programming for beginners by beginners
An introduction to statistical modeling for data analysis
[Introduction to Udemy Python3 + Application] 43. for else statement
Introduction to Python "Re" 1 Building an execution environment
An introduction to voice analysis for music apps
Understand Python for Pepper development. -Introduction to Python Box-
An introduction to self-made Python web applications for a sluggish third-year web engineer
Introduction to Python Django (2) Win
An introduction to machine learning
An introduction to Python distributed parallel processing with Ray
Reading Note: An Introduction to Data Analysis with Python
Python started by C programmers
[Introduction to Python] <list> [edit: 2020/02/22]
Introduction to Python (Python version APG4b)
An introduction to Bayesian optimization
~ Tips for beginners to Python ③ ~
[Introduction to Python] How to write repetitive statements using for statements
[Introduction to Udemy Python3 + Application] 42. for statement, break statement, and continue statement
How to make a Python package (written for an intern)
An introduction to Python that even monkeys can understand (Part 3)
Introduction to Python for VBA users-Calling Python from Excel with xlwings-
[Python] Introduction to graph creation using coronavirus data [For beginners]
An introduction to Python that even monkeys can understand (Part 1)
An introduction to Python that even monkeys can understand (Part 2)
Use a scripting language for a comfortable C ++ life-OpenCV-Port Python to C ++-
Prolog Object Orientation for Python Programmers
[Introduction to Udemy Python 3 + Application] 58. Lambda
[Introduction to Udemy Python 3 + Application] 31. Comments
Introduction to Python Numerical Library NumPy
Practice! !! Introduction to Python (Type Hints)
[Introduction to Python] <numpy ndarray> [edit: 2020/02/22]
[Introduction to Udemy Python 3 + Application] 57. Decorator
Introduction to Python Hands On Part 1
[Introduction to Python3 Day 13] Chapter 7 Strings (7.1-7.1.1.1)
[Introduction to Python] How to parse JSON
[Introduction to Python3 Day 14] Chapter 7 Strings (7.1.1.1 to 7.1.1.4)
Python cheat sheet (for C ++ experienced)
[Introduction to Udemy Python3 + Application] 59. Generator
[Introduction to Python3 Day 15] Chapter 7 Strings (7.1.2-7.1.2.2)
[Introduction to Python] Let's use pandas
How to wrap C in Python
[Introduction to Python] Let's use pandas
[Introduction to Udemy Python 3 + Application] Summary
[Introduction to Python] Let's use pandas
Introduction to Python Django (2) Mac Edition
An alternative to `pause` in Python
[AWS SAM] Introduction to Python version
[Introduction to Python3 Day 21] Chapter 10 System (10.1 to 10.5)
An introduction to statistical modeling for data analysis (Midorimoto) reading notes (in Python and Stan)