赞
踩
-
- # 1.遍历整个列表
- magicians = ['alice', 'david', 'carolina']
- for magician in magicians:
- print(magician)
-
- 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")
-
- # 2.避免缩进错误
- # 2.1忘记缩进
- # magicians = ['alice', 'david', 'carolina']
- # for magician in magicians:
- # print(magician)
-
- # 2.2忘记缩进额外的代码行
- # 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") # 最后一个magician为carolina
-
- # 2.3不必要的缩进
- # message = 'hello'
- # print(message)
-
- # 2.4遗漏了冒号
- # for magician in magicians
- # print(magician)
-
- # P50动手试一试
- # 4-1
- pizzas = ['A', 'B', 'C']
- for pizza in pizzas:
- print('I like %s pizza' % pizza)
- print('I really love pizza')
-
- # 4-2
- animals = ['A', 'B', 'C']
- for animal in animals:
- print('A %s would make a great pet.' % animal)
- print('Any of these animals would make a great pet!')
-
-
- # 3创建数值列表
- # 3.1使用range()函数,产生的结果:{x belongs with Z : lower <= x < upper}
- # 使用range()时,如果输出不符合预期,请尝试将指定的值加1或减1。
- for value in range(1, 5):
- print(value)
-
- # 3.2使用range()创建数字列表
- numbers = list(range(1, 5))
- print(numbers)
- # 打印1~10内的偶数:
- even_numbers = list(range(2, 11, 2))
- print(even_numbers)
-
- # 创建一个包含前10个整数的平方的列表
- squares = []
- for value in range(1, 11):
- # square = value ** 2 # 两个*表示乘方运算
- # squares.append(square)
- squares.append(value ** 2)
- print(squares)
-
- # 3.3对数字列表执行统计计算
- digits = list(range(0, 10))
- print(digits)
- print(min(digits))
- print(max(digits))
- print(sum(digits))
-
- # 3.4列表解析:列表解析将for循环和创建新元素的代码合并成一行,并自动附加新元素。
- '''
- 首先指定一个描述性的列表名,如squares; 然后,指定一个左方括号,
- 并定义一个表达式,用于生成你要存储到列表中的值。在这个示例中,表达式为value**2,
- 它计算平方值。接下来,编写一个for循环, 用于给表达式提供值,再加上右方括号。
- '''
- squares = [value**2 for value in range(1, 11)]
- print(squares)
-
- # P54动手试一试
- # 4-3
- for i in range(1, 21):
- print(i)
- # 4-4
- # for i in range(1, 1000001):
- # print(i)
- # 4-5
- numbers = list(range(1, 1000001))
- print(min(numbers))
- print(max(numbers))
- print(sum(numbers))
- # 4-6
- odds = list(range(1, 20, 2))
- for odd in odds:
- print(odd)
- # 4-7
- threes = []
- for i in range(3, 30):
- if i % 3 == 0:
- threes.append(i)
- print(threes)
- # 4-8/9
- nums = [value ** 3 for value in range(1, 11)]
- print(nums)
-
-
- # 4.使用列表的一部分
- # 4.1切片,要创建切片,可指定要使用的第一个元素和最后一个元素的索引。
- players = ['charles', 'martina', 'michael', 'florence', 'eli']
- print(players)
- print(players[0:3]) # 前三个
- print(players[1:4]) # 第2-4个
- # 如果你没有指定第一个索引或最后一个索引, Python将自动从列表开头开始或结尾终止:
- print(players[:4])
- print(players[1:])
-
- print(players[-3:]) # 后三个
- print(players[-2:]) # 后两个
-
- # 4.2遍历切片,如果要遍历列表的部分元素,可在for循环中使用切片。
- for player in players[:3]:
- print(player) # 前3个
-
-
- # 4.3复制列表
- my_foods = ['pizza', 'falafel', 'carrot cake'] #
- friend_foods = my_foods[:] # 在不指定任何索引的情况下从列表my_foods中提取一个切片,复制一个切片
- # friend_foods = my_foods # 浅复制,指向同一个列表
- print(friend_foods)
- my_foods.append('a')
- print('确实是两个列表')
- print(my_foods)
- print(friend_foods)
-
- # P58动手试一试
- # 4-10
- print('the list is:',players)
- print('The first three items in the list are:', players[:3])
- print('Three items from the middle of the list are:', players[1:4])
- print('The last three items in the list are:', players[-3:])
- # 4-11
- friend_pizzas = pizzas[:]
- print(friend_pizzas)
- print(pizzas)
- pizzas.append('D')
- friend_pizzas.append('F')
- print('My favorite pizzas are:')
- for pizza in pizzas:
- print(pizza)
- print("My friend's favorite pizza are:")
- for pizza in friend_pizzas:
- print(pizza)
-
-
- # 5.元组
- # Python将不能修改的值称为不可变的,而不可变的列表被称为元组。使用圆括号来标识
-
- # 5.1定义元祖
- dimensions = (200, 50)
- print(dimensions)
- # 5.2遍历元组中的所有值
- for dimension in dimensions:
- print(dimension)
-
- # 5.3修改元组变量,虽然不能修改元组的元素,但可以给存储元组的变量赋值
- dimensions = (300, 400)
- print(dimensions)
-
- # P60动手试一试
- #4-13
- foods = ('A', 'B', 'C', 'D', 'E')
- for food in foods:
- print(food)
- # foods[0] = 'C'
- foods = ('X', 'Y', 'C', 'D', 'E')
- for food in foods:
- print(food)
-
-
- # 6.设置代码格式:PEP8格式指南
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。