Summary of essential knowledge for python learning, and summary of essential knowledge for python
I. Variables
1. Variables
• Variable amount during Program Execution;
• Defining a variable is accompanied by three features: Memory ID, data type, and variable value.
• Before running other languages, you must manually release the program's memory space. However, the python interpreter comes with a memory reclaim mechanism. Once the python program runs, the memory space is automatically released.
Age = 10
Print (id (age), type (age), age)
2. Constants
• An unchangeable amount during program execution;
• Constants are generally defined with uppercase letters.
AGE = 10
Print (AGE)
3. variable naming
• Hump
AgeOfOldboy = 72
• Underline
Age_of_oldboy = 72
2. Interaction with programs
In ancient times, when we went to the bank to get the money, we needed a bank clerk waiting for us to input his account and password to him, and then he went to verify the result, let's enter and tell him the withdrawal amount.
Proud modern people will provide customers with an ATM (that is, a computer), so that the ATM can interact with users and replace manpower. However, machines are dead and we must write programs for them to run. This requires that our programming language can have a mechanism to interact with users and receive user input data.
1. python3
• Python3 supports UTF-8 Chinese encoding by default. Python2 needs to be added #-*-coding: UTF-8-*-in the Code header.
• Input in python3, no matter what type of input value is 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 python2, a value must be input. The type of the value will be saved as the type.
Name = input ('Please enter the username :')
Print (id (name), type (name), name)
Iii. Data Types
1.int integer
• It is generally used to define the age, ID card number, QQ number, and grade.
Age = 18
Id = 130530198805240011
Qq = 1, 379048558
Level = 99
2. float
• It is generally used to define height, weight, salary, etc.
Height = 1.81
Height = float (1.81)
3. str string type
• It is generally used to define the name, gender, and status of a person;
• A string is enclosed in single, double, and triple quotes.
Name = 'egon'
Sex = 'female'
Age = 18
• "+" For String concatenation"
Name = 'egon'
Sex = 'female'
Age = 18
Print (name + sex + str (age ))
Note: here the age variable is 18, which is an int integer and cannot be concatenated as a string. You need to use str (age) to convert it to the string type.
• "*" For String concatenation "*"
Name = 'egon'
Print (name * 10)
4. bool Boolean Type
• Only True and False values are supported;
• Mostly used for judgment.
Age = 73
AGE = 18
Print (age <AGE)
Print (age> AGE)
5. convert each type
• Integer --> floating point type
A = 18
Print (float ())
• Floating point --> integer
A = 1.81
Print (int ())
• Float type --> string type
A = 1.81
Print (str ())
• Integer --> string type
A = 18
Print (str ())
Iv. array type
1. List []
• The list in python is defined in [] and its elements are separated by commas;
Info = ['egon', 'Alex ', 18]
Print (info [2])
• The element can be any data type or any array type;
• Character elements must be enclosed by quotation marks. quotation marks are not required for integer, floating point, and list.
Info = [13, 18.1, 'Alex ', ['egon', 'Tony']
Print (info [3] [0])
2. dictionary {}
• A dictionary in python, also called an associated array, is defined within {}. Its elements are represented in the Project name: project content format, and separated by commas;
Info = {'name': 'egon', 'sex': 'male}
Print (info [3])
• The project content can be any data type or any array type;
• The string type in the project content needs to be enclosed by quotation marks, and no quotation marks are required for integer, floating point, and list types.
Info = {'name': 'aggene', 'Gender ': 'male', 'mymy': ['you', 'none']}
Print (info ['mysql'] [1])
Info = {'name': 'aggene', 'Gender ': 'male', 'mymy': 123}
Print (info ['mysql'])
Info = {'name': 'aggene', 'Gender ': 'male', 'mymy': 18.1}
Print (info ['mysql'])
Info = {'name': 'aggene', 'Gender ': 'male', 'mymy': 'none '}
Print (info ['mysql'] [1])
5. format the output
• My name is xxx, my age is xxx
• Use placeholder % s
Name = input ('user _ name >> :')
Age = input ('user _ age >> :')
Print ('My name is % s, my age is % s' % (name, age ))
Vi. Operators
1. Arithmetic Operators
• + -*/
Print (5 + 5) #5 + 5 equals 10
Print (5-5) #5 minus 5 equals 0
Print (5*5) #5 multiplied by 5 equals to 25
Print (5/2) #5 divided by 2 equals 2.5
• Evaluate the quotient integer part // calculate the quotient remainder Part % power **
Print (5 // 2) #5 divided by 2 vendors equals 2 + 1, only 2
Print (5% 2) #5 divide by 2 quotient equals 2 remainder 1, only take remainder 1
Print (3 ** 2) # The Power of 3 is 3 × 3 equals 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
• The logic and logic or the logic are not bitwise and & bitwise or |
• Logic and, all conditions must be filled before True is returned;
• Logic or, with only one condition satisfied, the result is True;
• The logic is not and the result is reversed.
Name = 'egon'
Age = 18
Print (age> 15 and name = 'egon ')
Print (age> 15 or name! = 'Egon ')
Print (not age> 15)