22. 练习:列表和成员运算符
练习:列表索引
请使用列表索引根据整型变量
month
判断特定月份有多少天,并将该值存储在整型变量
num_days
中。例如,如果
month
是 8,
num_days
应该设为 31,因为第八个月份八月应该有 31 天。
请记得索引以 0 开始!
Start Quiz:
month = 8
days_in_month = [31,28,31,30,31,30,31,31,30,31,30,31]
# use list indexing to determine the number of days in month
print(num_days)
练习:列表切片
请使用列表切片记法从此列表中选择列表中的最后三个元素。提示:切片可以使用负索引!
Start Quiz:
eclipse_dates = ['June 21, 2001', 'December 4, 2002', 'November 23, 2003',
'March 29, 2006', 'August 1, 2008', 'July 22, 2009',
'July 11, 2010', 'November 13, 2012', 'March 20, 2015',
'March 9, 2016']
# TODO: Modify this line so it prints the last three elements of the list
print(eclipse_dates)
可变性练习
QUIZ QUESTION: :
假设有以下两个表达式:
sentence1
和
sentence2
sentence1 = "I wish to register a complaint."
sentence2 = ["I", "wish", "to", "register", "a", "complaint", "."]
请将以下 Python 代码与修改后的
sentence1
或
sentence2
的值相匹配。如果代码会出错,请与“Error”匹配。
ANSWER CHOICES:
Python 代码 |
|
---|---|
["We", "want", "to", "register", "a", “complaint”, "."] |
|
"I wish to register a complaint!" |
|
Error |
|
["I", "wish", "to", "register", "a", "complaint", "!"] |
|
["Our Majesty", "wish", "to", "register", "a", "complaint", "."] |
SOLUTION:
Python 代码 |
|
---|---|
["We", "want", "to", "register", "a", “complaint”, "."] |
|
Error |
|
["I", "wish", "to", "register", "a", "complaint", "!"] |
|
["Our Majesty", "wish", "to", "register", "a", "complaint", "."] |