1. Sequential StructureFrom top to bottom, from left to right, skip any indented methods or class methods directly2. Branching Structure (Selection Structure)(1) if~elseif condition: execute statementelse: execute statement
- if and else are mutually exclusive; only one clause can be executed during each run of the code
- The branching structure must have at least one if; it cannot consist of only an else statement
- Python does not have a switch statement
ex.–content=int(input(“Please enter an integer:”)) # input receives any content as str typeif content<5: print(“The entered number is less than 5”)else: print(“The entered number is greater than or equal to 5”)(2) if-elif-else# Nested Selection StructureMethod 1:if content<=5: if content<5: print(“The entered number is less than 5“) else: print(“The entered number is equal to 5“)else: print(“The entered number is greater than 5“)Method 2:if content<5: print(“The entered number is less than 5”)elif content==5: print(“The entered number is equal to 5“)else: print(“The entered number is greater than 5”)ex.–Check if the entered number is divisible by 2 and 3number=int(input(“Please enter a number:”))if number%2==0: if number%3==0: print(“Divisible by 2 and 3”) else: print(“Divisible by 2, not divisible by 3”)else: if number%3==0: print(“Not divisible by 2, divisible by 3”) else: print(“Not divisible by 2, not divisible by 3”)3. Loop Structurefor, while, no do~whileex1.–Sum from 1 to 100for i in range(1,101): total+=iprint(“The sum from 1 to 100 is: %d”%total)ex2.–Sum of all odd numbers from 1 to 10for i in range(1,11,2): total+=iprint(“The sum of odd numbers from 1 to 10 is: %d”%total)ex3.–Sum from 1 to 10i=1total=0while i<=10: total+=i i+=1print(“The sum from 1 to 10 is: %d”%total)ex4.–Print if divisible by 2, otherwise do not printfor i in range(1,11): if i%2==0: print(i) else: pass # Empty statement, placeholder statement, does nothing# break and continuefor i in range(1,11): # If i=5 do not print, continue to print later if i==5: continue # End this loop iteration, continue to the next loop else: print(i)for i in range(1,11): # If i=5 do not print, do not print later if i==5: break # End the entire loop else: print(i)# while statementwhile condition: statementwhile~else executes the else block when the condition is false or when break is not encountered# for statementfor <variable> in <sequence>: <statement>else: <statement>Use break to exit the current loop, use continue to end this loop iteration