一、列表
names=["Ezreal","Caitlyn","Vayne","Zed"]
1.不按照索引取值
for name in names:
print(name,end=" ") # Ezreal Caitlyn Vayne Zed
2.按照索引取值
a.for循环
for name in range(len(names)):
print(name,names[name])
# 0 Ezreal # 1 Caitlyn # 2 Vayne # 3 Zed
b.while 循环
count=0
while count<len(names):
print(count,names[count])
count+=1
# 0 Ezreal # 1 Caitlyn # 2 Vayne # 3 Zed
二、元组
names=("Ezreal","Caitlyn","Vayne","Zed")
1.不按照索引取值
for name in names:
print(name)
# Ezreal Caitlyn Vayne Zed
2.按照索引取值
for name in range(len(names)):
print(name,names[name])
# 0 Ezreal # 1 Caitlyn # 2 Vayne # 3 Zed
三、字典
lol={"name":"Ezreal","position":"ADC","opponent":"Vayne"}
1.不按照索引取值
a.for i in lol:
print(i)
#取出key,并不会取出value #name #position #opponent
b.for i in lol:
print(i,lol[i])
# 取出key 和value #name Ezreal #position ADC #opponent Vayne
四、字符串
str="abcde"
for i in str:
print(i,type(i))
#a <class ‘str‘> #b <class ‘str‘> #c <class ‘str‘> #d <class ‘str‘> #e <class ‘str‘>
原文地址:https://www.cnblogs.com/Smart1san/p/9119001.html
时间: 2024-10-03 22:38:55