Python中For循环与While 循环的使用

For循环是迭代对象元素的常用方法,具有可迭代方法的任何对象都可以在for循环中使用。python的一个独特功能是代码块不被{} 或begin,end包围。相反,python使用缩进,块内的行必须通过制表符缩进,或相对于周围的命令缩进4个空格。

For循环在枚举中使用:

返回一个元组,其中包含每次迭代的计数(从默认为0开始)和迭代序列获得的值:

# For在枚举中使用:
# 返回一个元组,其中包含每次迭代的计数(从默认为0开始)和迭代序列获得的值:
friends = ['steve', 'rachel', 'michael', 'adam', 'monica']
for index, friend in enumerate(friends):
    print(index,friend)

Continue和Break在For循环中的使用:

利用For循环:从下面文本中删除标点符号并将其转换为列表:

  • 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

思路:

  • 第一步:从文本中删除标点符(String中replace方法)
  • 第二步:将其转换为列表(Split函数:将String转换成list)
# 利用For循环:从文本中删除标点符号并将其转换为列表:(Continue在For循环中的使用:)
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'''
print(text)

#第一步:从文本中删除标点符,
for char in '-.,;\n"\'':
    text = text.replace(char,' ')
print(text)

#第二步:将其转换为列表
# Split converts string to list。Split函数:将String转换成list
print('要将其转换为列表:',text.split(' ')[0:20])
#法一:利用空格长度=0,去除列表中空格。
# Each item in list is split on spaces
# Dont want to have non words in my list for example ''
# which in this case are things of zero length
len('')
# Making new list with no empty words in it
cleaned_list = []
for word in text.split(' '):
    word_length = len(word)
    if word_length > 0:
        cleaned_list.append(word)#不是空格,append追加
print('利用For循环中if语句:',cleaned_list[0:20])

#法二:利用Continue:
# continue语句将转到循环的下一次迭代
# continue语句用于忽略某些值,但不会中断循环
cleaned_list = []

for word in text.split(' '):
    if word == '':
        continue#如果是空格,不处理,继续for循环
    cleaned_list.append(word)
print('利用For循环中continue',cleaned_list[0:20])
# for中Break的使用:
# break语句将完全打断循环
cleaned_list = []

for word in text.split(' '):
    if word == 'desert':
        print('I found the word I was looking for')
        break
    cleaned_list.append(word)
print(cleaned_list)

举例:

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

# Making empty lists to append even and odd numbers to.
even_numbers = []
odd_numbers = []
for number in range(1,51):
    if number%2==0:
        even_numbers.append(number)
    else:
        odd_numbers.append(number)
print(even_numbers,"\n",odd_numbers)

For 循环与While 循环区别:

For 循环While 循环
遍历一组对象条件为false时自动终止
没有break也可以结束使用break语句才能退出循环

break语句在While 循环中使用:

使用break可以完全退出循环

count = 0
while count <= 5:
    if count == 2:
        break
    count += 1
    print (count)
x = 1
while x % 5 != 0:
    x += 1
    print(x)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Laura_Wangzx

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

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

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

打赏作者

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

抵扣说明:

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

余额充值