程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python] control structure. I dont give this tutorial to ordinary people

編輯:Python

Preface

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 …

Branch and indent

branch

• 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)

Indent

• 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
......

Sequential structure

• 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

Selection structure

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 、 Double branch 、 Multi branch structure

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!")

Double branch

•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!")

Multiple branches

• 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")

Loop structure

while loop

• 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-in loop

• Method 1: Use range() function

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

• Method 2: Use sequence items to iterate over list objects

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

• Method 3: Use enumeration functions to iterate over sequence objects

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

Last

That's the end of today's sharing , If you like it, remember to like it , See you in the next chapter !!!!


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved