Python之for循环
2020-12-30 08:27
标签:可迭代对象 图片 操作 用两个 character inf png you name 1.遍历字符串中每个字符的for循环 Python之for循环 标签:可迭代对象 图片 操作 用两个 character inf png you name 原文地址:https://www.cnblogs.com/JacquelineQA/p/13022375.htmlname="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)