Today's summary
1, Process control

Sequential structure: the code is executed from top to bottom, and the statement is executed only once a day. (default)
print('hello') print('word')

Branch structure: choose to execute or not execute part of the code according to the conditions (use if)
age = 10 if age >= 18: print('adult') else: print('under age')

Loop structure: make the code execute repeatedly (for, while)
for i in range(5): print(i)
2, if branch structure
1. If single branch structure  if
Problem solved: perform an operation when the conditions are met, and do not perform it when the addition is not met
Syntax:
if conditional statement:
Code snippet (code that will be executed only if conditions are met)
if  keyword; Fixed writing
Conditional statement  can be any expression with results, including: specific data, operation expression (except assignment operation), and the expression that has been assigned
Variables, function call expressions, etc Fixed writing
Code segment  structurally, it is one or more statements (at least one) that maintain an indent with if; Logically, code snippets are conditional
When the code is executed
age = 20 if age >= 18: print('adult')
2. If double branch structure  if... Then... Otherwise
Syntax:
if conditional statement:
Code snippet 1 (code to be executed if conditions are met)
else:
Code segment 2 (code to be executed when conditions are not met)
Code segment 3 (execute whether the condition is true or not)
#Print 'leap year' if the specified year is a leap year year = 2001 if year % 4 == 0 and year % 100 != 0 or year % 400 == 0: print('leap year') else: print('Non leap year')
3. If multi branch structure  if... If... If... If... If... Otherwise
Syntax:
if condition 1:
Code snippet 1
elif condition 2:
Code snippet 2
elif condition 3:
Code snippet 3
...
else:
Code snippet N
Note: elif can be any number, else can have or not
score = 83 if score >= 90: print('excellent') elif score >= 80: print('good') elif score >= 70: print('secondary') elif score >= 60: print('pass') else: print('fail,')
3, for loop
1. for loop
1) Syntax:
for variable in sequence:
Loop body (code that needs to be executed repeatedly)
2) Description:
for  keyword; Fixed writing
Variable  valid variable name (can be defined or undefined)
in  keyword; Fixed writing
Sequence  data of container data type. Container data types include string, list, dictionary, set, tuple, iterator, generator, range, etc Fixed writing
Loop body  one or more statements that maintain an indentation with for. The loop body is the code that needs to be executed repeatedly
3) Execution process
Let variables take values from the sequence, one by one, until they are finished; Take a value and execute the loop body once
The number of cycles of the for loop is related to the number of elements in the sequence
2. range function  creates an arithmetic sequence (integer)
1) range(N)  produces an equal difference sequence of [0,N), and the difference is 1
2) range(M,N)  produces an equal difference sequence of [M,N), and the difference is 1
3) range(M,N,step)  generates an equal difference sequence of [0,N), and the difference is step
3. Application scenario
1) Cumulative summation
Step 1: define the variable and save the result. The initial value of the variable is generally zero (sum) or 1 (product)
Step 2: obtain cumulative data one by one with a loop
Step 3: in the loop body, merge each data obtained into the variable corresponding to the result
#Find 1 + 2 + 3 ++ Sum of 100 result = 0 for i in range(1,101): result += i print(result)
2) Number of Statistics
Step 1: define the last number of variables to save. The default builtin variable is 0
Step 2: use the loop to obtain the statistical object
Step 3: add 1 to the number of data meeting the statistical conditions
#Count the number of odd numbers in 11000 count = 0 for i in range(1000): if i % 2 == 1: count += 1 print(count)
task
Basic questions

Print pass or fail according to the range of grades entered.
score = float(input('Please enter a score:')) if score >= 60: print('pass') else: print('fail,')

Print adults or minors according to the entered age range. If the age is not within the normal range (0 ~ 150), it is not a person!.
age = int(input('Please enter age:')) if age <= 18: print('under age') elif age <= 150: print('adult') else: print('This is not a man!')

Enter two integers a and b. if the result of ab is odd, the result will be output. Otherwise, the result of output prompt information ab is not odd
a = int(input('Please enter a Value of:')) b = int(input('Please enter b Value of:')) if (a  b) % 2 == 1: print(a  b) else: print('ab The result is not odd')

Use the for loop to output multiples of all 3 in 0 ~ 100.
for i in range(0,101,3): print(i)

Use the for loop to output the number of single digits or tens within 100 ~ 200 that can be divided by 3.
for i in range(100,201): if (i % 10 % 3) == 0 or (i // 10 % 10 % 3) == 0: print(i)

Use the for loop to count the number of 10 digits in 100 ~ 200 that are 5
count = 0 for i in range(100,201): if i // 10 % 10 == 5: count += 1 print(count)

Use the for loop to print all numbers in 50 ~ 150 that can be divided by 3 but cannot be divided by 5
for i in range(50,151): if i % 3 == 0 and i % 5 != 0: print(i)

Use the for loop to calculate the sum of all numbers in 50 ~ 150 that can be divided by 3 but cannot be divided by 5
sum = 0 for i in range(50,151): if i % 3 == 0 and i % 5 != 0: sum += i print(sum)

Count the number of digits within 100 that are 2 and can be divided by 3.
count = 0 for i in range(3,100,3): if i % 10 == 2: count += 1 print(count)
Advanced questions

Enter any positive integer. How many digits is it?
Note: you can't use strings here. You can only use loops
count = 0 num = input('Please enter an integer:') for i in num: if i != '': count += 1 print(count)

Print out all the numbers of daffodils. The socalled daffodils number refers to a three digit number, and the square sum of each digit is equal to the number itself. For example: 153 yes
The number of fairy flowers is 1 ³ + five ³ + three ³ Equal to 153.
for num in range(100,1000): if ((num // 100)**3 + (num // 10 % 10)**3 + (num % 10)**3) == num: print(num)

Judge whether the specified number is a prime number (prime number is a prime number, that is, a number that cannot be divided by other numbers except 1 and itself)
num = int(input('Please enter a number:')) for i in range(2, num): if num % i == 0: print('Not prime') break else: print('It's a prime') break

Output 9 * 9 formula. Procedure analysis: Considering branches and columns, there are 9 rows and 9 columns in total, i control row and j control column.
for i in range(1, 10): for j in range(1, i+1): print(j,'x',i,'=',i*j,end=' ') print()

This is the classic question of "100 horses and 100 loads". There are 100 horses, carrying 100 loads of goods, 3 loads of horses, 2 loads of medium horses and 1 load of two ponies. How many are the big, medium and small horses? (exhaustive method can be used directly)
big_horse = 0 medium_horse = 0 small_horse = 0 for big_horse in range(34): for medium_horse in range(51): for small_horse in range(200): if big_horse * 3 + medium_horse * 2 + small_horse * 0.5 == 100 and big_horse + medium_horse + small_horse == 100 : print(big_horse,medium_horse,small_horse)