Home > Article > Backend Development > Summary of python must-know knowledge points
This article mainly introduces the relevant information that summarizes the necessary knowledge for learning Python. Friends who need it can refer to it
1. Variables
1. Variables
•refers to the variables during program execution. , a variable quantity;
• Defining a variable will be accompanied by three characteristics, namely memory ID, data type and variable value.
•Before running other languages, you must manually release the memory space of the program. However, the python interpreter has its own memory recycling mechanism. Once the python program finishes running, the memory space will be automatically released.
age=10
print(id(age),type(age),age)
2. Constant
•Refers to an immutable quantity during program execution ;
•Constants are generally defined in uppercase letters.
AGE=10
print(AGE)
3. Naming method of variables
•Camel case
AgeOfOldboy=72
• Underscore
age_of_oldboy=72
2. Interacting with the program
In ancient times, when we went to the bank to withdraw money, we needed a bank clerk waiting for us to enter our account and password to him. , and then he goes to verify and wait for success. Then we enter the withdrawal amount and tell him.
Proud modern people will provide customers with an ATM machine (that is, a computer), allowing the ATM machine to interact with the user, thus replacing human power. However, the machine is dead, and we must write programs for it to run. This requires that our programming language has a mechanism that can interact with the user and receive user input data.
1.python3
•Python3 supports UTF-8 Chinese encoding by default. python2 needs to add # -*- coding:utf-8 -*- at the head of the code.
•Input in python3, no matter what type of value is entered, it will be saved as str (string) type
name=input('please enter the username: ')
print(id(name ),type(name),name)
2.python2
•raw_input in python2 is the same as input in python3;
name=raw_input('please enter the username: ')
print(id(name),type(name),name)
•In input in python2, a value must be entered, and the value will be saved as what type it is.
name=input('please enter the username: ')
print(id(name),type(name),name)
3. Data type
1. int integer type
•Generally used to define age, ID number, QQ number, level, etc.
age=18
id=130530198805240011
qq=379048558
level=99
2.float floating point type
•Generally used to define height, weight, salary, etc.
height=1.81
height=float(1.81)
3 .str string type
•Generally used to define a person's name, gender, status, etc.;
•Generally, strings are placed in single quotes, double quotes, or triple quotes.
name='egon'
sex='female'
age=18
•Use "+" for string concatenation
name='egon'
sex='female'
age=18
print(name+sex+str(age))
Note: The age variable value here is 18, which is an integer and cannot be used for string concatenation. , need to use str(age) to convert to string type.
•Use "*" for string concatenation
name='egon'
print(name*10)
4.bool Boolean value type
• There are only two values: True and False;
•Mostly used for judgment.
age=73
AGE=18
print(age < AGE)
print(age > AGE)
5. Convert each type to each other
•Integer type——>Floating point type
a=18
print(float(a))
•Floating point type——>Integer type
a=1.81
print(int(a))
•Floating point type——>String type
a=1.81
print(str(a))
•Integer type——>String type
a=18
print(str(a))
4. Array type
1. List []
•List in python is defined in [], with "comma" separating the elements;
info=['egon','alex',18]
print(info[2])
•Elements can be any data type, any array type;
•Character elements need to be enclosed in quotes, integers, floating point types, lists, etc. No quotes required.
info=[13,18.1,'alex',['egon','tony']]
print(info[3][0])
2.Dictionary{ }
•Dictionaries in python, also called associative arrays, are defined within {}, and the elements inside are expressed in the project name: project content format, and the elements are separated by "commas";
info= {'name':'egon','sex':'male',3:18}
print(info[3])
•The project content can be any data type, any An array type;
•The string type in the project content needs to be enclosed in quotation marks, and the integer type, floating point type, list, etc. do not need quotation marks.
info={'Name':'Aigen','Gender':'Male','Muscle':['Yes','No']}
print(info['Muscle' ][1])
info={'Name':'Aigen','Gender':'Male','Muscle':123}
print(info['Muscle'])
info={'Name':'Aigen','Gender':'Male','Muscle':18.1}
print(info['Muscle'])
info ={'Name':'Aigen','Gender':'Male','Muscle':'None'}
print(info['Muscle'][1])
5. Formatted output
•my name is xxx, my age is xxx
•You need to use the placeholder %s
name=input('user_name>>: ')
age=input('user_age>>: ')
print('my name is %s, my age is %s' %(name,age))
6. Operators
1. Arithmetic operators
•+ - * /
print(5+5) #5 plus 5 equals 10
print(5-5) #5 minus 5 equals 0
print(5*5) #5 times 5 equals 25
print(5/2) #5 divided by 2 equals 2.5
•Find the integer part of the quotient // Find the remainder part of the quotient % power* *
print(5//2) #The quotient of 5 divided by 2 is equal to 2 and the remainder is 1, only take the quotient 2
print(5%2) #The quotient of 5 divided by 2 is equal to 2 and the remainder is 1, only Get the remainder 1
print(3**2) #3 raised to the 2nd power is 3 times 3 equal to 9
2. Comparison operators
•> < >= <= == !=
print(30 > 20)
print(30 < 20)
print(30 >= 30)
print(30 <= 30)
print(30 == 30)
print(30 != 40)
3. Logical operators
•Logical AND and Logical OR Logical NOT Bitwise AND & Bitwise OR |
•Logical AND, all conditions must be met before the result is True;
•Logical or or, only one condition is met, the result is True;
•Logical NOT, the result is negated.
name='egon'
age=18
print(age > 15 and name == 'egon')
print(age > 15 or name != 'egon' )
print(not age > 15)
The above is the detailed content of Summary of python must-know knowledge points. For more information, please follow other related articles on the PHP Chinese website!