I. variables:
Explanation: stores intermediate results of program operations for subsequent program calls.
1. The variable must contain numbers, letters, underscores, and any combination.
2. The variable cannot start with a number.
3. It cannot be a keyword in Python.
['And', 'as', 'assert ', 'Break', 'class', 'contine', 'def', 'del ', 'elif ', 'else', 'Wait t', 'exec ', 'Finally', 'for ',
'From', 'global', 'if', 'import', 'in', 'is', 'lambda ', 'not', 'or', 'pass ', 'print ', 'raise', 'Return ', 'try', 'while', 'with', 'yield ']
4. variables must be descriptive.
5. Chinese characters are not recommended for variables.
6. The variable cannot be too long.
7. Official recommendation: camper or underline style
Example: hump: ageofoldboy = 56
Numberofstudents = 80
Underline: age_of_oldboy = 56
Number_of_students = 80
8. Use ''," ", ''' to wrap the variable. # use the following three quotation marks to wrap the variable:
9. Use single double quotation marks
1 msg = "My name is Alex , I‘m 73 years old!"2 print(msg)
View code
10. Use of strings
User interaction input: All input data is of the string type.
- String addition: concatenates a string.
1 s1 = ‘123‘2 s2 = ‘asd‘3 print(s1 + s2)
View code
- Multiply string and number
1 S3 = 'China' 2 print (S3 * 8)
View code
Ii. constants:
Explanation: constant amount, for example, π ID card number
Use: Capital all variables> constant. Put it at the top of the file.
3. Notes:
Explanation: it helps you explain that there should not be many comments, but it should be refined.
1. Single line comment :#
2. Multi-line comment: ''' commented content ''' "commented content """
4. bool Value
Print (1> 2) # The result is false.
5. format the output
Code Format:
'My name is % s, % d' % ('wenbo', 18) this year)
Note: S: String D: integer F: floating point type
For example:
Method 1
1 name = input ('enter name: ') 2 age = input ('enter age:') 3 job = input ('enter job :') 4 holobby = input ('enter Hobbies: ') 5 6 MSG = "7 ------------ info of % s ----------- 8 name: % s 9 Age: % D10 job: % S11 holobby: % S12 ------------- end ----------------- 13 "" % (name, name, INT (AGE), job, holobby) 14 print (MSG)
View code
Method 2
1 DIC = {"name": 'Alex ', 'age': 18, 'job': 'it', 'hobby ': female} 2 3 MSG = "" 4 ------------ info of % (name) s ----------- 5 Name: % (name) S 6 age: % (AGE) D 7 job: % (job) S 8 holobby: % (holobby) S 9 ------------- end ----------------- 10 "% dic11 print (MSG)
View code
Method 3 (Simplified Version)
1 name = input ('enter name: ') 2 age = input ('enter age:') 3 job = input ('enter job :') 4 holobby = input ('enter Hobbies: ') 5 6 MSG = 'name: % s, age: % d, job: % s, holobby: % s' % (name, INT (AGE), job, holobby) 7 print (MSG)
View code
Note: In the formatted output, you only want to simply represent % and write it as %.
For example
1 MSG = 'My name is % s, % d this year, learning progress: 5% % '% ('wenbo', 18) 2 print (MSG)
View code
Conclusion: If you want to create a template for the string class or convert some positions of the string into dynamic input, you can splice the string and format the output.
5. Operation
Assume the following variables: A = 10, B = 20
1. Arithmetic Operation: +-*/% **
For example
1 A = 102 B = 33 print (a % B) # obtain the remainder, result 14 print (2 ** 2) # multiply by, result 45 print (A // B) # divide by and the result is rounded up. The result is 3.
View code
2. Comparison calculation: =! ==<>=<=
Assume the following variables: A = 10, B = 20
3. Value assignment: + =-= */% **
Assume the following variables: A = 10, B = 20
4. logical operation: Not and or
Priority: () Not and or: the same priority is calculated from left to right.
Note:: X or Y, X is true, the value is X, X is false, and the value is Y;
X and Y, X is true, the value is Y, X is false, and the value is X.
- Both sides are numerical operations, such:
1 x or Y2 if X is true: 3 return X4 else: 5 # This indicates that the Code cannot be executed
View code
1 x and Y2 if X is true3 return Y4 else: 5 return X6 # This is a description, the Code cannot be executed
View code
- Both sides are comparison operations, such:
1 print (3> 2 and 3 <4 or 5> 6 and 2 <5) 2 # first compare 3> 2 and 3 <4, that is, true and false, and the result is true, 3 # Compare 5> 6 and 2 <5, that is, false and true, and the result is false4 # Compare true or false, the result is true5 print (1> 1 and 3 <4 or 4> 5 and 2> 1 and 9> 8 or 7 <6) 6 print (true or false)
View code
5. Member operation: judge whether some elements are in the STR, tuple, list, dict, Set
6,
Python operator priority
The following table lists all operators with the highest to lowest priority:
5. Int bool conversion:
Int ---> bool: non-zero: True
Bool ---> INT: True 1, false 0
Variable/constant/formatted output/Operation