1, numbers and expressions
What is an expression, 1+2*3 is an expression, where the plus and multiplication sign are called operators, 1, 2, 3 is called the operand. The result of the 1+2*3 calculation is 7, 1+2*3 = 7. We can save the result of the calculation in a variable, ret = 1-2*3. So an expression is a code or statement consisting of operands and operators that can be evaluated and placed on the right side of "=" To assign a value to a variable.
Calculation operation symbol: + 、-、 *,/,%, * *
Assignment operator: =, + = = *=/=%=//= **=
Comparison operators:>, <, >=, <=, = =,! =
Logical operators: not, and, or
Logical operators are used to make logical calculations. Like the comparison operators we used above, each time the comparison is actually a conditional judgment, it will get a value of TRUE or false accordingly. The operand of a logical operator is an expression or variable that is used to make conditional judgments.
Member operator: not in, in (determines if there is a letter in a word)
The member operator is used to determine whether an element is a member of another element. For example, we can tell if there is "H" in "Hello" and the result is true or False.
Identity operator: is, is not
Here we first assign 123456 to a, and then a to B, so that is actually a and B values are 123456, but the value of D is also 123456, why the first time A is B result is true, D and a result is false?
The reason is this: we know that the program is running in memory, the first time we assign 123456 to a, in fact, in memory opened a space, put 123456 in this space, in order to find the 123456 here, there will be a point to this space address, this address is called memory address , is 123456 of the addresses stored in memory. A actually points to the address that stores 123456 of the memory space. The b=a is executed so that B points to the same address as a. Then we execute d = 123456, and here we will open up a memory space and assign the memory address that points to the space to D, so that A and B point to the same 123456, and D to the other 123456.
Bitwise operators:
first understand a concept:
The numbers we usually use are represented in binary notation in the computer, and this binary number is called the number of machines. The number of machines is signed, in the computer with a number of the highest bit of the symbol, positive 0, negative number is 1.
For example: decimal number +7, the computer word length is 8 bits, converted to binary is 00000111. If it is-7, it is 10000111. So, here's 00000111 and 10000111 is the number of machines.
Original Code is the absolute value of the symbolic bit plus the truth , that is, the symbol is represented by the first bit, and the remaining bits represent values. For example, if it is a 8-bit binary:
[+1] original = 0000 0001
[-1] Original = 1000 0001
the first bit is the sign bit . Because the first bit is the sign bit, the range of values for the 8-bit binary number is:
11111111 to 01111111 i.e. 127 to 127
Anti-code is represented by:
The inverse of a positive number is its own
negative number of the inverse code is on the basis of its original code , the sign bit is unchanged, the remaining bits are reversed.
[+1] = [00000001] Original = [00000001] anti-
[-1] = [10000001] Original = [11111110] reverse
complement is represented by:
A positive complement is its own
The complement of a negative number is on the basis of its original code , the sign bit is unchanged, the rest of you take the counter, the last +1. (That is, on the basis of the anti-code +1)
[+1] = [00000001] Original = [00000001] anti = [00000001] complement
[-1] = [10000001] Original = [11111110] counter = [11111111] complement
we set a=234 (binary to 11101010), b=44 (binary 101100)
& Bitwise AND Operator: Two values that participate in the operation, if two corresponding bits are 1, the result of that bit is 1, otherwise 0
| Bitwise OR operator: As long as the corresponding two binary has one for 1 o'clock, the result bit is 1.
^ Bitwise XOR Operator: When two corresponding binary differences, the result is 1
~ Bitwise Inverse operator: Reverse each bits of the data , which turns 1 to 0 and 0 to 1
a = 10000000 =
~a
result:01111111 = 127
<< left move operator: Each binary of the operand all shifts left several bits, the number of "<<" to the right to specify the number of bits moved, high drop, low 0.
A = 10110011 = 179
A << 2
result:1011001100
>> Right Move operator: move all the binary of the left operand of ">>" to the right of several bits, andthe number to the right of ">>" to specify the number of bits to move
A = 10110011 = 179
A >> 2
result:00101100 = 44
Bit operators are generally used for binary operations, generally used in the lower level, we seldom use, know on it.
2, octal and hex
in Python, the hexadecimal number should be written like this:
Eight decimal numbers should be written like this:
3. Variables
variable (variable) is another concept that needs to be well known. The variables in Python are well understood. A variable basically represents (or references) a worthy name. For example, if you want to use the name X to represent 5, just execute the following statement:
Such an operation is called Assignment (Assignment), and the value 5 is assigned to the variable x. Another argument is that the variable x is bound to the value (or object) 5 above. After the variable is assigned, you can use the variable in the expression.
Naming rules for variables:
Variable names can only be any combination of letters, numbers, or underscores;
The first character of a variable name cannot be a number;
Variables are case-sensitive;
Rule of law, variable names should be meaningful, do not use Chinese to do variable names, do not using Pinyin;
The keyword cannot be declared as a variable name.
4. Comments
The program is very long time, do not understand the code does not know what the code is, this time what to do?
When we read a book, there is no understanding of the place, we generally will mark.
We write the same program, I am in the code next to it is very convenient.
Annotations are available in two ways:
Single-line Comment #
Multiline comment "" "Content" ""
Role:
Avoid yourself forgetting to write what the code is doing;
Written to people;
Don't comment on what your code does, but comment on what my code does.
Linux/unix What users need to be aware of:
Special NOTES:
In some Python files we will see that the first line of the file is
#!/usr/bin/env python
This line is a special note, he has a special role, known as shebang, generally appears in the Linux/unix.
Shebang is a string line (#!) consisting of "#" and "!", and she appears on the first line of the file. When Shebang appears in the file, the Linux/unix operating system's program loader parses the contents of the Shebang, takes the subsequent content as the Interpreter command, and invokes the execution, which will contain the Shebang file path as the interpreter parameter.
Here in #! It is used first to help the kernel find the Python interpreter, but it will be ignored when importing the module, so it is only necessary to add the #! to the directly executed file.
5. Character encoding
when the Python interpreter loads the code in the. py file, the content is encoded (default Ascill). ASCII (American Standard Code for Information interchange, United States Standards Information Interchange Code) is a set of computer coding systems based on the Latin alphabet, mainly used to display modern English and other Western European languages, which can be used up to 8 Bit to represent (one byte), that is: 2**8 = 256-1, so the ASCII code can only represent a maximum of 255 symbols.
about Chinese
To deal with Chinese characters, programmers designed GB2312 for Simplified Chinese and big5 for traditional Chinese.
GB2312 (1980) contains a total of 7,445 characters, including 6,763 Kanji and 682 other symbols. The inner code range of the Chinese character area is high byte from B0-f7, low byte from A1-fe, occupy code bit is 72*94=6768. 5 of these seats are d7fa-d7fe.
GB2312 supports too few Chinese characters. The 1995 Chinese character extension specification GBK1.0 contains 21,886 symbols, which are divided into Chinese characters and graphic symbol areas. The Chinese character area consists of 21,003 characters. The 2000 GB18030 is the official national standard for replacing GBK1.0. The standard contains 27,484 Chinese characters, as well as Tibetan, Mongolian, Uyghur and other major minority characters. Now the PC platform must support GB18030, the embedded products are not required. So mobile phones, MP3 generally only support GB2312.
From ASCII, GB2312, GBK to GB18030, these coding methods are backwards compatible, meaning that the same character always has the same encoding in these scenarios, and the latter standard supports more characters. In these codes, English and Chinese can be handled in a unified manner. The method of distinguishing Chinese encoding is that the highest bit of high byte is not 0. According to the programmer, GB2312, GBK, and GB18030 belong to the double-byte character set (DBCS).
Some Chinese Windows default internal code or GBK, you can upgrade to GB18030 through the GB18030 upgrade package. But GB18030 relative GBK increases the character, the ordinary person is difficult to use, usually we still use the GBK to refer to the Chinese Windows inside code.
It is clear that the ASCII code cannot represent all the words and symbols in the world, so it is necessary to create a new encoding that can represent all the characters and symbols, namely: Unicode
Unicode (Uniform Code, universal Code, single code) is a character encoding used on a computer. Unicode is created to address the limitations of the traditional character encoding scheme, which sets a uniform and unique binary encoding for each character in each language, which specifies that characters and symbols are represented by at least 16 bits (2 bytes), that is: 2 **16 = 65536,
Note: Here is a minimum of 2 bytes, possibly more
UTF-8, which is compression and optimization of Unicode encoding, does not use a minimum of 2 bytes, but instead classifies all characters and symbols: the contents of the ASCII code are saved with 1 bytes, the characters in Europe are saved in 2 bytes, and the characters in East Asia are saved in 3 bytes ...
Therefore, when the Python interpreter loads the code in the. py file, it encodes the content (the default ascill), if it is the following code:
Error: ASCII code cannot be expressed in Chinese
Correction: It should be shown to tell the Python interpreter what code to use to execute the source code, i.e.:
6. Get user input
When writing a program, you do not need to know the specific meaning of the variable. Of course, the interpreter eventually has to know the value of the variable. However, how can it know this value? The interpreter knows only what we tell it.
In fact, by writing a program for others to use, we can't predict what value the user will give the program. Well, look at the very useful input function.
7. Python type conversion
Function description
int (x [, Base]) converts x to an integer
Long (x [, Base]) converts x to a long integer
Float (x) converts x to a floating-point number
Complex (real [, Imag]) creates a complex number
STR (x) converts an object x to a string
REPR (x) converts an object x to an expression string
Eval (str) is used to calculate a valid Python expression in a string and returns an object
Tuple (s) converts a sequence s to a tuple
List (s) converts the sequence s to a list
Chr (x) converts an integer to one character
UNICHR (x) converts an integer to a Unicode character
Ord (x) converts a character to its integer value
Hex (x) converts an integer to a hexadecimal string
Oct (x) converts an integer to an octal string
For example, if you want to multiply the 2 input variables, you must do a type conversion.
8. If statement of Process Control
We know how to get the user input, now to write a guess the number of the game, we think about it, first we run the program, and then let the user enter a number, then the program to determine whether the user entered the correct number, and return to determine the result.
You need to use an if statement to judge. The structure of the IF statement is:
If judgment condition: Execute statement ... var = input ("Enter:") if var = = "A": print ("True")
Attention:
Indent--Four spaces recommended (use 2, 3 spaces or tab is available)
Don't mix tabs with spaces
The display logic for the spaces is always the same for different software, but there are a variety of tabs.
Some software expands the tab into a space, and some does not. Some tab widths are 4, some widths are 8,
These inconsistencies can make the code confusing, especially by indenting the python that represents the block structure.
We continue to write our guessing numbers game.
Before we write the guessing number game, only when the user input the correct time, will have the prompt, that input wrong time, is not also should prompt?
You need to use the If-else statement here.
If statement if the condition: Execute statement ... else: EXECUTE statement ...
Flowchart for If statement
There are three structures for if statements
9. Process Control while Loop
Now there is a requirement, from 1 to 10, to print 10 numbers, which can be implemented in the following ways:
Python to implement the loop function, the code is written according to the method described above.
Nested loops
If I want to implement this function, print 10 lines of numbers on the console, 5 columns per row, 1,2,3,4,5, how do I do that?
The implementation code is as follows:
The output results are as follows:
Break statement
We need to change, the above print 10 rows and 5 columns of numbers, I would like to print the 3rd line of numbers, then exit the loop, if it is implemented?
The implementation code is as follows:
The output results are as follows:
To use the break statement here, the break statement will end the current loop.
Continue statements
Now, in the number matrix of 10 rows and 5 columns, I just want to print the 1,2,3,5 column, how can I do that?
The output results are as follows:
To use the Continue statement here, the function of the continue statement is to exit the current loop and proceed to the next loop.
Get to know Python quickly