Python BASICS (2) --- data type, python Data Type
1. Differences between python versions:
Comparison between 2.x and 3.x
Version |
2. x |
3. x |
Print |
Print "" or print () can be printed normally |
Only print (). Otherwise SyntaxError |
Input Raw_inut |
Input: outputs the native data type. If you input a value of any type, the native data type is output. Raw_input: all output in string format |
3. x cancels the raw_input method and can only use input () The method prompts the input string. This method is used with raw_input () of version 2. x () If you want to implement a value of the native data type with 2.x input, You can use eval (input ()) |
Class |
2. x supports new classes and classic classes. When new classes are used, the class inheritance sequence will affect the final inheritance result. |
New classes must be used to solve the issue of inter-class inheritance order |
/ |
For example, the output value of 1/2, 2. x is 0. |
For example, the output value of 1/2 3.x is 0.5. |
2. python Data Type
Int integer, such as 1,123,123 4,-1,-123..., the range is-2 ** 31 ~ The value range is 2 ** 31-1, depending on the operating system.
Float, such as 1.1, 1.12,-1.1,-1. 12...
Str string, such as 'hello', '123', 'abc'... the string must be enclosed by single or double quotation marks.
Boolean type. There are only two values, True: True, False: False. If the return value is True for any non-zero data type and the return value is 0, the return value is False.
The long type only exists in 2.x. The value range is infinite, depending on the available virtual memory.
The complex number of complex, such as 3.12j, 2.45e-4.7...
Tuples (tuple) such as ('1', 'abc', 'Hello ').
List, for example, ['A', 'abc', 'python'].
Dictionary (dict) such as {'name': 'Tom ', 'age': '20', 'job': 'it '}.
3. python Encoding
Python 2.x defaults to string character encoding, and 1 character can only be 8 bits. You can use built-in functions, chr () and ord () for character conversion.
Python 3.x uses the unicode encoding format by default. You can use the built-in functions unichr () and ord () to convert characters.
4. python naming rules
The python variable name (identifier) can only start with a letter or underline and cannot contain special characters. Note that the keywords reserved by python cannot be used as the variable name, which is equivalent to rewriting the built-in method of python, this may affect the calling of other methods. For more information about reserved characters in python, see section 4.1.
We recommend that you use a uniform style for variable names, for example, TestLoginSucess or test_login_success.
An identifier starting with a single leading underline, which means the conventions are private.
Two identifiers starting with a leading underscore (_) indicate strongly dedicated identifiers.
If the identifier ends with two underscores, the identifier is a special name of the language.
In addition, it should be noted that Python is a dynamic advance, that is, when defining a variable, you do not need to specify the Data Type of the variable. Python will automatically change the data type of the variable when a variable is assigned a value.
4.1python Reserved Words
# By importing the keyword module, we can check which keywords are currently retained by python import keyword # view the key word reserved by python. kwlist #2. x outputs [and, as, assert, break, class, continue, def, del, elif ', 'else', 'Got t', 'exec ', 'Finally', 'for ', 'from', 'global', 'if', 'import', 'in ', 'Is ', 'lambda', 'not ',' or ', 'pass', 'print', 'raise ', 'Return', 'try', 'while ', 'with', 'yield '] #3. x outputs ['false', 'none', 'true', 'and', 'as', 'assert ', 'Break', 'class', 'contine ', 'def', 'del ', 'elif', 'else', 'partition t', 'Finally', 'for ', 'from', 'global', 'if ', 'import', 'in', 'is ', 'lambda', 'nonlocal', 'not ', 'or', 'pass', 'raise', 'Return ', 'try', 'while ', 'with', 'yield'] # Note: 3. x adds ['false', 'none', 'true', 'nonlocal'] and removes the 2.x ['exec '] keyword, should Follow 3. the keyword of x to facilitate backward compatibility # If you are not sure whether the variable is named as a keyword, you can also use the following method to check whether it is a keyword. iskeyword ('name') # If False is returned, this variable is not a reserved keyword.
View Code
5. python annotations
Python can be annotated in two ways. One is a single-line comment. You can add the # symbol before the statement or use multiple lines of comment, use three consecutive single quotes to add two sides to the content range to be annotated, or use three consecutive double quotes.
Example:
'''
Single quotes
'''
"""
Double quotation mark comment
"""
6. python syntax
Python is famous for its conciseness. It abandons the other {} curly braces (such as c) writing methods and the readability of the language in the future. It requires syntax indentation, and the code indentation of the same statement block must be the same, indentationError may occur in charge of indentation. If you want to write multiple statements in one line, you can separate them with good scores.
7. python Operators
7.1 Arithmetic Operators
Arithmetic Operators
Operator |
Description |
Example |
+ |
Addition |
>>> 14-5 9 |
- |
Subtraction |
>>> 14 - 59 |
* |
Multiplication |
>>> 5 * 1470 |
/ |
Division |
>>> 14 / 52.8 |
% |
Modulo, that is, the remainder of two operators |
>>> 14 % 54 |
** |
Power Operation |
>>> 2 ** 38 |
// |
Returns the integer part of the quotient of two numbers. |
>>> 10 // 33 |
7.2 comparison operators: Comparison operators are not limited to numbers, strings, lists, and so on.
Comparison Operators
Operator |
Description |
Example |
= |
Determine whether two objects are equal (True is returned if they are equal, False is returned if they are not equal) |
>>> 'abc' == 'abc'True |
! = |
Checks whether the connected objects are not equal (opposite to =). If they are equal, False is returned. If they are not equal, True is returned) |
>>> 1 != 2True |
<> |
Determine whether the connected objects are not equal (same as above, but not recommended) |
|
> |
Judge whether the first object is greater than the second object. If the value is greater than True, no greater than (including equal), return Fasle. |
>>> 3 > 1True |
< |
Judge whether the first object is smaller than the second object. If the value is smaller than or equal to True, return Fasle. |
>>> 3 < 4True |
> = |
Determines whether the first object is greater than or equal to the second object. If the value is greater than or equal to the second object, True is returned. Otherwise, False is returned. |
>>> 3 >= 3True |
<= |
Determines whether the first object is less than or equal to the second object. If the value is less than or equal to the second object, True is returned. Otherwise, False is returned. |
>>> 3 <= 4True |
7.3 value assignment operator: Performs value assignment.
Value assignment operator
Operator |
Description |
Example |
= |
Assignment |
A = c |
+ = |
Auto-increment value a + = 1 is equivalent to a = a + 1 |
A + = 1 |
-= |
The auto-subtraction value a-= 1 is equivalent to a = a-1. |
A-= 1 |
* = |
The auto-multiplication value a * = 1 is equivalent to a = a * 1. |
A * = 1 |
/= |
The auto-division value a/= 1 is equivalent to a = a/1. |
A/= 1 |
% = |
The automodulo value a % = 1 is equivalent to a = a % 1. |
A % = 1 |
** = |
The self-calculated power value a ** = 1 is equivalent to a = a ** 1 |
A ** = 1 |
// = |
Auto-Division assignment a // = 1 is equivalent to a = a // 1 |
A // = 1 |
7.4-bit operator: Performs logical computing based on binary
Bitwise operators
Operator |
Description |
Example |
& |
Bitwise AND |
>>> 5 & 144 |
| |
By bit or |
>>> 5 | 1415 |
^ |
Exclusive or |
>>> 5 ^ 1411 |
< |
Move left |
>>> 14 << 256 |
> |
Right Shift |
>>> 14 >> 23 |
8. python data type operations
8.1 variables and constants
Variables are used to store the identifiers of data that can be changed temporarily during the program running. python has its own memory collection mechanism. Therefore, you do not need to consider destroying variables during development.
Python is different from other static languages such as C. The Interpreter creates a data object in the memory when the python variable is assigned a value, and then creates a variable, and point the variable to the previously created data object. It is a bit like a pointer in C language, so some people say that although python does not have a pointer concept, everything is a pointer.
8.2 assign values to variables
A = 2 # assign a value to variable a to 2b = a # assign the value of variable a to variable B, equivalent to directing a to the memory space 3 at the same time to B, at this time, B is equal to 3a = 5 # assign a value to the variable as 5, because the previous a has been assigned a value, the python interpreter will change the memory space pointed to by a to 5 print (a, B) (5, 2) # print the output to 5, 2 at this time, because although B =, and the value of a is changed, but B = a only points B to the memory address pointed to by a, and does not actually point to a and, only change the memory address that a points to. When multiple variables point to the same address space, python's memory recycle mechanism will mark this memory space, the number of people who reference this memory space is equal to + 1 until the reference is 0. At this time, the python interpreter will reclaim the memory space, this is also one of the biggest features that differ from other development languages. You do not need to pay attention to memory collection,
View Code
# To check whether the current two variables point to the same memory address, you can use the id () method a = 2 print (id ()) # The print result is 140723441642548b = aprint (id (B) # The print result is 140723441682448 # visible. both a and B point to the same address space. Note: the preceding two values are related to the platform used. They are not necessarily the same values, but they must be equal to a = 5 print (id ()) # print the result as 140723441682376 print (id (B) # print the result as 140723441682448 # by observing the pointer changes of the two variables, we can find that, changing the value of a does not affect the value of B that has been assigned a value.
View Code