码迷,mamicode.com
首页 > 编程语言 > 详细

Python之for循环

时间:2020-06-01 00:39:59      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:可迭代对象   图片   操作   用两个   character   inf   png   you   name   

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循环

标签:可迭代对象   图片   操作   用两个   character   inf   png   you   name   

原文地址:https://www.cnblogs.com/JacquelineQA/p/13022375.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!