day4 - daily summary and homework

***

Process control

1) Sequential structure: code is executed from top to bottom, and each statement is executed in sequence (default)
2) Branch structure: choose to execute or not execute part of the code according to the conditions (use if)
3) Loop structure: make the code execute repeatedly (for, while)

Branching structure

age = 10
if age >= 18:
    print('adult')
else:
    print('under age')

if branch structure

if single branch structure

Syntax:
if conditional statement:
Code snippet

explain:
if - keyword; Fixed writing
Conditional statement - can make any expression with results, including: specific data, operation expression (except assignment operation), variables that have been paid, function call expression, etc
: - Fixed writing
Code snippet - structurally, it is one or more statements (at least one) that maintain an indent with if; Logically: code is the code that will be executed when the condition is established

if (): # Null = None=0=False
    print('a')

If it is a leap year, print the leap year

a = float(input('Please enter the year'))
if a % 4 == 0 and a % 100 != 0 or a % 400 == 0:
    print('leap year')
else:
    print('Missing intercalation')

If the variable is saved as an integer, print 'Integer'

num = 34
if type(num) == int:
    print('integer')

if double branch structure

Syntax:
if conditional statement:
Code snippet 1 (code executed when conditions are met)
else:
Code snippet 2 (code executed without conditions)
Code snippet 3 (code executed regardless of whether the condition is satisfied or not)

Judge the parity of a number

a = float(input('please enter a number'))
if a % 2 == 0: #if a % 2 :
    print('even numbers')
else:
    print('Odd number')

if multi branch structure

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 make any number of; else can have or not
a = float(input('please enter a number'))
if a > 90:
    print('2000')
elif 85 < a <= 90 : #You can not write < = 90, because the case of a > 90 has been excluded in the previous step, so it is already in
    print('1000')
elif 75 < a <= 85 :
    print('500')
elif 60 <= a <= 75:
    print('Little safflower')
else:
    print('What reward do you want for failing?')

for loop

for loop

Syntax:

for variable in sequence:
Loop body (code that needs to be executed repeatedly)

explain:

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

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

for x in 'abc':
    print('qwer')

Execution process:

First time: x = 'a' - > Print ('qwer ')
Second time: x = 'b' - > Print ('qwer ')
Third time: x = 'c' - > Print ('qwer ')

range function

range(N) - produces an equal difference sequence of [0,N), and the difference is 1
range(3) -> 0,1,2
range(5) -> 0,1,2,3,4

range(M,N) - produces an equal difference sequence of [M,N), and the difference is 1
range(10,21) -> 10,11,12,13,14,15,16,17,18,19,20

range(M,N,STEP) - generate an equal difference sequence of [M,N), and the difference is step
range(1,10,2) -> 1,3,5,7,9
range(20,2,-3) -> 20,17,14,11,8,5

Print all even numbers within 1000

for x in range(0,1001,2):
    print(x)

Two basic application scenarios of for loop

Cumulative summation

Exercise 1 Write code to find the sum of 1 + 2 + 3 + 4... + 100
# Step 1: define the variable and save the result. The initial value of the variable is generally 0 (summation) or 1 (quadrature)
a = 0
# Step 2: obtain cumulative data one by one with a loop
for b in range(1,101):
    # Step 3: the recycle volume combines each data obtained into the variable corresponding to the result
    a += b
print(a)
Exercise 2 Find the factorial of 10: 123... * 10
c = 1
for d in range(1,11):
    c *= d
print(c)
Exercise 3 Find the sum of all even numbers in 100 to 200 that can be divided by 3
e = 0
for f in range(102,201,6):
    e += f
print(e)

Number of Statistics

Exercise 1: count the number of odd numbers within 1000

Step 1: define the last number of variables to save. The default value of variables is 0
count = 0
# Step 2: use the loop to obtain the statistical object
for x in range(1000):
    # Step 3: add one to the number of data that meet the statistical conditions
    if x % 2 == 0:
        count += 1
print(count)

Or directly

for x in range(1,1000,2):
    count += 1
print(count)

Exercise 2: count the number of numbers within 1000 that can be divided by 3 but cannot be divided by 7

s = 0
for i in range(1000):
    if i % 3 == 0 and  i % 7 != 0:
        s += 1
print(s)

Homework after class

Basic questions

  1. Print pass or fail according to the range of grades entered.

    a = float(input('Please enter'))
    if a >= 60:
        print('pass')
    else:
        print('fail,')
    
  2. 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!.

    a = float(input('Please enter'))
    if 150 > a >= 18:
        print('adult')
    elif a < 18 :
        print('under age')
    else:
        print('This is not a person')
    
  3. Enter two integers a and b. if the result of a-b is odd, the result will be output. Otherwise, the result of output prompt information a-b is not odd

    a = float(input('Please enter a'))
    b = float(input('Please enter b'))
    c = a - b
    if c % 2 != 0:
        print(c)
    else:
        print('a-b The result is not odd')
    
  4. Use the for loop to output multiples of all 3 in 0 ~ 100.

    for x in range(0,101,3):
        print(x)
    
  5. Use the for loop to output the number of single digits or tens within 100 ~ 200 that can be divided by 3.

    for x in range(100,201):
        if (x % 10) % 3 == 0:
            print(x)
        elif ((x % 100)// 10) % 3 == 0:
            print(x)
    
  6. Use the for loop to count the number of 10 digits in 100 ~ 200 that are 5

    a = 0
    for x in range(100,201):
        if ((x % 100) // 10) / 5 == 1:
            a += 1
            print(x)
    print(a)
    
  7. Use the for loop to print all numbers in 50 ~ 150 that can be divided by 3 but cannot be divided by 5

    for x in range(50,151):
        if x % 3 == 0 and x % 5 != 0:
            print(x)
    
  8. 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

    a = 0
    for x in range(50,151):
        if x % 3 == 0 and x % 5 != 0:
            a += x
    print(a)
    
  9. Count the number of digits within 100 that are 2 and can be divided by 3.

    a = 0
    for x in range(2,100,10):
        if x % 3 == 0:
            a += 1
    print(a)
    

Advanced questions

  1. Enter any positive integer. How many digits is it?

    Note: you can't use strings here. You can only use loops

    b = 1
    a = int(input('Please enter a'))
    while a // 10 > 0:
     a //= 10
     b += 1
    print(b)
    
    
    perhaps
    a = int(input('Please enter a')); b = 0
    for x in range(a):
        a //= 10
        b += 1
        if a == 0:
            print(b)
            break
    
  2. Print out all the numbers of daffodils. The so-called 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 x in range(100,1000):
        if (x // 100) ** 3 + ((x % 100) // 10) ** 3 + (x % 10) ** 3 == x:
            print(x)
    
  3. 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)

    a = int(input('Please enter a'))
    for x in range(2,a):
        if a % x == 0:
            print('Not prime')
            break
        else:
            print('Is a prime number')
            break
    
  4. 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.

    # i = 0 ; j = 0
    for i in range(9):
        print()
        # i += 1
        for j in range(9):
            # j += 1
            s = i * j
            if j <= i:
                print(i, '*', j, '=', s,end = '      ')
                
    After simplification
    for i in range(1,10):
        print()
        for j in range(1,i+1):
            s = i * j
            print(j, '*', i, '=', s,end = '      ')
    
  5. 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)

    # a = 0 ; b = 0 ; c = 0
    for a in range(34):
        # a += 1
        for b in range(50):
            # b += 1
            for c in range(200):
                # c += 2
                if a * 3 + b * 2 + c * 0.5 == 100 and a + b + c == 100:
                    print(a,b,c)
    
    

Keywords: Python Back-end

Added by libinaz on Thu, 17 Feb 2022 16:36:48 +0200