Python class exercises (6): after-school exercises for lists and tuples


Practice pre-knowledge

  1. Chapter One: Start the Python Journey
  2. Chapter Two: A collection of Python language foundations, operators and expressions, and program control structures
  3. Chapter Three: Detailed explanation of string operations in Python sequences
  4. Chapter Four: A collection of detailed explanations of Python sequence lists and tuple operations

1. List of exercises after class

(1) If you want to add all the elements in one list to another list, what method can be used to implement the list object? ()
A. append()
B. insert()
C. extend()
D. reversed()

(2) Qi Qi was watching the Chinese Poetry Conference recently. In the tenth game of the fifth season, the four contestants for the championship were "Peng Min, Zheng Kunjian, Han Yaxuan, and Jiang Yiling". She wrote 4 pieces of code to output the final champion "Peng Min" and runner-up "Han Yaxuan". May I ask which piece of code can achieve the effect she wants. ()

A:
mylist = ['彭敏', '郑坤健', '韩亚轩', '姜怡伶']
print(mylist[0:3])

B:
mylist = ['彭敏', '郑坤健', '韩亚轩', '姜怡伶']
print(mylist[::2])

C:
mylist = ['彭敏', '郑坤健', '韩亚轩', '姜怡伶']
print(mylist[1:3])

D:
mylist = ['彭敏', '郑坤健', '韩亚轩', '姜怡伶']
print(mylist[0, 3])

(3) In order to improve the readability of data, when creating a list, we usually ().
A. Put different types of data
B. If they are all numeric types, it is recommended to round them and put them into
C. Put the same type of data
D. Put the data into a tuple and then put it into the list

(4) In Python, "[]" means ().
A. Empty set
B. Empty dictionary
C. Empty tuple
D. Empty list

(5) Regarding the deletion list, which of the following statements is correct is ().
A. The syntax format is del listneme
B. Before deleting the list, the list name does not need to exist, because even if it does not exist, a new list will be created when
deleting C. Before deleting the list, you must ensure that the list name already exists, otherwise the system will Throwing an exception
D. The del statement is often used, because Python does not automatically delete the list.

(6) The range object can be converted into a list through the () function.
A. array()
B. []
C. list()
D. set()

(7) What are the ways to traverse the list, and what are the differences between the ways? ()
A. Use the for loop method
B. Use the for loop and enumerate() function
C. The for loop can only output the elements of the list, and the for loop and enumerate() can output the elements and the index values ​​of the elements
D. A, B, C are all right

(8) The running result of the following code is ().

untitle = ['python', 28, "人生苦短,我用Python", ["爬虫", "自动化运维", "Web开发"]]
print(untitle[2])

A. Life is too short
B. 28
C. Life is too short, I use Python
D. Comma

(9) The running result of the following code is ()

lipsticks_list = ["Dior", "Chanel", "Gucci", "Givenchy"]
len(lipsticks_list)
lipsticks_list.append("Lancome")
len(lipsticks_list)
print(lipsticks_list)

A. [‘Dior’, ‘Chanel’, ‘Gucci’, ‘Givenchy’, ‘Lancome’]
B. [‘Dior’, ‘Chanel’, ‘Gucci’, ‘Lancome’]
C. ‘Dior’, ‘Chanel’, ‘Gucci’, ‘Givenchy’, ‘Lancome’
D. ‘Dior’, ‘Chanel’, ‘Gucci’, ‘Givenchy’

(10) Xiao Ming wants to modify Monday's class schedule and change the language of the second period to English. Please help him to complete the code. ()

lesson_list = ["数学", "语文", "政治", "历史"]
print(lesson_list)
print(lesson_list)

A. Add del lesson_list("语文") between lines 2 and 3 of the code
B. Add insert lesson_list("语文") as ("英文") between lines 2 and 3 of the code
C. Add lesson_list[1] = "English" between lines 2 and 3 of the code
D. Add lesson_list[1] = "English "not" Chinese" between lines 2 and 3 of the code

(11) The result of running the following code is ().

verse = ["长亭外", "古道边", "芳草碧连天"]
del verse[-1]
print(verse)

A. ['Ancient Road Side','Fragrant Grass Biliantian']
B. ['Changting Outside','Ancient Road Side']
C. ['Fangcao Biliantian']
D. Outside Changting Pavilion, an Ancient Road

(12) Xiao Ming wants to sort the scores of the top 10 students in the class in descending order using a list. Below is the code she wrote. Please fill in the code in ______.

grade = [98,99,97,100,100,96,94,89,95,100]
print("原列表:",grade)
__________________________
print("降序:", grade)

A. grade.sort()
B. grade.sort(reverse=True)
C. grade.sort(reverse=False)
D. sort(grade)

(13) Xiaoqi made a mistake in dictating Chinese characters, so the teacher punished him to write each wrong character 10 times. Xiaoqi wants to write a piece of code to print out the result that the teacher wants. So he wrote the following 4 pieces of code, please select which piece of code can achieve this function. ()

A 
01 word = '佩励博藕薄拂浇'
02 print(word*10)

B
01 word = '佩励博藕薄拂浇'
02 print(word+10)

C 
01 word = '佩励博藕薄拂浇'
02 for i in word:
03 	print(i*10)

D
01 word = '佩励博藕薄拂浇'
02 print(word,10)

(14) Xiaoqi wanted to multiply every number in the list of multiples of 7 he defined by himself by 3, so he wrote the following code. After running it did not get the desired effect, can you help him correct it? ()

num_list = [7,14,21,28,35,42,49]
print(num_list * 3)

A. Modify the second sentence to print(num_list[1]*3)
B. Add above the second sentence: for i in num_list:
C. Modify the second sentence to: print(i*3)
D. B and C must be modified

(15) Xiao Qi defines a list of multiples of 7 arranged in increasing order. Now I want to output all the remaining elements starting from the 4th element, so I wrote the following code, can you see if his code will report an error? ()

num_list = [7,14,21,28,35,42,49]
print(num_list[3:100])

A. It will report a list index out of range error
B. In addition to outputting "28,35,42,49", it will also output a string of blank values
. C. It will report a syntax error
. D. It will output normally.

(16) Arrange according to height! It is known that the heights of 4 people are 165, 172, 183, and 178 respectively. They are required to be arranged in teams from high to low. Which of the following codes is correct is ()

A
x = [165,172,183,178]
x.sort()
print(x)

B x = (165,172,183,178)
x.sort(reverse = True)
print(x)

C
x = [165,172,183,178]
y = sorted(x)
print(y)

D
x = (165,172,183,178)
y = sorted(x,reverse = True)
print(y)

(17) Create an empty list, name it names, add Lihua, Rain, Jack, Xiuxiu, Peiqi and Black elements to it, insert a Blue before Black in the names list, change the name of Xiuxiu in the names list to Chinese, point out The wrong place in the code below ().
Insert picture description here
A. Nothing wrong
. B. Line 2 should not be 1, but 0
C. Line 2 should not be 1, but -1
D. Line 2 should use append() method

(18) Xiaohong wants to design such a piece of code. The user enters the month to determine which season the month is. Below is the code she wrote. Please help to see if there are any errors ().
Insert picture description here

A. There is no error
B. The first line needs to use the int() function for data type conversion
C. The elif on line 14 should be changed to else
D. Both B and C are correct

(19) DRY is a guiding principle recognized by programmers: Don’t Repeat Yourself. Don’t always copy the code repeatedly, so Xiao Ming, who has just learned the sequence, decided to write a piece of code by himself. The function is to enter 5 integers at random. The minimum value of these 5 integers is automatically output. Can this code be implemented?

number = ()
j = 0
for i in range(5):
    x = input("请输入数字:\n")
    j += 1
    if j == 6:
        break
    number.append(x)

print(f"五个整数中的最小值为: {min(number)}")

A. No, the 6th line of code should be changed to if j == 5:
B. No, the 1st line of code should be changed to number = []
C. No, the 4th line of code should be changed to x = int(input(“ Please enter a number:\n”))
D. Both B and C options have to be changed

(20) The unit price of Huawei P40 pro is 5988. Amo, who has just studied the list, bought one for himself and his mother. Now it wants to output the total amount. Can the following code achieve the desired function?
Insert picture description here
A. It can be realized
B. No, you can change p40pro*2 to p40pro[0]*2
C. No, you can change [5988] to 5988
D. B or C can be either

2. Tuple exercises after class

(1) Which of the following statements about tuples are correct? ()
A. When connecting tuples, the content of the connection is not restricted
. B. The tuple can be connected to the list.
C. If the tuple to be connected has only one element, then no comma
is needed . D. The tuple is an immutable sequence. It cannot modify its individual element value

(2) When creating a tuple, if there is only one element, () is required.
A. Add another parenthesis to this element
B.
Add a comma after the value C. Add a comment
D. Use the assignment operator to assign a value to this element

(3) Which of the following descriptions of tuples and lists are wrong? ()
A. Tuples are defined by (), lists are defined by []
B. The elements in the list can be modified directly, but the elements in the tuple cannot be modified directly
C. The tuple is an immutable sequence structure, and the list is a variable sequence Structure
D. Tuples are variable sequence structures, and lists are immutable sequence structures

(4) Which of the following statements is correct about the creation and deletion of tuples? ()
A. Creating an empty tuple can only be used to pass empty values ​​and return values ​​to functions.
B. Create an empty tuple. There is no specific command function, that is, the tuple function changes the parameter in tuple() to None.
C. Create numeric tuples can be used with the range() function.
D. You can only use the del command to delete tuples.

(5) What are the ways to access the elements in the tuple? ()
A. Specify the index to access
B. Use the slice method to obtain the specified element
C. Use the for loop to traverse
D. All of the above

(6) Regarding the modification of tuples, which of the following statements is correct? ()
A. Tuple is an immutable sequence, so the elements in the tuple cannot be modified.
B. Can re-assign tuples
. C. Can splice tuples, and tuples can be spliced ​​with any type of data.
D. Re-assign the tuples by concatenating tuples. If one of the tuples has only one element, there is no need to add a comma at the end.

(7) The following difference between the tuple generated by the tuple comprehension and the tuple generated by the tuple() function alone, () is wrong.
A. When the amount of data is large, it is more convenient and faster to generate tuples using tuple comprehension.
B. The result generated by the tuple comprehension can be directly used as a tuple.
C. The result generated by the tuple comprehension is only a tuple generator, which needs to be converted with the tuple() function before it can be used.
D. To tuple generator derivation tuples generated can cycle through or for direct use by __next__()traversal methods.

(8) How are tuples better than lists? ()
A. Tuples are faster to access and process than lists.
B. Lists can be used as dictionary keys, but tuples cannot.
C. Tuples can add and change data at will, and lists cannot
. D. Tuples can be connected with any other types of data, but lists cannot.

(9) In Python, "()" means ().
A. Empty set
B. Empty dictionary
C. Empty tuple
D. Empty list

(10) Which of the following is not a legal tuple ()?
A. (1,3,5,7,9)
B. 1,3,5,7,9
C. (1,3,5,7,(2,20,40))
D. (1)

(11) The output result of the following code is ().

verse1 = ("莫轻言放弃",)
print(verse1)

A. ('Don't give up lightly',)
B. ('Don't give up lightly')
C.'Don't give up lightly'
D. Don't give up lightly

(12) The output result of the following code is ().
Insert picture description here

A. (10, 12, 14, 16, 18)
('Blue Mountain','Cappuccino','Mandheling','Mocha','Colombia')
B. 10,12,14,16,18
Blue Mountains, Cappuccino, Mandheling, Mocha, Colombia
C. (
10,12,14,16,18,20 ) Blue Mountains, Cappuccino, Mandheling, Mocha, Colombia
D. (10 20)
( 'Blue Mountain','Cappuccino','Mandheling','Mocha','Colombia')

(13) The result of running the following code is ().
Insert picture description here

A. ('Chinese','Mathematics','English','Politics','History')
B. Language and Mathematics English Political History
C. The system will report TypeError
D. (Language, Mathematics, English, Politics, History)

(14) The running result of the following code is ().

verse1 = ("这份爱会不会会不会让你也好疲惫",)
print(f"verse1的类型为: {type(verse1)}")
verse2 = ("这份爱会不会会不会让你也好疲惫")
print(f"verse2的类型为: {type(verse2)}")

A. The type of verse1 is <
class'tuple '> The type of verse2 is <
class'str '> B. The type of
verse1 is class'tuple ' The type of
verse2 is class'str ' C. The type of
verse1 is'tuple ' verse2 The type is'str'
D. tuple
str

(15) The running result of the following code is ().

num = (i for i in range(4))
for i in num:
    pass

print(tuple(num))

A. (0, 1, 2, 3)
B. ()
C. 0,1,2,3
D. <generator object at 0x00000156E6CA80B0>

(16) To access the tuple element through the following code, () will be output.

temp = (1,2,3)
print(temp[2:])

A. 3
B. (3,)
C. (3)
D. (2,3)

(17) Xiao Ming wrote a tuple whose content is the name of the coffee in Yimi’s Café. He wants to modify the content of the fifth element in it to be "latte". He tries to get the element through the index first, and then to it Perform re-assignment, the code is as follows, please help to see if the code he wrote is correct? ()
Insert picture description here
A. Correct
B. The tuple is an immutable sequence and cannot directly replace a specific value in the
tuple. C. The symbol of the tuple is incorrectly defined. {} should not be used, but () should be used.
D. B and C said We are all

(18) Amo wants to add a new tuple after the original coffee tuple, he wrote the following code, please help to see if there are any errors ().

coffeename = ('蓝山','卡布奇诺','曼特宁','摩卡')
coffeename = coffeename + ['麝香猫']

A. No.
B. Tuples are immutable sequences and cannot be directly connected to lists.
C. When the connected tuple has only one element, a comma must be added after the element, and
D is not added in the code. Both B and C are correct.

(19) Amo has recently learned programming and wants to output 5 idiom numbers (the serial number starts from 1), please help to see if there is any error in her code ().

place = ("金石为开", "笃信好学", "锲而不舍", "水滴石穿", "磨杵成针")
for index, item in enumerate(place):
    print(index, item)

A. There is no error
B. The enumerate in line 2 is changed to enumerates
C. The in in line 2 is changed to is
D. The index in line 3 is changed to index + 1

(20) Amo wants to use tuple comprehension to generate a generator object containing 10 random numbers, and then convert it into a tuple and output it, so she wrote the following code, please help to see if there are any errors () .
Insert picture description here

A. Random is added in front of randint.
B. The 1 in the for loop should be changed to i
C. A and B are correct for
D. No error

Guess you like

Origin blog.csdn.net/xw1680/article/details/115090333