Day5 ape learning progress punch in

Daily summary

review

This week's mind map

[the external chain picture transfer fails. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (IMG eoqdofz3-1645362548051) (C: \ users \ ASUS \ pictures \ data analysis process \ unnamed file (11)] png)

Homework concern
# High order question: enter any positive integer and find out how many digits it is--- It's best to think of it as a loop while true and divide 10 to 0
print('------------')
num=0
count=0
while True:
     num//=10
     count+=1
     if num==0:
         break
print(count)
 # 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)
*Use cycle for-range To take out every number,Re judgment:If it can be divided by other numbers in a certain range,Then it must not be on the prime side else Put it in break The front is because break-else Caused by usage,Make sure you have break There is still content output after
num=35
for x in range(2,num):
    if num%x==0:
        print('Not prime')
        break
else:
        print('It's a prime')
# 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.
# print is indented in the first for, which means that the elements in the first group will be printed once, and the blank space will be printed once The first set of for is originally used to control rows
for i in range(1,10):
    for j in range(1,i+1):
        result=i*j
        if result<10:
            print(i,'x',j,'=', result, ' ',end=' ',sep='')
        print(i,'x',j,'=', result, end=' ',sep='')
    print('')

new

1.while statement

After the principle meets the conditions, it will continue to make cyclic judgment until it meets the non-conforming conditions

while condition:
    Circulatory body
 Other statements
# Use the while loop to print 'hello world!' five times
a = 0
while a < 5:
    print('hello world!')
    a += 1
    
while and for Comparison of cycles 
  
# Enter an integer and print all even numbers between 0 and X
x = int(input('integer:'))
for i in range(0, x + 1, 2):
    print(i)

*Method to control the loop: the condition needs to be True at the beginning, False after n cycles, and end the loop. First define a variable with 0 (0 times), write the condition < several times, add 1 to it each time, and finally print (put the position into the loop body)
*Execution process: first judge the condition as
*Special circumstances: 1 If the condition is always True, it falls into an endless loop. To exit the loop, you can use the break function 2 If False at the beginning, it ends at the beginning

The rule of double circulation: when one layer of circulation takes out, the second layer of circulation will take out all

2. Circular keyword
  • Continue: end a loop (if you encounter continue when executing the loop body, when the loop ends, you can directly enter the next loop)

  • Break: when executing the loop body, if it encounters a break, it will not be recycled, and all results will be output only once And will not perform any other operations after break

    # Randomly generate a random number from 0 to 100. The player inputs the number. The input number is equal to the generated number. The game is over! If it is not equal, give a prompt of 'big' or 'small'
    from random import randint
    num=randint(0,100)
    while True:
        value=int(input('Please enter a(0-100)Random number of:'))
        if value > num:
             print('Big')
        elif value < num:
            print('Small')
        else:
            print('You guessed right!fantastic!')
            break
    
3. About keyword else:
  1. The existence of else will not affect the execution of the original loop
  2. If the loop ends because it encounters a break, it will not be executed else exists to test break (understand)
str1 = '81283472'
# Judge whether the string is a numeric string
for x in str1:
    if not '0' <= x <= '9':
        print(str1, 'Not a pure numeric string')
        break
else:
    print(str1, 'Is a pure numeric string')
4. Ternary operator
***grammar: Value 1 if expression else Value 2 
#There is a definite value
a=16
result=18 if a>17 else a
print(result)
# Example of expression with explicit value: if it is greater than 10, let a add 1, otherwise let a-1
a=18
result=a+1 if a<10 else a-1
print(result)

task

Basic questions

First week assignment

1, Multiple choice questions

  1. Which of the following variable names is illegal? (C)

    A. abc

    B. Npc

    C. 1name

    D ab_cd

  2. Which of the following options is not a keyword? (C)

    A. and

    B. print

    C. True

    D. in

  3. Which of the following options corresponds to the correct code writing? (C)

    A.

    print('Python')
      print('Novice Village')
    

    B.

    print('Python') print('Novice Village')
    

    C.

    print('Python')
    print('Novice Village')
    

    D.

    print('Python''Novice Village')
    
  4. Which of the following options can print 50? (B)

    A.

    print('100 - 50')
    

    B.

    print(100 - 50)
    
  5. For quotation marks, what is the correct one to use in the following options? (D)

    A.

    print('hello)
    

    B.

    print("hello')
    

    C.

    print("hello")
    

    D.

    print("hello")
    

2, Programming problem

  1. Write code and print good study, day, day up on the console!

    print('good good study, day day up!')
    #result
    good good study, day day up!
    
    
  2. Write code and print 5 times on the console you see see, one day!

    for x in range(5):
        print('you see see, one day day!')
    #
    you see see, one day day!
    you see see, one day day!
    you see see, one day day!
    you see see, one day day!
    you see see, one day day!
    
  3. Write code and print numbers 11, 12, 13,... 21

    for x in range(11,22):
        print(x)
    #
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    
  4. Write code and print numbers 11, 13, 15, 17,... 99

    for x in range(11,100):
        print(x)
    #
    
    
  5. Write code and print numbers: 10, 9, 8, 7, 6, 5

    for x in range(10,4,-1):
        print(x)
    #
    10
    9
    8
    7
    6
    5
    
  6. Write code and calculate: the sum of 1 + 2 + 3 + 4 +... + 20

    sum=0
    for x in range(21):
        sum+=x
    print(sum)
    #
    210
    
  7. Write code to calculate the sum of all even numbers within 100

    result=0
    for x in range(2,100,2):
        result+=x
    print(result)
    #
    2450
    
  8. Write code to count the number of numbers with 3 in 100 ~ 200

    count=0
    for x in range(103,193):
        if x%10==3:
            count+=1
    print(count)
    #
    9
    
  9. Write code to calculate 2 * 3 * 4 * 5 ** 9 results

    result=1
    for x in range(2,10):
        result*=x
    print(result)
    #
    362880
    
  10. Enter a number. If the number entered is even, print even number; otherwise, print odd number

    value=int(input('Enter a number:'))
    if value%2:
        print('Odd number')
    else:
        print('even numbers')
    #
        Enter a number:7
     Odd number
    
  11. Count the number of numbers within 1000 that can be divided by 3 but cannot be divided by 5.

count=0
for x in range(3,1000,3):
    if x%5!=0:
        count+=1
print(count)
#
267

Higher order problem

  1. Judge how many primes there are between 101-200 and output all primes.

    count=0
    for x in range(101,199):
        if not x%2==0:
            count+=1
            print(x)
    print(count)
    #
    101
    103
    105
    107
    109
    111
    113
    115
    117
    119
    121
    123
    125
    127
    129
    131
    133
    135
    137
    139
    141
    143
    145
    147
    149
    151
    153
    155
    157
    159
    161
    163
    165
    167
    169
    171
    173
    175
    177
    179
    181
    183
    185
    187
    189
    191
    193
    195
    197
    49
    
  2. Find the cumulative value of integers 1 ~ 100, but it is required to skip all numbers with 3 bits.

    sum=0
    for x in range(1,101):
        if not x%10==3:
          sum+=x
    print(sum)
    #
    4570
    
  3. Write a program to calculate the factorial n of n! Results

    n=10
    for x in range(1,11):
        n*=x
        n+=n
    print(n)
    #
    37158912000
    
  4. Ask for 1 + 2+ 3!+…+ 20! And

    sum=1
    result=0
    for x in range(1,21):
         sum*=x
         result+=sum
    print(result)
    #
    2561327494111820313
    

A number with 3 bits.

sum=0
for x in range(1,101):
    if not x%10==3:
      sum+=x
print(sum)
#
4570
  1. Write a program to calculate the factorial n of n! Results

    n=10
    for x in range(1,11):
        n*=x
        n+=n
    print(n)
    #
    37158912000
    
  2. Ask for 1 + 2+ 3!+…+ 20! And

    sum=1
    result=0
    for x in range(1,21):
         sum*=x
         result+=sum
    print(result)
    #
    2561327494111820313
    

Keywords: Python

Added by gravedig2 on Sun, 20 Feb 2022 17:33:04 +0200