Home > Article > Backend Development > Automated testing: several common programming patterns in Python
This chapter updates the content related to "Python syntax specifications and data types", mainly to let everyone understand what types of programming modes Python has, master the basic syntax of Python, and understand How to output and the basic application of command line parameters. After understanding the data types of Python, you can do more related operations.
①Python interactive command programming.
②Python script programming.
③Chinese encoding processing.
The interactive command programming mode is a typical line-by-line reading execution mode.
This programming mode is a typical application when the program has only one line or less.
The following figure uses the PythonIDLE editor for programming, and the programming mode of this editor is a typical interactive command encoding symbol.
>>> is the prompt for entering interactive commands. Each time you press Enter after completing the input, the command will be executed by the Python parser.
When we need to write more complex or large sections of code, imperative programming is not convenient enough.
Therefore, Python provides a script programming mode. You can create a script file with the suffix *.py and write a large amount of code into the file, which facilitates the maintenance and update of the code. You can then use interactive commands or IDE tools to run it.
String is a data type. However, strings have a special encoding problem.
Because computers can only process numbers, if you want to process text, you must first convert the text into numbers before processing.
The earliest computers used 8 bits as a byte during design. Therefore, the largest integer that can be represented by a byte That is 255 (binary 11111111 = decimal 255). If you want to represent a larger integer, you must use more bytes. For example, the maximum integer that can be represented by two bytes is 65535, and the maximum integer that can be represented by 4 bytes is 4294967295.
Since the computer was invented by Americans, only 127 characters were encoded into the computer at first, that is, uppercase and lowercase English letters, numbers and some symbols. This encoding table is called ASCII encoding, such as uppercase letters The code for the letter A is 65, and the code for the lowercase letter z is 122.
The reason why Python3 can solve the Chinese garbled problem well is that it uses unicode for all strings Character Encoding.
● Unicode unifies all languages into one set of codes so that there will be no garbled characters.
● Unicode is also constantly developing, but the most commonly used one is to use two bytes to represent a character (if you encounter a very rare character, you need 4 bytes). Most operating systems and most programming languages we see now support unicode.
ASCII encoding is 1 byte, while Unicode encoding is usually 2 bytes.
New problems arise again: If unified into Unicode encoding, the garbled code problem will disappear. However, if the text you write is basically all in English, Unicode encoding requires twice as much storage space as ASCII encoding, which is very uneconomical in terms of storage and transmission.
The birth of the solution: The UTF-8 encoding that converts Unicode encoding into "variable length encoding" appeared again.
● UTF-8 encoding encodes a Unicode character into 1-6 bytes according to different number sizes. Commonly used English letters are encoded into 1 byte, and Chinese characters are usually 3 bytes. Only Very rare characters will be encoded into 4-6 bytes.
● If the text you want to transmit contains a large number of English characters, using UTF-8 encoding can save space.
● UTF-8 encoding has an additional benefit, that is, ASCII encoding can actually be regarded as part of UTF-8 encoding. Therefore, a large number of historical legacy software that only supports ASCII encoding can be encoded in UTF-8 Keep working while coding.
Special note: Unicode encoding is used uniformly in computer memory.
In the Python3 version, strings are encoded in Unicode, which means that Python strings support multiple languages.
For the encoding of a single character, Python provides the ord() function to obtain the decimal integer representation of a single character, and the chr() function to convert the encoding into the corresponding character.
>>> ord(‘A’) 65 >>> ord(‘中’) 20013 >>> chr(66) ‘B’ >>> chr(25991) ‘文’
Python source code is also a text file, so when your source code contains Chinese, you need to specify the UTF-8 encoding when saving the source code. When the Python interpreter reads source code, in order for it to be read in UTF-8 encoding, we usually write this line at the beginning of the file.
#-*- coding:utf-8 *-
The comment is to tell the Python interpreter to read the source code according to UTF-8 encoding. Otherwise, the Chinese output you write in the source code may be garbled.
The above is the detailed content of Automated testing: several common programming patterns in Python. For more information, please follow other related articles on the PHP Chinese website!