操作列表的大致说明
作者:互联网
本篇文章主要是操作列表的大致说明包含一点for循环:
#操作列表
#1、遍历列表
magicians=['liuqian','aclie','david']
#for循环
for magician in magicians:
print(magician)
for magician in magicians:
print("你好,我是魔术师"+magician.title())
print("接下来是我的表演时刻"+magician)
print("谢谢观赏")
#2、注意缩进以及冒号
#3、创建数值列表
for value in range(1,5):
print(value)
#range代替列表
numbers = list(range(1,6))
print(numbers)
#输出偶数
even_number=list(range(2,11,2))
print(even_number)
#输出奇数
ji_number=list(range(1,11,2))
print(ji_number)
#平方
squares=[]
for value in range(1,11):
square = value**2
squares.append(square)
print(squares)
squares_0=[]
for value1 in range(1,11):
squares_0.append(value1**2)
print(squares_0)
#对数字列表进行简单统计
digits=[1,2,3,4,5,6,7,8,9,10]
print(min(digits))#最小数
print(max(digits))#最大数
print(sum(digits))#和
#列表解析
squares_1=[value**2 for value in range(1,11)]
print(squares_1)
#打印1-20
for value in range(1,21):
print(value)
#打印奇数
jishu =[value for value in range(1,11,2)]
print(jishu)
#循环列表
threes=[3,6,9,12,15]
for three in threes:
print(three)
#1-10的三次方
sancifang=[value**3 for value in range (1,11)]
print(sancifang)
#列表解析生成列表
san=[]
for value in range(10):
san.append((value+1)**3)
print(san)
#使用列表的一部分
#切片
names=[1,2,3,4,5,6]
print(names[0:3])
print(names[1:3])
print(names[:3])
print(names[1:])
print(names[-3:])
#遍历切片
names_0=['one','two','three','four']
for name in names_0[:3]:
print(name.title())
#复制列表
foods=['pizza','falafel','carrot cake']
friends_foods = foods[:]
friends_foods.append('ice cream')
print("my favorite foods are")
print(foods)
print("my friend favorite foods are")
print(friends_foods)
#元组
dimensions=(200,50)
print(dimensions[0])
print(dimensions[1])
for dimension in dimensions:
print(dimension)
#修改元组变量
yz=(200,50)
print("Original yz:")
for yz_1 in yz:
print(yz_1)
yz=(400,50)
print("\nModified yz")
for yz_1 in yz:
print(yz_1)
标签:大致,value,列表,range,yz,print,操作,squares 来源: https://blog.csdn.net/qq_43070795/article/details/118815444