Python之for循环

1.遍历字符串中每个字符的for循环

name="Ted"
for character in name:
print(character)

2. for遍历列表元素
shows=["GOT",
"You"
"Know"
]
for show in shows:
print(show)

3.for遍历元组中的元素
coms=(
"World",
"always",
"sunshine"
)
for com in coms:
print(com)
4.for循环遍历字典元素
people={
"Key1":"value1",
"Key2":"value2",
"Key3":"value3"
}
for peo in people:
print(peo)
5.使用for循环修改可变且可迭代对象中的元素
people={
"Key1":"value1",
"Key2":"value2",
"Key3":"value3"
}
for peo in people:
print(peo)
由于访问可迭代对象中索引和元素是很常见的操作,python提供了专门的语法
tv=[
"GOT","Narcos","Vice"
]

for i,show in enumerate(tv):
new=tv[i]
new=new.upper()
tv[i]=new
print(tv)

6.使用两个for循环获取两个不同列表中的所有字符串,然后将每个字符大写,并放入一个新的列表中
tv=[
"GOT",
"Narcos",
"Vice"
]
coms=[
"Arrsted Development",
"friends",
"Always Sunny"
]
all_shows=[]

for show in tv:
show=show.upper()
all_shows.append(show)

for show in coms:
show =show.upper()
all_shows.append(show)

print(all_shows)

 

 

 



 

你可能感兴趣的:(Python之for循环)