Python basic exercises
1, Multiple choice questions
-
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
-
Python script files have a (B) extension
A. .python
B. .py
C. .pt
D. pg
-
When you need to use special characters in A string, python uses (A).
A. \
B. /
C. #
D. %
-
The following (D) is not a valid variable name.
A. _demo
B. banana
C. Number
D. My-score
-
The power operator is (B).
A. *
B. **
C. %
D. //
-
The operator with the highest priority is (D).
A. /
B. //
C. *
D. ()
-
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]
-
Which of the following statements is illegal in Python? (B, D)
A. x = y = z = 1
B. x = (y = z + 1)
C. x, y = y, x
D. x += y x=x+y
-
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
-
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
-
Copy data from one list to another.
list1 = [1, 2, 3] list2 = list1.copy() print(list2)
-
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.
score = 60 if 100 >= score >= 90: print('A') elif score >= 60: print('B') else: print('C')
-
Enter a line of characters and count the number of English letters, spaces, numbers and other characters.
str1 = 'a f o4-[mp09i' num_count = 0 eng_count = 0 space_count = 0 other_str_count = 0 for s in str1: if '0' <= s <= '9': num_count += 1 elif 'a' <= s <= 'z' or 'A' <= s <= 'Z': eng_count += 1 elif s == ' ': space_count += 1 else: other_str_count += 1 print(f'English letters have{eng_count}Yes, there are spaces{space_count}Yes, there are{num_count}Characters, other characters{other_str_count}individual')
-
Print the 5 characters in reverse order.
str2 = 'wqwer' print(str2[::-1])
-
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.
num = 2354 str_num = str(num) print(len(str_num), str_num[::-1])
-
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.
num2 = 34507 str_num2 = str(num2) if str_num2[0] == str_num2[4] and str_num2[1] == str_num2[3]: print('Is the palindrome number') else: print('Not palindromes')
-
Comma separated list.
[1,2,3,4,5] -> 1,2,3,4,5
list3 = [1, 2, 3, 4, 5] for item in list3[:-1]: print(item, end=',') print(list3[-1])
-
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
list4 = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] print(f'And are:{list4[0][0] + list4[1][1] + list4[2][2]}')
-
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]
list5 = [10, 20, 34, 56, 90] num3 = 50 # Method 1 list5.append(num3) if list5[0] < list5[-1]: list5.sort() else: list5.sort(reverse=True) print(list5) # Method 2 if list5[0] < list5[-1]: for index, item in enumerate(list5): if item >= num3: list5.insert(index, num3) break else: list5.append(num3) else: for index, item in enumerate(list5): if item <= num3: list5.insert(index, num3) break else: list5.append(num3) print(list5)
-
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] ]
list_1 = [ [12, 7, 3], [4, 5, 6], [7, 8, 9] ] list_2 = [ [5, 8, 1], [6, 7, 3], [4, 5, 9] ] list_sum = [] for i in range(3): list_value = [] for j in range(3): list_value.append(list_1[i][j] + list_2[i][j]) list_sum.append(list_value) print(list_sum)
-
Find the square of the input number. If the square operation is less than 50, exit.
num4 = 100 while (num4 ** 2) > 50: num4 = int(input('Please enter a number:')) print('Square is:', num4 ** 2)
-
The values of the two variables are interchanged.
x = 'a' y = 3 x, y = y, x print(x, y)
2. Advanced questions
-
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?
count = 0 for x in range(1, 5): for y in range(1, 5): if x == y: continue for z in range(1, 5): if z == x or z == y: continue print(x * 100 + y * 10 + z, end=',') count += 1 print(f'share{count}Three digits.')
-
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?
money = int(input('Please enter profit (unit)/10000 yuan):')) money2 = 0 if 0 <= money <= 10: money2 = money * 0.1 elif money <= 20: money2 = 10 * 0.1 + (money - 10) * 0.075 elif money <= 40: money2 = 10 * 0.1 + 10 * 0.075 + (money - 20) * 0.05 elif money <= 60: money2 = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (money - 40) * 0.03 elif money <= 100: money2 = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + (money - 60) * 0.015 else: money2 = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + 40 * 0.015 + (money - 100) * 0.01 print('The total amount of bonus is:', money2)
-
An integer is a complete square number after adding 100, and a complete square number after adding 168. What is the number?
i = 0 while True: i += 1 if (i + 100) ** 0.5 == int((i + 100) ** 0.5) and (i + 268) ** 0.5 == int((i + 268) ** 0.5): print(i) break
-
Output 9 * 9 multiplication formula table. (alignment required)
for i in range(1, 10): for j in range(1, i + 1): print(f'\t{i}x{j}={i * j}', end=' ') else: print()
-
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?
for n in range(1, 11): if n == 1 or n == 2: print('The first', n, 'The number of rabbits in months is 1 pair', sep='') else: value_1 = 1 value_2 = 1 for i in range(n - 2): value_1, value_2 = value_2, value_1 + value_2 print('The first', n, 'The number of rabbits in months is', value_2, 'yes', sep='')
-
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.
for i in range(2, 1001): sum_i = 0 for j in range(1, i // 2 + 1): if i % j == 0: sum_i += j if sum_i == i: print(i, end=' ') print()
-
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_value = 100 count = 0 s = 0 while count < 10: s += h_value + h_value / 2 count += 1 h_value /= 2 print(f'Common course{s + h_value}Meters, tenth rebound{h_value / 2}rice')
-
Print the following pattern (diamond):
* *** ***** ******* ***** *** *
n = 4 for i in range(2 * n - 1): if i < n: print((n - (i + 1)) * ' ', (2 * i + 1) * '*', sep='') else: print((i + 1 - n) * ' ', ((2 * n - 1 - 2 * (i - n + 1)) * '*'), sep='')
-
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.
num_1 = num_2 = 1 n = 20 sum_end = 0 for i in range(n): num_sum = num_1 + num_2 sum_end += num_sum / num_2 num_1 = num_2 num_2 = num_sum print('And are:', sum_end)
-
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
# Create a list of Yang Hui triangle list_end = [] for i in range(10): list_value = [] if i == 0: list_value.append(1) list_end.append(list_value) continue elif i == 1: list_value.append(1) list_value.append(1) list_end.append(list_value) continue for j in range(i + 1): if j == 0 or j == i: list_value.append(1) continue list_value.append(list_end[i-1][j-1] + list_end[i-1][j]) list_end.append(list_value) # Output Yang Hui triangle for item in list_end: for i in item: print(i, end='\t') print()