What will the following code print out?:

以下代码将打印出什么内容?

n = 0
while True:
    if n == 3:
        break
    print(n)
    n = n + 1

答案:

0
1
2

How many lines will the following code print?:

以下代码将打印多少行?:

for i in [2,1,5]:
    print(i)

答案:
3

for i in [2,1,5]:
    print(i)

这段代码的运行结果将会是:

2
1
5

代码解释:

创建了一个包含三个元素的列表 [2,1,5]。
使用 for 循环遍历这个列表。
对于列表中的每个元素 i,使用 print() 函数将其值打印出来。
因此,循环依次输出列表中的每一个数字。

Below is code to find the smallest value from a list of values. One line has an error that will cause the code to not work as expected. Which line is it?:

以下是从值列表中查找最小值的代码。一行出现错误,将导致代码无法按预期工作。这是哪条线路?

smallest = None
print("Before:", smallest)
for itervar in [3, 41, 12, 9, 74, 15]:
    if smallest is None or itervar < smallest:
        smallest = itervar
        break
    print("Loop:", itervar, smallest)
print("Smallest:", smallest)

答案:
6

What will the following code print?:

以下代码将打印什么?:

for n in "banana":
    print(n)

答案:

b
a
n
a
n
a

What is the value of i in the following code?

下面代码中 i 的值是多少?

word = "bananana"
i = word.find("na")

答案:
2