Operation supplement
Member operation
-
Judge whether an individual is in a group
-
In stay
-
not In be not in
name_list = ['Monkey D Luffy','Asso','Gongsun Li'] # Saved records name = input('Please enter the name to query:') # User input print(name in name_list) # query print(name not in name_list) # inverse query True # result
Identity operation
-
Judge whether the two data 'values' and' memory addresses' are equal
-
==The value is determines the memory address
-
l1 = ['red','yellow','pueplr'] # value l2 = ['red','yellow','pueplr'] # value print(l1 == l2) # Are the two values equal print(l1 is l2) # Is the id address the same print(id(l1),id(l2)) # id address query True # Query record False # Query record 2410481399880 2410481399816 # Query record
Process control
Process control is to control the execution process of things
There are only three situations in which any execution process can be used
- 1. The sequential structure runs from top to bottom
- 2. The branch structure may execute different processes according to different conditions during operation
- 3. Some codes of loop structure need to be executed repeatedly during operation
Know and know
- Conditions are converted to Boolean values to determine whether the subcode is executed
- In python, indentation is used to indicate the dependency of code
- Not all code can have subcodes
- Multiple lines of subcode belonging to a code must maintain the same indentation
- Four spaces are recommended for indentation in python
- If there is a colon at the end of the previous line of code, the next line of code is indented
1. Sequential structure
- Run from top to bottom
2. Branch structure
1. Single branch structure
-
Single branch structure
-
if condition: the sub code block executed after the condition is established
-
name = input('Please enter your name:') # Input: Mr if name == 'Mr': print('Hello') Please enter your name: Mr
-
-
if with else
-
else condition: the sub code block executed when the condition is not tenable
-
If is used in conjunction with else, only one of their subcodes will be executed forever (multiple else conditions start from the first, and the next will be executed only if all the above conditions are not true)
-
else requires sub code that will run only if none of the above conditions are true
name = input('Please enter your name:') #Input: 1 if name == 'Mr': print('Hello') else: print('Sorry, I made a mistake') Please enter name: 1 Sorry, I made a mistake
-
-
if, elif and else are used together
-
Elif can add multiple conditions between if and else (multiple elif conditions start with the first one in turn, and the next one will be executed only if all the above conditions are not true)
-
score = input('achievement') score = int(score) if score > 90: print('excellent') elif score > 80: print('good') elif score > 70: print('commonly') elif score > 60: print('pass') else: print('Failed to renew') Score 1 Failed to renew
-
2. Nesting of single support structure
-
name = 'Mr' Name = input('Enter name:') if name == Name : print("correct") Password = input('Please input a password:') password = "123" if Password == password : print('correct!!!') else: print('Error!!!') else: print("error") Enter name: Mr correct Please enter password: 123 correct!!! Name = input('Enter name:') if Name == 'Mr' : print("correct") Password = input('Please input a password:') if Password == '123' : print('correct!!!') else: print('Error!!!') else: print("error") Enter name: Mr correct Please enter password: 123 correct!!!
Cyclic structure
1.while loop
-
while condition: the subcode executed in the loop after the condition is established
-
while True : age = input('Guess my age') if age == '20': print('Congratulations, you guessed right') else: print('No, go on') Guess my age 1 No, go on Guess my age 20 Congratulations, you guessed right Guess my age
2.while + break
-
Break: end the loop of this layer (when the value is equal to the if judgment value, the loop of break layer will be ended)
-
while True : age = input('Guess my age') if age == '20': print('Congratulations, you guessed right') break else: print('No, go on') Guess my age 1 No, go on Guess my age 20 Congratulations, you guessed right
-
while True : age = input('Guess my age') if age == '20': print('Congratulations, you guessed right') else: print('No, go on') break Guess my age q No, go on
break the meaning of this layer
-
While can be nested. The while will end the loop at which level the break is located
-
while True: username = input('username:') password = input('password:') if username == 'Mr' and password == '123': print('correct') while True: dee = input('Enter what is required:') if dee == 'No,': print('well!') else: print('What shall I do?') print('bye') break break username:Mr password:123 correct What is required for input: what? What shall I do? bye
3. Global flag bit
- By giving True a variable name, using the variable name globally, add the variable name = False at the bottom of the while layer you want to end, and end the whole while loop