3-1姓名:
name = ["James", "Harden", "Steph", "Curry"]
print(name[0].title() + " " + name[1].title() + " " + name[2].title() + " " + name[3].title() + " ")
3-2问候语:
name = ["James", "Harden", "Steph", "Curry"]
print("Hello, " + name[0].title())
print("Hello, " + name[1].title())
print("Hello, " + name[2].title())
print("Hello, " + name[3].title())
3-5修改嘉宾名单:
guest = ["Hawking", "Einstein", "Turing"]
print(guest[0] + " can't attent the meeting.")
guest[0] = "Yang"
print("Welcome to the meeting, " + guest[0] + ", " + guest[1] + " and " + guest[2] + ".")
3-6添加嘉宾:
guest = ["Hawking", "Einstein", "Turing"]
print(guest[0] + " can't attent the meeting.")
guest[0] = "Yang"
print("Welcome to the meeting, " + guest[0] + ", " + guest[1] + " and " + guest[2] + ".")
print("A bigger table was found.")
guest.insert(0,"Gauss")
guest.insert(2,"Newton")
guest.append("Fermi")
print("Welcome to the meeting, " + guest[0] + ", " + guest[1] + ", " + guest[2] + "." + guest[3] + ", " + guest[4] + " and " + guest[-1] + ".")
3-7缩减名单:
guest = ["Hawking", "Einstein", "Turing"]
print(guest[0] + " can't attent the meeting.")
guest[0] = "Yang"
print("Welcome to the meeting, " + guest[0] + ", " + guest[1] + " and " + guest[2] + ".")
print("A bigger table was found.")
guest.insert(0,"Gauss")
guest.insert(2,"Newton")
guest.append("Fermi")
print("Welcome to the meeting, " + guest[0] + ", " + guest[1] + ", " + guest[2] + ", " + guest[3] + ", " + guest[4] + " and " + guest[-1] + ".")
print("Sorry, only two guests could be invited.")
poped_guest = guest.pop()
print("Sorry, " + poped_guest + ".")
poped_guest = guest.pop()
print("Sorry, " + poped_guest + ".")
poped_guest = guest.pop()
print("Sorry, " + poped_guest + ".")
poped_guest = guest.pop()
print("Sorry, " + poped_guest + ".")
print(guest[0] + " is still invited.")
print(guest[1] + " is still invited.")
del guest[1]
del guest[0]
print(guest)
3-8放眼世界:
place = ["Japan", "Britain", "America", "France", "China"]
print(place)
print(sorted(place))
print(place)
print(sorted(place, reverse = True))
print(place)
place.reverse()
print(place)
place.reverse()
print(place)
place.sort()
print(place)
place.sort(reverse = True)
print(place)
3-9晚餐嘉宾:
guest = ["Hawking", "Einstein", "Turing"]
print(str(len(guest)) + " guests can attent the meeting.")
4-1比萨:
pizza = ["beef", "pork", "mushroom"]
for i in pizza:
print("I love "+ i + " pizza.")
print("I really love " + pizza[0] + ", " + pizza[1] + " and " + pizza[2] + " pizza.")
4-3数到20:
for i in range(1,21):
print(i)
4-5计算1~1000000的总和:
numbers = list(range(1,10000001))
print(min(numbers))
print(max(numbers))
sum(numbers)
4-6奇数:
odd_numbers = list(range(1,21,2))
for num in odd_numbers:
print(num)
4-7 3的倍数:
three_numbers = list(range(3,31,3))
for num in three_numbers:
print(num)
4-8立方:
squares = []
for i in range(1,11):
ii = i ** 3
squares.append(ii)
print(squares)
4-9立方解析:
squares = [i ** 3 for i in range(1,11)]
print(squares)
4-10切片:
pizza = ["beef", "pork", "mushroom", "cheese", "vegetable"]
print(pizza[:3])
print(pizza[1:4])
print(pizza[-3:])
4-11你的比萨和我的比萨:
pizza = ["beef", "pork", "mushroom", "cheese", "vegetable"]
friend_pizza = pizza[:]
pizza.append("sauce")
friend_pizza.append("coke")
print(pizza)
print(friend_pizza)
4-13自助餐:
dimensions = ("beef", "pork", "mushroom", "cheese", "vegetable")
for i in dimensions:
print(i)
dimensions = ("beef", "pork", "mushroom", "sauce", "coke")
for i in dimensions:
print(i)