《Python编程-从入门到实践》第三章及第四章习题训练

本文涵盖了《Python编程-从入门到实践》第三章和第四章的习题,包括姓名和问候语的编程实现,嘉宾名单的操作,数学计算如数的总和、奇偶性和立方等,以及比萨和切片相关的编程练习。
摘要由CSDN通过智能技术生成

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)




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值