for()循环-----Python

For 循环:

For循环是迭代对象元素的常用方法(在第一个示例中,列表)

具有可迭代方法的任何对象都可以在for循环中使用。

python的一个独特功能是代码块不被{} 或begin,end包围。相反,python使用缩进,块内的行必须通过制表符缩进,或相对于周围的命令缩进4个空格。

虽然这一开始可能看起来不直观,但它鼓励编写更易读的代码,随着时间的推移,你会学会喜欢它

#取第一个列表成员(可迭代),暂称它数字(打印它)
#取列表的第二个成员(可迭代),暂时将其称为数字,等等......

for number in [23, 'q', 12, 16, 7]: 
    print(number)
print('Hi')


#输出:
23
q
12
16
7
Hi

枚举 

friends = ['steve', 'rachel', 'michael', 'adam', 'monica']
for index, friend in enumerate(friends):
    print(index,friend)

#输出:
0 steve
1 rachel
2 michael
3 adam
4 monica

Continue:

continue语句将转到循环的下一次迭代

continue语句用于忽略某些值,但不会中断循环

详见方法二

Break :

break语句将完全打断循环

text='''i come from huaian over 1 2 3 qw e r'''
text=text.split()[0:20]
new=[]

for tex in text:
    if tex=='over' :
        break
    new.append(tex)
print(new)



#输出:
['i', 'come', 'from', 'huaian']

 

题目: 

从文本中删除标点符号并将最终产品转换为列表: 

On a dark desert highway, cool wind in my hair Warm smell of colitas, rising up through the air Up ahead in the distance, I saw a shimmering light My head grew heavy and my sight grew dim I had to stop for the night There she stood in the doorway; I heard the mission bell And I was thinking to myself, "This could be Heaven or this could be Hell" Then she lit up a candle and she showed me the way

(加州旅馆)

solution 1: 

text = '''On a dark desert highway, cool wind in my hair Warm smell of colitas, rising up through the air Up ahead in the distance, I saw a shimmering light My head grew heavy and my sight grew dim I had to stop for the night There she stood in the doorway; I heard the mission bell And I was thinking to myself, "This could be Heaven or this could be Hell" Then she lit up a candle and she showed me the way'''
for string in ', . ; " ':
    text=text.replace(string,' ')
text=text.split()[0:100]
newtext=[]
for tex in text :
    if len(tex)>0:
        newtext.append(tex)
print(newtext)


#输出:
['On', 'a', 'dark', 'desert', 'highway', 'cool', 'wind', 'in', 'my', 'hair', 'Warm', 'smell', 'of', 'colitas', 'rising', 'up', 'through', 'the', 'air', 'Up', 'ahead', 'in', 'the', 'distance', 'I', 'saw', 'a', 'shimmering', 'light', 'My', 'head', 'grew', 'heavy', 'and', 'my', 'sight', 'grew', 'dim', 'I', 'had', 'to', 'stop', 'for', 'the', 'night', 'There', 'she', 'stood', 'in', 'the', 'doorway', 'I', 'heard', 'the', 'mission', 'bell', 'And', 'I', 'was', 'thinking', 'to', 'myself', 'This', 'could', 'be', 'Heaven', 'or', 'this', 'could', 'be', 'Hell', 'Then', 'she', 'lit', 'up', 'a', 'candle', 'and', 'she', 'showed', 'me', 'the', 'way']

solution 2:

newt=[]

for tex in text:
    if tex==' ':
        continue
    newt.append(tex)
print(newt)



#输出:
['On', 'a', 'dark', 'desert', 'highway', 'cool', 'wind', 'in', 'my', 'hair', 'Warm', 'smell', 'of', 'colitas', 'rising', 'up', 'through', 'the', 'air', 'Up', 'ahead', 'in', 'the', 'distance', 'I', 'saw', 'a', 'shimmering', 'light', 'My', 'head', 'grew', 'heavy', 'and', 'my', 'sight', 'grew', 'dim', 'I', 'had', 'to', 'stop', 'for', 'the', 'night', 'There', 'she', 'stood', 'in', 'the', 'doorway', 'I', 'heard', 'the', 'mission', 'bell', 'And', 'I', 'was', 'thinking', 'to', 'myself', 'This', 'could', 'be', 'Heaven', 'or', 'this', 'could', 'be', 'Hell', 'Then', 'she', 'lit', 'up', 'a', 'candle', 'and', 'she', 'showed', 'me', 'the', 'way']

题目二:
   编写一个Python程序,它迭代整数从1到50(使用for循环)。对于偶数的整数,将其附加到列表even_numbers。对于奇数的整数,将其附加到奇数奇数列表中

# Making empty lists to append even and odd numbers to. 
even_numbers = []
odd_numbers = []

for i in range(1,51):
    if i%2 == 0:
        even_numbers.append(i)
    else :
        odd_numbers.append(i)

print("Even Numbers: ", even_numbers)
print("Odd Numbers: ", odd_numbers)


#输出:
Even Numbers:  [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50]
Odd Numbers:  [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49]

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

静海彭于晏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值