Coursera课程Python for everyone:chapter5

本文详细探讨了Coursera上的Python for everyone课程第五章的内容,包括10道试题的解析和解答。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Chapter 5

10 试题

1. 

What is wrong with this Python loop:

n = 5
while n > 0 :
    print n
print 'All done'

This loop will run forever

The print 'All done' statement should be indented four spaces

There should be no colon on the while statement

while is not a Python reserved word

2. 

What does the break statement do?

Jumps to the "top" of the loop and starts the next iteration

Exits the currently executing loop

Exits the program

Resets the iteration variable to its initial value

3. 

What does the continue statement do?

Exits the program

Jumps to the "top" of the loop and starts the next iteration

Resets the iteration variable to its initial value

Exits the currently executing loop

4. 

What does the following Python program print out?

tot = 0 
for i in [5, 4, 3, 2, 1] :
    tot = tot + 1
print tot

5

0

15

10

5. 

What is the iteration variable in the following Python code:

friends = ['Joseph', 'Glenn', 'Sally']
for friend in friends :
     print 'Happy New Year:',  friend
print 'Done!'

friend

Glenn

Sally

friends

6. 

What is a good description of the following bit of Python code?

zork = 0
for thing in [9, 41, 12, 3, 74, 15] :
    zork = zork + thing
print 'After', zork

Compute the average of the elements in a list

Find the smallest item in a list

Sum all the elements of a list

Count all of the elements in a list

7. 

What will the following code print out?

smallest_so_far = -1
for the_num in [9, 41, 12, 3, 74, 15] :
   if the_num < smallest_so_far :
      smallest_so_far = the_num
print smallest_so_far

Hint: This is a trick question and most would say this code has a bug - so read carefully

-1

74

42

3

8. 

What is a good statement to describe the is operator as used in the following if statement:

if smallest is None :
     smallest = value

Is true if the smallest variable has a value of -1

matches both type and value

The if statement is a syntax error

Looks up 'None' in the smallest variable if it is a string

9. 

Which reserved word indicates the start of an "indefinite" loop in Python?

indef

while

break

for

def

10. 

How many times will the body of the following loop be executed?

n = 0
while n > 0 :
    print 'Lather'
    print 'Rinse'
print 'Dry off!'

0

This is an infinite loop

5

1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值