# [3-1, 3-2, 3-3]
# 3-1
names = ['Mao', 'Deng', 'Jiang', 'Hu', 'Xi']
for name in names:
print(name)
# 3-2
for name in names:
print(name + ': Hello!')
# 3-3
vehicles = ['motorcycle', 'bike', 'car']
for vehicle in vehicles:
print('I would like to own a ' + vehicle)
# [3-4, 3-5, 3-6, 3-7]
# 3-4
names = ['Mao', 'Deng', 'Jiang', 'Hu', 'Xi']
for name in names:
print(name + ', would you please having a meal with me?')
# 3-5
print('\nOh, ' + names[2] + ' won\'t have time!')
names[2] = 'Wallace'
for name in names:
print(name + ', would you please having a meal with me?')
# 3-6
print('\nI\'ve found a bigger table. It\'s my pleasure to invite more people.')
names.insert(0, 'Zhu')
names.insert(len(names)//2, 'Li')
names.append('Zhou')
for name in names:
print(name + ', would you please having a meal with me?')
# 3-7
print('\nOops, sorry, I can only invite two people.')
while len(names) > 2:
print(names.pop() + ': I\'m sorry to tell you that I can\'t invite you.')
for name in names:
print(name + ', would you please having a meal with me?')
del(names[0])
del(names[0])
print(names == [] and 'It\'s empty.' or 'It\'s not empty.')
# [3-8, 3-9, 3-10]
# 3-8
places = ['Heaven', 'Hell', 'Well', 'World' ,'SYSU']
print(places)
print(sorted(places))
print('%s\n%s'%(places, (places == ['Heaven', 'Hell', 'Well', 'World' ,'SYSU'] and 'Didn\'t changed' or 'Changed')))
print(sorted(places, reverse=True))
print('%s\n%s'%(places, (places == ['Heaven', 'Hell', 'Well', 'World' ,'SYSU'] and 'Didn\'t changed' or 'Changed')))
places.reverse()
print('%s\n%s'%(places, (places == ['Heaven', 'Hell', 'Well', 'World' ,'SYSU'] and 'Didn\'t changed' or 'Changed')))
places.reverse()
print('%s\n%s'%(places, (places == ['Heaven', 'Hell', 'Well', 'World' ,'SYSU'] and 'Didn\'t changed' or 'Changed')))
places.sort()
print('%s\n%s'%(places, (places == ['Heaven', 'Hell', 'Well', 'World' ,'SYSU'] and 'Didn\'t changed' or 'Changed')))
# 3-9
names = ['Mao', 'Deng', 'Jiang', 'Hu', 'Xi']
print('I would like to invite %d people'%len(names))
# 3-10
myList = ['Membrane', '衆軮', 'はぢはぢ', 233, places]
print(myList)
myList.append(myList)
print(myList)
print(sorted(myList, key=lambda a: False, reverse=True))
myList.reverse()
print(myList)
# [4-1, 4-2]
# 4-1
pizzas = ['A', 'B', 'C']
for pizza in pizzas:
print(pizza)
for pizza in pizzas:
print('I like ' + pizza + ' pizza')
print('I really love pizza!')
# 4-2
animals = ['horse', 'donkey', 'lama pacos']
for animal in animals:
print(animal)
for animal in animals:
print('A ' + animal + ' walks with four legs')
print('Any of these animals walk with four legs!')
# [4-3, 4-4, 4-5, 4-6, 4-7, 4-8, 4-9]
# 4-3
for i in range(1, 21):
print(i)
# 4-4
l = range(1, 1000001)
for i in l:
print(i)
# 4-5
print(min(l))
print(max(l))
print(sum(l))
# 4-6
l2 = [i for i in range(1, 21, 2)]
for i in l2:
print(i)
# 4-7
l3 = [i for i in range(3, 31, 3)]
for i in l3:
print(i)
# 4-8
l4 = [i**3 for i in range(1, 11)]
for i in l4:
print(i)
# 4-9
l5 = [i**3 for i in range(1, 11)]
hw2(第二周)
最新推荐文章于 2024-03-02 19:54:47 发布