catalogue | concrete content |
---|---|
Judgment statement | 1: Use format of if statement 2: Use format of if else 3: Use format of if elif else 4: if nesting |
Circular statement | 1: Format of while loop 2: while loop nesting 3: Format of for loop |
1, Judgment statement
In the program, if some conditions are met, you can do something, but if not, you are not allowed to do it. This is the so-called judgment
1.1 format of if statement
if Conditions to judge: What to do when the conditions are established
Case: judge age. If age is greater than 18, enter adult
age = 18 if age>18: print("You are an adult")
Note: the code is indented with a tab key or 4 spaces
1.2 format of if else
if Conditions to judge: What to do when the conditions are established else: What to do when the conditions are not tenable
Case: judge age. If age is greater than 18, enter adult; otherwise, enter minor
age = 18 if age>18: print("You are an adult") else: print("You are a minor")
1.3 format of if elif else
if Conditions to judge: What to do when the conditions are established elif Conditions to judge: What to do when the conditions are established else: What to do when the conditions are not tenable
Case: 80 < score < = 100, Grade A; 60 < score < = 80, grade B; Remaining, grade C
score = 72 if score >80 and score <=100: print("A") elif score >60 and score <= 80: print("B") else: print("C")
1.4 if nesting
if Conditions to judge: What to do when the conditions are established if Conditions to judge: What to do when the conditions are established else: What to do when the conditions are not tenable else: What to do when the conditions are not tenable
Case: if the balance on the bus card is greater than 2, you can get on the bus. Otherwise, please recharge the output. If the number of seats on the bus is greater than 0, you can take a seat. Otherwise, please stand firmly and hold it
monery = input("Please enter amount:") if int(monery)>2 : print("Please get in the car") seat = input("Please enter the remaining seats:") if int(seat) > 0: print("Please take your seat") else: print("Please stand still and hold it") else: print("Please recharge")
2: Circular statement
Code that needs to be executed repeatedly can be completed in a loop
Loops are not necessary, but in order to improve the reuse rate of code
2.1 while cycle
2.1.1 format of while loop
while condition: What to do when the conditions are established What to do when the conditions are established .....
- Case: cyclic printing of 1 ~ 100 data and
i=1 sum=0 while i<=100: sum+=i i=i+1 print(sum)
- Exercise: calculate the even sum between 1 and 100 (including 1 and 100)
i=1 sum=0 while i<=100: if i % 2 == 0 : sum +=i i+=1 print("1~100 Between all even numbers and:%d" %sum)
2.1.2 while loop nesting
while condition: What to do when the conditions are established What to do when the conditions are established ..... while condition: What to do when the conditions are established What to do when the conditions are established .....
- practice
- Print five sided triangles with nesting
* ** *** **** *****
i =1 while i<=5: j=1 while j<=i: print("*",end="") j+=1 print("\n") i+=1
- Practice printing the following drawings
***** **** *** ** *
- Print 99 multiplication formula
1*1=1 2*1=2 2*2=4 3*1=3 3*2=6 3*3=9 4*1=4 4*2=8 4*3=12 4*4=16 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
code
i=1 while i<=9: j=1 while j <=i : print("%d*%d=%d"%(i,j,i*j), end=" ") j+=1 print("\n") i+=1
2.2 for loop
2.2.1 for mat of for loop
for Temporary variable in Iteratable objects such as lists or strings: Code executed when the loop satisfies the condition
- practice
- String cyclic printing
name="jack" for i in name: print(i)
- Print data from 0 to 9
for i in range(10): print(i)
- break/continue/return
continue Function of: when the conditions are met,Used to end this cycle, followed by the execution of the next cycle break Function: when the conditions are met, it ends immediately break Cycle in which
- Case: while loop 1 to 9. When the number is 3, jump out of this loop and execute other loops
i=0 while i<=9: i+=1 if i ==3: continue print(i)
- Case: while loop 1 to 9. When the number is 3, stop the loop
i=0 while i<=9: i+=1 if i ==3: break print(i)
Note:
- break/continue can only be used in loops, and cannot be used alone
- In a nested loop, break/continue only works on the nearest loop
4. Use of pass
pass action
1. Python pass Is an empty statement to maintain the integrity of the program structure. 2. pass Do nothing, usually used as a placeholder statement.
for i in range(10): pass
task
1: Use a loop (either) to calculate 0--100 The sum of all odd numbers between 2:
Supplement: break/continue/return difference