Declaration and assignment of variables
The declaration and assignment of variables are used to bind a variable to an object in the following format: variable name = expression (literal)
In retrograde, the interpreter runs the expression on the right, generates an object representing the result of the expression, and assigns the address of that object to the variable on the left.
Operation A variable must be initialized (assigned first) before it can be used.
Remove variables and garbage collection mechanisms
Variables that are no longer used can be deleted through the DEL statement
del str
print(str)
Copy the code
If the object has no variable reference, it is collected by the garbage collector, emptying memory.
Chain assignment
Used to assign to multiple variables from the same object
x = y = z = 100
print(x, y, z) # 100 100 100
Copy the code
Series unpack assignment
Assign to variables corresponding to the same number (the number must be the same)
a, b, c = 1, 2, 3
print(a, b, c) #1, 2, 3
Copy the code
[Operation] Use a series of unpackage assignment to achieve the exchange of variables
a, b = 1, 2
a, b = b, a
print(a, b) # 2, 1
Copy the code
constant
Python does not support constants, that is, there are no syntax rules that restrict changing a value that is two lengths long. We can only specify the naming conventions for constants and do not programmatically change the values of constants.
MAX_VALUE = 100
print(MAX_VALUE)
Copy the code
Introduction to basic built-in data types
Every object has a type. The basic data types in Python are:
- Integer: integer
- Floating point: decimal
- Boolean: true, false
- String: A sequence of characters
digital
Operation of numbers:
We can also get both the quotient and remainder by using the function divmod() :
print(divmod(12, 5)) # (2, 2)
Copy the code
The integer
In Python, we have base 10, and there are three other bases:
- 0b or 0b, binary: 0,1
- 0o or 0o, octal: 0,1,2,3,4,5,6,7
- 0x or 0x, hex: 0,1,2,3,4,5,6,7,8,9, a, b, c, d, e, f
Type conversion with int() :
- Floating-point numbers directly omit the decimal part.
- Boolean values: true=1, false=0.
- If the string is an integer (not a floating point number), it is directly converted to the corresponding integer.
Floating point Numbers
Floating point number. The Vertina tree is identified by scientific notation in the form of a×b to the tenth power. For example, 3.14 is expressed as 314E-2 or 314E-2.
Type conversion and rounding:
- Like int (), we use float() to automatically convert its type to float.
- When an integer and a floating-point number are mixed, the result of the expression is automatically converted to a floating-point number.
- Round (value) can return a rounded value.
Time to say
In computers, time is represented in milliseconds (1/1000s) from 00:00:00, January 1, 1970, or what we call Unix time
In Python, you can get the current moment with time.time().
Boolean value
Python defines true and false as keywords, but they’re still essentially 1 and 0, and can even be added to numbers.
Comparison operator
All comparison operators return true when 1 and false when 0.
Logical operator
Identical operator
The same operator is used to compare the short memory circles of two objects, which are actually compared to the addresses of the objects.
Difference between is and == :
- Is is used to determine whether two variable reference objects are the same, i.e. the address of the object is compared.
- == is used to determine if the value of a reference variable refers to an object is equal. By default, the object’s __eq() method is called.
Integer caching problem Python only caches against smaller certificate objects (range [-5,256]), not all integer objects. Note that this is only executed on the command line, and is not the same when executed by Pycharm or saved as a file because the interpreter is partially optimized (range [-5, any positive integer])
Conclusion:
- Is compares whether the IDS of two objects are the same and refer to the same memory address.
- == compares whether the contents and values of two objects are equal.
- Small positive integer objects [-5,256] are placed in the cache for reuse at the global interpreter scope.
- The is comparator is more efficient than == and should be used when comparing variables with None.