python branch and loop
1, Process control: controls how code is executed
According to the different execution methods of control code, the process control methods are divided into three types
1. Sequential structure: the code is executed in the sequential structure by default. The code is executed in order from top to bottom, and each code is executed only once
2. Branch structure (IF statement): you can choose to execute code according to conditions
3. for,while: let the code execute repeatedly. The code is only written once and executed many times
2, Branch structure (if)
1. if single branch structure
If.... Just....
a. Grammar
if conditional statement:
Code snippet (indent and press tab)
b. Explain
if - keyword, fixed writing method
Conditional statement - it can be any expression with results, such as specific data, variables that have been assigned, and the operation expression of some operation with results
: - Fixed writing, in English
Code snippet - keep an indent with if structurally, which can be one or more
age = 19 if age > 18: print('adult')
num = 18 if num % 2 == 0: print('even numbers') age = 18 if age >= 18: print('adult') num = 56 if num % 4 == 0: print('4 Multiple of')
2. if double branch structure
If... Just... Otherwise...
a. Grammar
if conditional statement:
Code segment 1 (code executed when the condition is true)
else:
Code segment not executed
b. Supplement: when the conditional statement is not Boolean, whether the condition is true or not depends on whether the result of the conditional statement is true or not after it is converted into Boolean
Boolean type conversion: all types of data can be converted to Boolean. When converting, all zero and null values will be converted to false, and others will be true
age = 4 if age > 18: print('adult') else: print('under age')
score = 97 if score == 100: print('buy a car') else: print('Don't buy a car') year = 2003 if year % 4 == 0 and year % 100 != 0: print('leap year') else: print('Ordinary year') num = 32 if num % 2 == 0: print('even numbers') else: print('Odd number') num = 32 if num % 2: print('Odd number') else: print('even numbers') str1 = '1' if str1 == '': print('empty') else: print('Non empty') str1 = '1' if str1: print('Non empty') else: print('empty')
3. if multi branch structure
Do different things according to different conditions
If... Just... If... Just... If... Just...
Method 1: --- conditions are met. If one of the conditions is true, other conditions may also be true
if condition 1:
Code snippet 1
if condition 2:
Code snippet 2
if condition 3
Code snippet 3
Method 2. If one of the conditions is true, the other conditions will never be true
if condition 1:
Code snippet 1
elif condition 2:
Code snippet 2
elif condition 3:
Code snippet 3
else:
Code snippet 4
age = 11 if 0 <= age <= 3: print('child') elif 4 <= age <= 17: print('juvenile') elif 18 <= age <= 38: print('youth') elif 39 <= age <= 69: print('middle age') else: print('old age')
age = 3 if age < 18: print('under age') elif 18 <= age <= 150: print('adult') else: print('This is not a person!')
4. if nesting
num = 10 if num % 2 == 0: print('even numbers') if num % 4 == 0: print('4 Multiple of') else: print('Odd number')
3, Circulation
1. for loop - makes code execute repeatedly
for variable in sequence:
Circulatory body
a. Description:
for, in --- keyword: fixed writing method
Variable ---- variable name
Sequence ---- data of container data type, such as string, list, dictionary, ancestor, set, iterator, generator and range
: -------- fixed writing method
Loop body ---- structurally, the loop body is one or more statements that maintain an indent with for
Logically, the code in the loop body is the code that needs to be executed repeatedly
b. Execution process:
Let the variables take values from the sequence one by one. Until they are finished, the loop body will be executed once for each value
The number of for loops is related to the number of elements in the sequence
2. range function - creates an arithmetic sequence
range(n) -- generate [0, n) equal difference sequence, and the difference is 1, for example: range (3) - 0,1,2
Range (m,n) -- generate an equal difference sequence of [m,n]. The difference is 1, for example: range (2,8) - 2,3,4,5,6,7
Range (m,n,step) --- generate an equal difference sequence of [m,n]. The difference is step, for example: range (1,10,3) - 1,4,7
for x in range(-1, 9, 2): print(x) for x in range(11): print(x) for x in range(0, 18, 3): print(x)
3. range actual combat
Summation:
a. Define a variable to save the last sum, and the initial value is 0
b. Obtain each data to be summed through the for loop
c. Every time you get a data that needs to be summed, add one to the variable that saves the last sum
s = 0 for x in range(1, 101): s += x print(s) nums = [10, 29, 30, 40] for x in nums: s += x print(s) s = 0 for x in range(100): if x % 3 == 0 or x % 5 == 0: s += x print(s)
Statistics
a. Define variables and save the last number. The initial value is 0
count=0
b. Use the for loop to get all statistical objects
c. Get a statistical object and add 1 to the original number of variables
count = 0 for x in range(1, 100): if x % 3 == 0 or x % 4 == 0: count += 1 print(count)