Python basic exercises

Python basic exercises

1, Multiple choice questions

  1. The following is not a Python feature (C)

    A. Easy to learn

    B. Open source free

    C. It belongs to low-level language

    D. High portability

  2. Python script files have a (B) extension

    A. .python

    B. .py

    C. .pt

    D. pg

  3. When you need to use special characters in a string, python uses (B).

    A. \

    B. /

    C. #

    D. %

  4. The following (A) is not A valid variable name.

    A. _demo

    B. banana

    C. Number

    D. My-score

  5. The power operator is (B).

    A. *

    B. **

    C. %

    D. //

  6. The operator with the highest priority is (B).

    A. /

    B. //

    C. *

    D. ()

  7. The running result of the following program is (B).

    list1 = [1, 2]
    temp = list1[0]
    list1[0] = list1[1]
    list1[1] = temp
    print(list1)
    

    A. [1, 2]

    B. [2, 1]

    C. [2. 2]

    D. [1, 1]

  8. Which of the following statements is illegal in Python? (B)

    A. x = y = z = 1

    B. x = (y = z + 1)

    C. x, y = y, x

    D. x += y x=x+y

  9. With regard to Python memory management, the following statement is wrong (B).

    A. Variables do not have to be declared in advance

    B. Variables can be used directly without creating and assigning values first

    C. Variables do not need to specify a type

    D. You can use del to release resources

  10. The following statement that cannot create a dictionary is (C).

    A. dict1 = {}

    B. dict2 = {3: 5}

    C. dict3 = dict([2, 5], [3, 4])

    D. dict4 = dict(([2, 5], [3, 4]))

2, Programming problem

1. Basic questions

  1. Copy data from one list to another.

    list1 = [29, 89, 89, 20, 90]
    new_list1 = list1.copy()
    print(new_list1)
    
  2. Use the nesting of conditional operators to complete this problem: students with academic achievement > = 90 points are represented by A, those with 60-89 points are represented by B, and those with less than 60 points are represented by C.

    x = int(input('Please enter your academic record:'))
    if x >= 90:
        print('A')
    elif 60 <= x <= 89:
        print('B')
    else:
        print('C')
    
  3. Enter a line of characters and count the number of English letters, spaces, numbers and other characters.

    str1 = input('Enter a line of characters:')
    n = m = t = y = a = 0
    for x in str1:
        if '0' <= x <= '9':
            n += 1
        elif 'a' <= x <= 'z' or 'A' <= x <= 'Z':
            m += 1
        elif '\u4e00' <= x <= '\u9fa5':
            t += 1
        elif x == ' ':
            y += 1
        else:
            a += 1
    print('Among them, Chinese%d English letters%d Spaces%d Number, number%d And other characters%d individual' % (t, m, y, n, a))
    
  4. Print the 5 characters in reverse order.

    str2 = '12345'
    new_str2 = ''.join(str2[-1::-1])
    print(new_str2)
    
  5. Give a positive integer with no more than 5 digits. Requirements: first, find how many digits it is, and second, print out the numbers in reverse order.

    x =int(input('Give a positive integer of no more than 5 bits:'))
    y = str(x)
    n = 0
    for m in y :
        n+=1
    new_y = ''.join(y[-1::-1])
    print('It is%d digit'%n,'Reverse order printing',new_y)
    
  6. A 5-digit number to judge whether it is a palindrome number. That is, 12321 is the palindrome number, the number of bits is the same as 10000 bits, and the number of tens is the same as thousands.

    x = input('Give me a five digit number:')
    if x[0] == x[-1] and x[1] == x[-2]:
        print('Is the palindrome number')
    else:
        print('Not palindromes')
    
  7. Comma separated list.

    [1,2,3,4,5] -> 1,2,3,4,5

    list2 = [1, 2, 3, 4, 5]
    str3 = ','.join(str(x) for x in list2)
    print(str3)
    
  8. Find the sum of the main diagonal elements of a 3 * 3 matrix.

    '''
    For example, the list is as follows:
    [
      [1, 2, 3],
      [4, 5, 6],
      [7, 8, 9]
    ]
    Request: 1+ 5+ 9 Sum of
    '''
    list3 =[
      [1, 2, 3],
      [4, 5, 6],
      [7, 8, 9]
    ]
    print('This 3*3 The sum of the main diagonal elements of the matrix is', list3[0][0]+list3[1][1]+list3[2][2])
    
  9. There is an ordered array. Now enter a number and ask to insert it into the array according to the original law.

    For example, the original list is [10, 20, 34, 56, 90]. After entering 50, the list is: [10, 20, 34, 50, 56, 90]

    list4 = [10, 20, 34, 56, 90]
    x = int(input('Enter a number'))
    list4.append(x)
    list4.sort()
    print(list4)
    
  10. Two matrices with 3 rows and 3 columns can add the data at their corresponding positions and return a new matrix:

    '''
    X = [[12,7,3],
        [4 ,5,6],
        [7 ,8,9]]
    
    Y = [[5,8,1],
        [6,7,3],
        [4,5,9]]
    
    X + Y Is:
    [
      [17, 15, 4],
      [10, 12, 9],
      [11, 13, 18]
    ]
    '''
    X = [[12, 7, 3],
         [4, 5, 6],
         [7, 8, 9]]
    Y = [[5, 8, 1],
         [6, 7, 3],
         [4, 5, 9]]
    L=[]
    a = 0
    for x in X:
        N = [x[0] + Y[a][0], x[1] + Y[a][1], x[2] + Y[a][2]]
        a += 1
        L.append(N)
    print(L)
    
  11. Find the square of the input number. If the square operation is less than 50, exit.

    while True:
        num = int(input('Enter number:'))
        print('The calculation result is%d' % num ** 2)
        if num ** 2 < 50:
            break
    
  12. The values of the two variables are interchanged.

    x = 9
    y = 0
    x, y = y, x
    print(x, y)
    

2. Advanced questions

  1. There are four numbers: 1, 2, 3 and 4. How many three digits that are different from each other and have no duplicate numbers? How much is each?

    list5 = [1, 2, 3, 4]
    n = 0
    for index in range(len(list5)):
        list51 = list5.copy()
        x = str(list51.pop(index))
        for index2 in range(len(list51)):
            list52 = list51.copy()
            y = str(list52.pop(index2))
            for index3 in range(len(list52)):
                print(x + y + str(list52[index3]), end=' ')
                n += 1
    print('Energy composition', n, 'Three digits that are different from each other and have no duplicate digits')
    
  2. The bonus paid by the enterprise is based on the profit commission. When the profit (I) is less than or equal to 100000 yuan, the bonus can be increased by 10%; When the profit is higher than 100000 yuan and lower than 200000 yuan, the part lower than 100000 yuan will be deducted by 10%, and the part higher than 100000 yuan will be deducted by 7.5%; Between 200000 and 400000 yuan, the part higher than 200000 yuan can be deducted by 5%; 3% commission can be given for the part higher than 400000 yuan between 400000 and 600000 yuan; When it is between 600000 and 1 million yuan, the part higher than 600000 yuan can be deducted by 1.5%. When it is higher than 1 million yuan, the part higher than 1 million yuan can be deducted by 1%. Enter the profit I of the current month from the keyboard to calculate the total amount of bonus to be paid?

    x = float(input('Enter current month profit I:'))
    if x <= 10e4:
        print('Bonus can be withdrawn:', x * 0.1, 'element')
    elif x <= 20e4:
        print('Bonus can be withdrawn:', 10e4 * 0.1 + (x - 10e4) * 0.075)
    elif x <= 40e4:
        print('Bonus can be withdrawn:', 10e4 * 0.1 + 10e4 * 0.075 + (x - 20e4) * 0.05)
    elif x <= 60e4:
        print('Bonus can be withdrawn:', 10e4 * 0.1 + 10e4 * 0.075 + 20e4 * 0.05 + (x - 40e4) * 0.03)
    elif x <= 100e4:
        print('Bonus can be withdrawn:', 10e4 * 0.1 + 10e4 * 0.075 + 20e4 * 0.05 + 40e4 * 0.03+(x-60e4)*0.015)
    else:
        print('Bonus can be withdrawn:', 10e4 * 0.1 + 10e4 * 0.075 + 20e4 * 0.05 + 40e4 * 0.03 + 60e4 * 0.015+(x-100e4)*0.01)
    
  3. An integer is a complete square number after adding 100, and a complete square number after adding 168. What is the number?

    x = 0
    while True:
        x1 = x + 100
        x2 = x + 100 + 168
        new_x1 = int(x1 ** 0.5)
        new_x2 = int(x2 ** 0.5)
        if new_x1 ** 2 == x1 and new_x2 ** 2 == x2:
            print(x)
            break
        x += 1
    
  4. Output 9 * 9 multiplication formula table. (alignment required)

    for x in range(10):
        for y in range(1, x + 1):
            if x * y//10==0:
                print('%d*%d=%d ' % (y, x, x * y), end=' ')
            else:
                print('%d*%d=%d' % (y, x, x * y), end=' ')
        print()
    
  5. Classical question: a pair of rabbits give birth to a pair of rabbits every month from the third month after birth. The little rabbit grows to another pair of rabbits every month after the third month. If the rabbits don't die, what is the total number of rabbits every month?

    a = 1
    b = 1
    n = int(input(""))
    if n == 1 or n == 2:
        print('The first',n,'Months,There are 2 rabbits')
    else:
        for x in range(n - 3):
            a, b = b, a + b
        print('The first',n,'Months,There are rabbits',(a + b)*2,'only')
    
  6. If a number is exactly equal to the sum of its factors, it is called "perfect". For example, 6 = 1 + 2 + 3 Program to find all completions within 1000.

  7. A ball falls freely from a height of 100 meters and jumps back to half of the original height after each landing; How many meters will it pass on the 10th landing? How high is the 10th rebound?

    h = 100
    s = 0
    n=10
    if n==1:
        print('Common course',h,'rice,First rebound',h/2,'Meter high')
    else:
        for x in range(n-2):
            h1 = h / 2
            h = h1
            s += h * 2
        print(s+100)
        print(h)
    
  8. Print the following pattern (diamond):

    '''
       *
      ***
     *****
    *******
     *****
      ***
       *
    '''
    a = 1
    b = 2
    while a>0:
        print(int((7 - a) / 2) * ' ' + '*' * a)
        a += b
        if a == 7:
            b = -b
    
  9. There is a fractional sequence: 2 / 1, 3 / 2, 5 / 3, 8 / 5, 13 / 8, 21 / 13... Find the sum of the first 20 terms of this sequence.

    a = 1
    b = 1
    c = 0
    for x in range(20):
        a, b = b, a + b
        c += ((a + b) / b)
    print(c)
    
  10. Print out Yang Hui triangle (10 lines are required, as shown in the figure below).

    '''
    1 
    1 1 
    1 2 1 
    1 3 3 1 
    1 4 6 4 1 
    1 5 10 10 5 1 
    1 6 15 20 15 6 1 
    1 7 21 35 35 21 7 1 
    1 8 28 56 70 56 28 8 1 
    1 9 36 84 126 126 84 36 9 1
    '''
    
    
    
    
    
    

Keywords: Python

Added by MilesWilson on Fri, 14 Jan 2022 06:11:01 +0200