python之for循环完整版_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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值