I. Value
Every date type in python is a type.
Any data type in Python belongs to a certain type.
For example, 3 belongs to the integer type, 3.14 belongs to the floating point type, and 'abc' belongs to the string type.
# Use the type () function to view the variable type (3) #=> <class 'int'> type (3.14) #=> <class 'float'> type ('abc') #=> <class 'str'>
Ii. type check
Python is a language with strong type check.
For example, when you perform another four arithmetic operations, the operator checks the type of the calculation object.
#2.2 type check 3 + 4 #=> 73 * 'AB' # => 'abababab' 'AB' + 'ac' # => 'abac' 3 + 'AB '# = TypeError: unsupported operand type (s) for +: 'int' and 'str' str (3) + 'AB' # => '3ab'
When the string and number are added, Python checks that the type does not match and automatically reports an error. Of course, python does not check all type errors. For example, Python does not check the type when comparing 3 <'AB' in earlier versions (the latest version is available ). Therefore, a good type specification should be developed.
Develop good type specifications.
Iii. variable types
The binding method between variable types and values is dynamic.
When a value is assigned to a variable, the variable inherits the value type. However, if the value of the variable changes, the type also changes.
#2.3 variable type x = 3 type (x) # => <class 'int'> y = 'abc' type (y) #=> <class 'str'> x = ytype (x) #=> <class 'str'>
When the value of variable x is 3, x is an integer. When the value 'abc' of y is assigned to x, the x type is changed to the string type. Therefore, to avoid unnecessary errors, you should develop good programming habits:
Do not arbitrarily change the variable type.
Iv. Notes
Python supports single-line and multi-line comments. Single-line comments are marked with #, and multi-line comments are marked with '''comment content. To develop good programming habits
A year later, you still understand your code.
5. Condition statements
If condition:
Instruction Set
Elseif conditions:
Instruction Set
Else:
Instruction Set
The colon is very important because it declares that the next series of commands will be a code block.
#2.5 determine the parity of x = 15if (x/2) * 2 = x: print ('even ')
Print ('even') else: print ('odd ')