如何将’hello’赋给列表的第四个值,而列表保存在名为 spam的变量中?(假定变量包含[2, 4, 6, 8, 10])。
spam = [2,4,6,8,10]
spam[3] = 'hello'
print(spam)
spam = ['a','b','c','d']
spam [int('3'*2)/11]
print(spam)
print(spam[-1])
print(spam[:2])
[
2, 4, 6, 'hello', 10]
Traceback (most recent call last):
File "F:/python/.idea/11,10练习.py", line 62, in <module>
spam [int('3'*2)/11]
TypeError: list indices must be integers or slices, not float
d
['a', 'b']
5.假定 bacon 包含列表[3.14, ‘cat’, 11, ‘cat’, True]。
6 .bacon.index(‘cat’)求值为多少?
7.bacon.append(99)让 bacon 中的列表值变成什么样? 8.bacon.remove(‘cat’)让 bacon 中的列表时变成什么样?
bacon = [3.14,'cat',11,'cat',true]
bacon.index('cat')
print(bacon)
bacon.append(99)
print(bacon)
bacon.remove('cat')
print(bacon)
bacon = [3.14,'cat',11,'cat',true]
NameError: name 'true' is not defined
//index()检验其中是否还有子字符串
[3.14, 'cat', 11, 'cat', 'true']
[3.14, 'cat', 11, 'cat', 'true', 99]
[3.14, 11, 'cat', 'true', 99]