The list type in Python is ( ). A. d = [1 2 3]B. d = [1,2,3] C. d = [] D. d = list(range(3)) The keyword to define a function in Python is (

Multiple choice questions

1.The list type in Python is ( ).

A. d = [1 2 3]

B. d = [1,2,3]

C. d = []

D. d = list(range(3))

Answer: A

Analysis:

A. d = [1 2 3] is a syntax error code, the elements should be separated by commas, so it does not belong to the list type.

B. d = [1,2,3] is a list containing three integer elements.

C. d = [] is an empty list.

D. d = list(range(3)) Convert the range object to a list type. The generated list contains three integer elements: 0, 1, and 2.

Therefore, the answer is A, d = [1 2 3] is not of list type.

2.The keyword for defining functions in Python is ( ).

A.define

B. def

C. function

D.defunct

Answer: B

Analysis:

B. def is the keyword for defining functions in Python.

In Python, a function is defined using the def keyword, followed by the function name, a pair of parentheses, and then a colon. The body of the function must be indented, usually 4 spaces. For example:

def my_function():
    print("Hello World!")

Therefore, the answer is B, the keyword for defining functions in Python is def.

3. Regarding the for loop, which of the following statements is wrong

A. A for loop can traverse a string.

B. The target in the for loop syntax format cannot be a number.

C. continue can be used in for loops.

D. for loops cannot be nested.

Answer: D

Analysis:

A. The for loop can traverse the string. In Python, strings are treated as sequence types and each character in them can be iterated over using a for loop. For example:

str = "Hello World"
for char in str:
    print(char)

Output result:

H
e
l
l
o

W
o
r
l
d

C. The continue statement can be used in a for loop, which can skip certain iterations in the current loop. For example:

for i in range(10):
    if i == 3:
        continue
    print(i)

Output result:

0
1
2
4
5
6
7
8
9

D. for loops can be nested to iterate elements in a sequence in the inner loop. For example:

for i in range(3):
    for j in range(2):
        print(i, j)

Output result:

0 0
0 1
1 0
1 1
2 0
twenty one

Therefore, option D is incorrect and is incorrect.

4. When for or while is used with else, the correct description of executing the else statement is:

A. Only executed after the loop ends abnormally (ends with break).

B. Only executed after the loop ends normally.

C. Always executed.

D. Never execute.

Answer: B

Analysis:

B. Executed only after the loop ends normally is a correct description.

In Python, when a for loop or while loop ends normally (that is, not interrupted by a break statement), the corresponding else statement block will be executed. This means that the code in the else block will only be executed when the complete execution of the loop ends. If the loop is terminated prematurely by a break statement, the else statement block will not be executed.

Here’s an example:

for i in range(5):
    print(i)
else:
    print("Loop ends normally")

# Output:
#0
# 1
# 2
#3
#4
# Loop ends normally

In this example, the loop iterates from 0 to 4, and no break statement is encountered, so the loop ends normally. Therefore, the else statement block will be executed and “Loop ends normally” is output.

It should be noted that when the loop ends abnormally (that is, when the break statement is encountered), the else statement block will not be executed. This does not match the description in option A. The always executed in option C and the never executed in option D are also inaccurate. The else block will only be executed when the loop ends normally, which is the correct description of option B.

5.S=’1234567890’, the following options for 1234’ are ( ).

A. s[-10:-5]

B.s[0:3]

C.s[0:4]

D.s[1:5]

Answer: C

Analysis:

In Python, String slicing is a method for obtaining substrings of a string. It uses indexes to specify the start and end positions of the substring that needs to be extracted.

The syntax format is:

string[start:end:step]
  • start: Indicates the starting position of the substring (included in the slice).
  • end: Indicates the end position of the substring (not included in the slice).
  • step: Optional parameter, indicating the step size (that is, every number of characters, one character is taken, the default is 1).

Here are some examples:

string = "Hello, World!"

# Extract the substring from position 2 to position 5 (excluding position 5)
substring = string[2:5]
print(substring) # Output: llo

# Extract the substring from position 0 to position 6 (excluding position 6), with a step size of 2
substring = string[0:6:2]
print(substring) #Output: Hlo

# Extract the substring from position 5 to the end
substring = string[5:]
print(substring) #Output:, World!

It should be noted that the slicing operation returns a new string and does not modify the original string. If any of the start, end or step parameters are omitted, the default value is used (start defaults to 0, end defaults to the length of the string, step defaults to 1).

In addition to positive indexing, you can also use negative indexing to represent the position from the end of the string. For example, string[-1] represents the last character of the string.

Slicing can also be used with other string operations such as concatenation, replacement, etc. It is a very convenient and flexible way to deal with substrings in strings.

6. Which of the following Python comment codes is incorrect

A. #Python comment code

B. #Python comment code 1#Python comment code 2

C. ”’Python documentation comments”’

D. //Python comment code

Answer: D

Analysis:

D. //Python comment code is incorrect.

In Python, single-line comments start with a pound sign (#) instead of a double slash (//). The syntax of double slashes for single-line comments is common in other programming languages, but does not apply in Python.

Therefore, //Python commented code in option D is incorrect form of Python commented code. The correct form of comments should start with #, as shown in options A and B. The ”’Python documentation comment”’ in option C is a form of multiline comment that is also correct in Python.