'''
遍历:用循环语句for
for 变量名 in 列表名:
'''
magicians = ['alice','david','carolina']
for magician in magicians:
print(magician.title()+',that was a great trick!')
print('I can`t wait to see you next trick '+magician.title()+'\n')#每次迭代结束都将插入一个空行。
print('Thank you everyone.That was a great magic show!')#不在循环体里面
'''
range(a,b):函数创建数字列表
'''
for value in range(1,5):#只会生成14的数值不会生成15的
print(value)
'''
创建数字列表:用range()作为list()函数的参数来生成列表
'''
print('--------')
numbers = list(range(1,6))
print(numbers)
'''
range()函数还可以指定步长:range(a,b,步长)
'''
print('--------')
even_number = list(range(2,11,2))
print(even_number)
'''
将前十个整数的平方加入到一个列表中
'''
print('--------')
squares = []
for i in range(1,11):#变量不用申明直接应用。
#i = i**2
squares.append(i)
squares.append(i**2)
print(squares)
'''
对数字列表执行简单的数值操作
'''
digits = [1,2,3,4,5,6,7,8,9,0]
print(min(digits))
print(max(digits))
print(sum(digits))
'''
列表解析直接创造列表
'''
print('--------')
squares = [i**2 for i in range(1,11)]#[for i in range(1,11)]是错误的,这种语句方括号里for前一定要有操作
print(squares)
'''
列表切片
'''
magicians = ['alice','david','carolina','habod','vilic']
print('--------')
print(magicians[0:3])#打印列表第0~2个元素
print('--------')
print(magicians[2:4])#打印任意位置(此处为2,3)的元素
print('--------')
print(magicians[:4])#自动从开头打印(此处为(0,1,2,3))
print('--------')
print(magicians[2:])#从第三个元素开始打印一直到列表结尾
print('--------')
print(magicians[-3:])#从倒数第三个开始打印
'''
遍历切片
'''
print('--------')
magicians = ['alice','david','carolina','habod','vilic']
for magician in magicians[:3]:
print(magician.title())
'''
复制列表:
'''
print('--------')
my_food = ['pizza','falafel','carrot cake']
friend_food = my_food[:]#建立一个列表副本,将列表副本保存到friend_food里面
print(my_food)
print(friend_food)
my_food.append('cannoli')
friend_food.append('ice cream')
print(my_food)
print(friend_food)
'''
'''
print('--------')
my_food = ['pizza','falafel','carrot cake']
friend_food = my_food#相当于将friend_food与my_food建立联系。两个变量对应同一个列表
print(my_food)
print(friend_food)
my_food.append('cannoli')
friend_food.append('ice cream')
print(my_food)
print(friend_food)
'''
不可变的列表叫元组
元组名 = (元素1,元素2,元素3,...)
'''
访问元组
dimensions = (200,50)
print(dimensions[0])
print(dimensions[1])
修改元组元素会出现错误,因为元组元素是固定不可修改的
dimensions = (200,50)
dimensions[0] = 250
遍历元组
dimensions = (200,50)
for dimension in dimensions:
print(dimension)