Today's talk includes if multi branch structure, ternary operator, for loop, while loop, continue and break.
if multi branch structure
1. if multi branch structure - do different things according to different conditions
1) if - elif - else - there are different conditions: when one of the conditions is true, the other conditions will not be true
if Condition 1: Code snippet 1 elif Condition 2: Code snippet 2 elif Condition 3: Code snippet 3 ... else: Code snippet N
Execution features: if the previous conditions are true, the latter conditions will not be judged (the latter conditions are judged when the previous conditions are not true)
2) When using multiple if - conditions alone, there are differences: one of the conditions holds, and other conditions may also hold
if Condition 1: Code snippet 1 if Condition 2: Code snippet 2 if Condition 3: Code snippet 3 ...
Exercise 1: print the adult or minor according to the entered age range. If the age is not within the normal range (0 ~ 150), it is not a person!.
age = 35 if age < 0 or age > 150: print('This is not a person!') elif age < 18: print('under age') else: print('adult')
Exercise 2: print odd, even, and multiples of 4 based on the value of the number
num = 25 if num % 2: print('Odd number') else: if num % 4 == 0: print('4 Multiple of', end=' ') print('even numbers')
1. Ternary operation
1) Three purpose operators in C, java and js:?:
Expression 1? Value 1: value 2 - first judge whether the result of expression 1 is true. If yes, the result of the whole operation expression is value 1, otherwise it is value 2.
int a = 100; int x; x = a > 100 ? 10:20;
2) Ternary operator in Python
Value 1 if expression 1 else value 2 - first judge whether the result of expression 1 is True. If so, the result of the whole operation expression is value 1, otherwise it is value 2.
a = 100 x = 10 if a > 100 else 20 print(x)
1. Loop structure - let the code execute repeatedly
for _ in range(3): print('hello world!')
for variable in Sequence: Circulatory body
for, in - keywords; Fixed writing
Variable - variable name (it can be the variable name of a defined variable or the variable name of an undefined variable).
Sequence - the data of container data type is a sequence, such as string, list, dictionary, tuple, set, iterator, generator, range, etc.
: - Fixed writing
Loop body - one or more statements (at least one) that are structurally indented with for; Logically, it is the code that needs to be executed repeatedly.
3) Execution process
Let the variables take values from the sequence one by one. Until they are finished, the loop body is executed once for each value.
The for loop is determined by the number of elements in the sequence
4) range function - a function that produces a sequence of numbers in an arithmetic sequence
range(N) - produce[0, N)Digital sequence with step size of 1 range(3) -> 0, 1, 2 range(5) -> 0, 1, 2, 3, 4 range(M, N) - produce[M, N)Digital sequence with step size of 1 range(1, 5)-> 1, 2, 3, 4 range(10, 21) -> 10, 11, 12,..., 20 range(M, N, step) - produce[M, N)Step size step A numeric sequence of specified values, range(1, 5, 2) -> 1, 3 range(10, 20, 3) -> 10, 13, 16, 19 range(20, 14, -1) -> 20, 19, 18, 17, 16, 15
Exercise: use the loop to calculate the sum sum sum routine of 1 + 2 + 3 +... + 100
sum1 = 0 # Save last and for x in range(1, 101): sum1 += x # sum1 = sum1 + x print(sum1)
Count the number of numbers that can be divided by 3 within 1000
ount = 0 # Save last number for x in range(1, 1000): if x % 3 == 0: count += 1 print(count)
Count the number of numbers within 1000 that can be divided by 3 but cannot be divided by 7
# Method 1: count = 0 for x in range(1, 1000): if x % 3 == 0 and x % 7 != 0: count += 1 print(count) # Method 2: count = 0 for x in range(3, 1000, 3): if x % 7 != 0: count += 1 print(count)
1)Syntax: while Conditional statement: Circulatory body 2)explain: while - Keywords, fixed writing Conditional statement - Any resulting expression(Cannot be assignment syntax) : - Fixed writing Circulatory body - Structurally and while One or more statements that maintain an indentation; Logically, it is the code that needs to be executed repeatedly 3)Execution process: First judge whether the conditional statement is True，If yes True After executing the loop body, judge whether the conditional statement is True, by True And execute the loop body, And so on, if the conditional statement is False，The cycle is over!
2. Selection of for loop and while
The for loop is used when the number of cycles is determined, and the while loop is used when the number of cycles is uncertain.
Exercise (uncertain number of cycles): prompt the user to enter the password until the password is entered correctly
pw = '123456' # Original password get_pw = None # Save the password you entered # Prompt for input when the input password is not equal to the original password while get_pw != pw: get_pw = input('Please input a password:')
continue and break
1. continue - end a cycle
When continue is encountered when executing the loop body, this loop ends and goes directly to the next loop.
for x in range(10): if x % 2: continue print(x) # 0，2，4，6，8
2. break - end the whole cycle
If a break is encountered when executing the loop body, the whole loop will end directly.
or x in range(1, 10): if x % 3 == 0: break print(x)
3. Coordination of while loop and break
Beginner's while loop while True: Code that needs to be executed repeatedly if Conditions for the end of the cycle: break