【pytnon】循环、排序、元组、if语句

print("for循环遍历整个列表")
magicians = ['alice', 'david', 'carolina']
for magician in magicians:print(magician)

magicians = ['alice', 'david', 'carolina']
for magician in magicians:
    print(magician.title() + ", that was a great trick!")
    print("I can't wait to see your next trick, " + magician.title() + ".\n")
print("Thank you, everyone. That was a great magic show!")

#函数range() 让Python从你指定的第一个值开始数,并在到达你指定的第二个值 后停止,因此输出不包含第二个值(这里为5)。
print("创建数值列表")
for value in range(1,5): print(value)
numbers = list(range(1,6))
print(numbers)
even_numbers = list(range(2,11,2))
print(even_numbers)


squares = []
for value in range(1,11):
    squares.append(value**2)
    print(squares)

digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
print(min(digits))
print(max(digits))
print(sum(digits))

print("使用列表的一部分")
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[0:3])
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[2:])
print("遍历切片")
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print("Here are the first three players on my team:")
for player in players[:3]:
    print(player.title())

my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:]
print("My favorite foods are:")
print(my_foods)
print("\nMy friend's favorite foods are:")
print(friend_foods)

my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:]
my_foods.append('cannoli')
friend_foods.append('ice cream')
print("My favorite foods are:")
print(my_foods)
print("\nMy friend's favorite foods are:")
print(friend_foods)

print("元组")
dimensions = (200, 50)
print(dimensions[0])
print(dimensions[1])

dimensions = (200, 100)
for dimension in dimensions:
    print(dimension)

####################### 第 5 章 if 语句 ################################
print("##############第 5 章章 if 语句##############")
cars = ['audi', 'bmw', 'subaru', 'toyota']
for car in cars:
    if car == 'bmw': print(car.upper())
    else:print(car.title())

car = 'bmw'
car == 'bmw'
print(car == 'bmw')

answer = 17
if answer != 42: print("That is not the correct answer. Please try again!")

requested_toppings = ['mushrooms', 'onions', 'pineapple']
print('mushrooms' in requested_toppings)

age = 12
if age < 4: print("Your admission cost is $0.")
elif age < 18: print("Your admission cost is $5.")
else:print("Your admission cost is $10.")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱喝水的小猪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值