How many people can't python Control structure of , Tell me in the comments section , Let me ask questions one by one . today , Just teach you python Control structure of …
• A statement takes up one line
• Long statements can take up more than one line , Use \ or () Control line feed
•[]、{}、() You can directly span multiple lines , In the list 、 Dictionaries 、 When a new line is needed in a tuple, there is no need to add a continuation character
a = 1
b = 2
c =a +b
income = (gross_wages
+taxable_interest
- ira_deduction)
• Four English blanks
• The indent length of statements in the same code block should be equal
• The first line of the file does not need to be indented
python Exchange of learning Q Group :906715085###
if Conditional expression :
Execute statement
......
else:
Execute statement 1
......
• In order from top to bottom , The execution of a statement
The most basic structure
The code is top-down , Execute sequentially
Write the corresponding sentences in the order of solving the problem
Start
Open the refrigerator door
Refrigerate the elephant
Close the refrigerator door
end
Execute the decision according to the judgment
• Use if…else… The statement means
Open the refrigerator door
if An elephant can load :
Refrigerate the elephant
else:
Find a bigger refrigerator
Close the fridges
• Through the execution of one or more statements (True or False) To determine the code block to execute
• The basic form is if…else… sentence
if Conditional statements and else There is a colon after
Statements must be in if and else The code block after the statement should be indented
Judgment conditions are often used >、<、==、>=、<=
When the judgment condition is true , Execute code block 1
When the judgment condition is not tenable , Execute code block 2
Single branch
• only one if sentence
If the condition is correct, a one-way if sentence
If and only if the condition is True when , To execute the corresponding operation
if salary >= 10000:
print("I am Happy!")
•if-else Statement determines which action to perform according to whether the condition is true or false
If the judgment condition is True when , execute if Code block under statement
If the judgment condition is False when , perform else Code block under statement
if salary >= 10000:
print("I am Happy")
else:
print("I am Fine!")
• Achieve more refined conditional judgment
elif yes else if Abbreviation , There can be multiple elif
elif Is a more refined judgment condition , And end with a colon
Condition judgment matches from top to bottom , Execute the corresponding block statement when the condition is met , Follow up elif and else No more execution
At this point, pay attention to the order of conditions
if salary >= 10000:
print ("happy")
elif salary >= 5000:
print("ok")
else:
print("sad")
• As long as the conditions are met , It's been circulating
Under certain conditions , Loop through a program , It is used to deal with repeated and identical tasks
while There is a colon after the statement
When the judgment condition is true , Execute code block 1, The code needs to be indented
When the judgment condition is false , Execute code block 2, Code should be UN indented
Pay attention to prevent dead circulation
while Judge the condition :
Code block 1
Code block 2
for There is a colon at the end of the statement
Statements in a loop need to be indented
range() Function to create a list of integers , Generally speaking, it is related to for Recycling in combination
range(start, end, step=1)
range(5): 0,1,2,3,4 No, 5
range(1,5):1,2,3,4 No, 5
range(1,5,2): 1,3
•for Variable name in range(5)
patients = ["Alice","Bob","Cathy","Eric"]
for index in range(len(patients)):
print(index,patients[index])
result :
0 Alice
1 Bob
2 Cathy
3 Eric
Iterate directly over each element in the sequence
for The statement must end with a colon
Statements in a loop need to be indented
Substitute each element into a variable x, Then execute the indented code block
for x in sequence
Code block
patients = {
"Alice","Bob","Cathy","Eric"}
for patient_name in patients:
print(patient_name)
result :
Alice
Bob
Cathy
Eric
for index,x in enumerate(sequence):
Code block
•enumerate() Function converts a sequence object into an index sequence , And return the index and element of the sequence object
Index Count the index returned ( from 0 Start )
x For the returned sequence element
patients = ["Alice","Bob","Cathy","Eric"]
for index,patient_name in enumerate(patients):
print(index,patient_name)
result :
0 Alice
1 Bob
2 Cathy
3 Eric
break&continue
break
• Use in while and for In circulation
• Used to exit the loop ahead of time
• Usually cooperate if Statements use
continue
• be used for while and for loop
• Skip the current loop , Start the next cycle directly
• Usually cooperate if Statements use
Don't abuse break and continue sentence
•break and continue Too many code execution logic forks , It's easy to make mistakes
• Most loops do not need to be used break and continue sentence
If there is something wrong with the code , It's going to trap the program ” Dead cycle “, have access to Ctrl+C Exit procedure , Or forced end python process
That's the end of today's sharing , If you like it, remember to like it , See you in the next chapter !!!!