码迷,mamicode.com
首页 > 编程语言 > 详细

Python第四章

时间:2020-06-30 10:25:07      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:ike   ext   循环   black   ack   player   语句   really   like   

4.1 遍历整个列表

magicians = [‘alice‘, ‘david‘, ‘carolina‘]
for magician in magicians:
print(magician)

4.1.2 在for循环中执行更多的操作

magicians = [‘alice‘, ‘david‘, ‘carolina‘]
for magician in magicians:
print(magician.title() + ", that was a great trick!")
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")

4.1.3 在for循环结束后执行一些操作

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!")

4.2.1 忘记缩进

magicians = [‘alice‘, ‘david‘, ‘carolina‘]
for magician in magicians:

print(magician) # 忘记缩进会报错必须按照下一行输出

print(magician)

4.2.2 忘记缩进额外的代码行

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") # 此处忘记缩进,最后导致只在最后一次循环输出打印这一行内容

4.2.3 不必要的缩进

message = "Hello Python world!"

print(message) # 此处不需要缩进,否则会有报错提示,必须按照下一行语句输出打印

print(message)

4.2.4 循环后不必要的缩进

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!") # 此处不需要缩进,不然结果每次循环都会打印输出此行内容

4.2.5 遗漏了冒号

magicians = [‘alice‘, ‘david‘, ‘carolina‘]

for magician in magicians # 若此行不加冒号会导致语法错误,必须按照下一行语句输出打印

for magician in magicians:
print(magician)

4-1 按要求打印输出披萨列表

pizzas = [‘pepperoni‘, ‘hawaiian‘, ‘veggie‘]
for pizza in pizzas:
print(pizza)
print("\n")
for pizza in pizzas:
print("I like " + pizza + "pizza.")
print("\nI really love pizza!")

4-2 按要求打印动物列表

animals = [‘dog‘, ‘cat‘, ‘pig‘]
for animal in animals:
print(animal)
print("A " + animal + " will make a great pet.")
print("\nAny of these animals would make a great pet!")

4.3 创建数值列表

4.3.1 使用函数range()

for value in range(1, 5):
print(value) # 打印1-4

for value in range(1, 6):
print(value) # 打印1-5

4.3.2 使用函数range()创建数字列表

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

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

4.3.3 对数字列表执行简单的统计计算

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

4.3.4 列表解析

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

4.4.1 切片

players = [‘charles‘, ‘martina‘, ‘michael‘, ‘florence‘, ‘eli‘]
print(players[0:3])

players = [‘charles‘, ‘martina‘, ‘michael‘, ‘florence‘, ‘eli‘]
print(players[1:4])

players = [‘charles‘, ‘martina‘, ‘michael‘, ‘florence‘, ‘eli‘]
print(players[:4])

players = [‘charles‘, ‘martina‘, ‘michael‘, ‘florence‘, ‘eli‘]
print(players[2:])

players = [‘charles‘, ‘martina‘, ‘michael‘, ‘florence‘, ‘eli‘]
print(players[-3:])

4.4.2 遍历切片

players = [‘charles‘, ‘martina‘, ‘michael‘, ‘florence‘, ‘eli‘]
print("Here are the first three players on my team:")
for player in players[:3]:
print(player.title())

4.4.3 复制列表(省略起始索引和终止索引)

my_foods = [‘pizza‘, ‘falafel‘, ‘carrot cake‘]
friend_foods = my_foods[:]
print("My favorite foods are:")
print(my_foods)
print("\nMy fiend‘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 fiend‘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 fiend‘s favorite foods are:")
print(friend_foods)

4.5.1 定义元组(元组:不可变的列表)

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

dimensions = (200, 50)

dimensions[0] = 250 # 元组不能修改,即不能给元组的元素赋值,这样运行会出现类型错误

dimensions[1] = 100 # 元组不能修改,即不能给元组的元素赋值,这样运行会出现类型错误

4.5.2 遍历元组中的所有值

dimensions = (666, 66)
for dimension in dimensions:
print(dimension)

4.5.3 修改元组变量

dimensions = (200, 50)
print("Original dimensions:")
for dimension in dimensions:
print(dimension)

dimensions = (400, 100)
print("\nModified dimensions:")
for dimension in dimensions:
print(dimension)

4-3 使用一个for 循环打印数字1~20(含)

numbers = range(1, 21) # 或者用numbers = list(range(1, 21))
for number in numbers:
print(number)

4-4 创建一个列表并打印

lists = range(1, 1000001)

for n in lists:

print(n)

4-5 计算1~1 000 000的总和

numbers = range(1, 1000001)
print(min(numbers))
print(max(numbers))
print(sum(numbers))

4-6 创建列表(0~20)并打印其中的奇数

list = range(1, 20, 2)
for number in list:
print(number)

4-7 创建列表:3的倍数(其中包含3~30内能被3整除的数字)并打印

list = range(3, 33, 3)
for number in list:
print(number)

4-8 打印1-10的立方数

list = range(1, 11)
numbers = []
for n in list:
numbers.append(n**3)
print(numbers)

4-9 立方解析打印1-10的立方数

numbers = [n**3 for n in range(1, 11)]
print(numbers)

4-10 切片

numbers = range(1, 11)
print("\nThe first three items in the list are:\n")
print(numbers[:3])
for n in numbers[:3]:
print(n)
print("\nThe items from tne middle of the list are:\n")
print(numbers[3:6])
for n in numbers[3:6]:
print(n)
print("\nThe last three items in the list are:\n")
print(numbers[-3:])
for n in numbers[-3:]: # 要取最后一个元素,后面只能用空;如果用-1,无法打印最后一个元素
print(n)

4-11 按要求打印输出披萨列表

favorite_pizzas = [‘pepperoni‘, ‘hawaiian‘, ‘veggie‘]
friend_pizzas = favorite_pizzas[:]
favorite_pizzas.append("meat lover‘s")
friend_pizzas.append(‘pesto‘)
print("\nMy favorite pizzas are:\n")
for pizza in favorite_pizzas:
print(pizza)
print("\nMy friend‘s favorite pizzas are:\n")
for pizza in friend_pizzas:
print(pizza)

4-12 使用多个循环输出打印列表

my_foods = [‘pizza‘, ‘falafel‘, ‘carrot cake‘]
friend_foods = my_foods[:]
my_foods.append(‘cannoli‘)
friend_foods.append(‘ice cream‘)
print("My favorite foods are:")
for food in my_foods:
print(food)
print("\nMy fiend‘s favorite foods are:")
for food in friend_foods:
print(food)

4-13 按要求打印餐馆的实物

menu_items = (‘rockfish sandwich‘, ‘ice cream‘, ‘halibut nuggets‘, ‘smoked salmon chowder‘, ‘salmon burger‘, ‘crab cakes‘)
print("\nYou can choose the following menu items:")
for food in menu_items:
print("- " + food)

menu_items[0] = ‘vegetables‘ # 无法修改元组内的元素

print(menu_items[0])
menu_items = (‘rockfish sandwich‘, ‘ice cream‘, ‘halibut nuggets‘, ‘smoked salmon chowder‘, ‘black cod tips‘, ‘king crab legs‘)
print("\n Our menu has been updated.")
print("\nYou can choose the following items:")
for food in menu_items:
print("- " + food)

Python第四章

标签:ike   ext   循环   black   ack   player   语句   really   like   

原文地址:https://www.cnblogs.com/XDZ-TopTan/p/13211979.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!