#写 一些 python特性
mylist =[1,2,3,45,6,7,8]
other =[mylist[0],mylist[1]] #取前面俩位
for i in range(3):
other.append(mylist[i])
other =mylist[0:2]#取前俩位的范围
other = mylist[2:4]#去下标2到4的俩位
mylist=list(range(100))#做了一个从一到100的数
other =mylist[::2]#从开始到结束 俩个已取
other =mylist[::-2]#倒序
#循环
for a in mylist:
print (a)
listlen =len(mylist)
for a in range(listlen):
print (a)
#字典的特性
mydict ={1:'a',2:'b',3:'c'}
for a in mydict:#打印key
print (a)
for a in mydict.keys():#打印key
print (a)
for a in mydict:#打印value
print (mydict[a])
for a in mydict.values():#打印value
print (a)
for a in mydict.items():
print (a)
#生成器
mylist=[]
for i in renge(1,11):
mylist.append(i*i)
#列表生成器 可以玩的很复杂 他是玩具
mylist =[x*x for x in range(1,11)]
#生成器 用一次就可以扔了
gen=(x*x for x in range(1,11))
next(gen)#读一个 每一个只能读一次
#遍历生成器 不能会头 去没了
for x in gen:
print (x)
python3 里面的一些特性
最新推荐文章于 2024-04-14 16:18:09 发布