3-2
names = ['mike','john','jack']
print("Hello,"+ names[0].title()+"!")
print("Hello,"+ names[1].title()+"!")
print("Hello,"+ names[2].title()+"!")
output:
Hello,Mike!
Hello,John!
Hello,Jack!
3-4~ 3-7
friends = ['a','b','c']
for friend in friends:
print(friend +",please have dinner with me.")
print("\n")
busy_friend = 'b'
print("Oh,"+ busy_friend +" is unable to arrive.")
friends.remove(busy_friend)
friends.append('d')
for friend in friends:
print(friend +",please have dinner with me.")
print("\n")
print("Oh,I get a bigger table and I can invite more friends.")
friends.insert(0,'e')
friends.insert(2,'f')
friends.append('g')
for friend in friends:
print(friend +",please have dinner with me.")
print("\n")
print("Sadly,the new table I buy won't arrive in time.I can only invite two friends now.")
whilelen(friends) >2 :
one = friends.pop()
print("I am sorry,"+ one +".Some accident happened and I couldn't have dinner with you this night.")
for friend in friends:
print(friend +",we will still have dinner this night.")
del friends[0]
del friends[0]
for friend in friends:
print("Oh,"+ friend +"is still in the list.")
output:
a,please have dinner with me.
b,please have dinner with me.
c,please have dinner with me.
Oh,b is unable to arrive.
a,please have dinner with me.
c,please have dinner with me.
d,please have dinner with me.
Oh,I get a bigger table and I can invite more friends.
e,please have dinner with me.
a,please have dinner with me.
f,please have dinner with me.
c,please have dinner with me.
d,please have dinner with me.
g,please have dinner with me.
Sadly,the new table I buy won't arrive in time.I can only invite two friends now.
I am sorry,g.Some accident happened and I couldn't have dinner with you this night.
I am sorry,d.Some accident happened and I couldn't have dinner with you this night.
I am sorry,c.Some accident happened and I couldn't have dinner with you this night.
I am sorry,f.Some accident happened and I couldn't have dinner with you this night.
e,we will still have dinner this night.
a,we will still have dinner this night.
3-10
countries = ["China","England","America"]
print("The initial list:")
print(countries)
print("Its length is "+str(len(countries)))
print("The sorted list without changing the initial list:")
print(sorted(countries))
print("The reversed list:")
countries.reverse()
print(countries)
print("The sorted list in reverse:")
countries.sort(reverse = True)
print(countries)
output:
The initial list:
['China', 'England', 'America']
Its length is 3
The sorted list without changing the initial list:
['America', 'China', 'England']
The reversed list:
['America', 'England', 'China']
The sorted list in reverse:
['England', 'China', 'America']