python while 循环语句 (1)

演示python while语句执行过程

a = 1
while a < 10:
    print(a)
    a += 2    #意思是a=a+2
1
3
5
7
9

复杂一点

numbers = [12,37,5,42,8,3]     #odd,3个字母,3-->奇数
even = []                      #even,四个字母,4-->偶数
odd = []
while len(numbers) > 0:
    number = numbers.pop()      #pop()将列表指定位置的元素移除,同时可以将移除的元素赋值给某个变量,不填写位置参数
    if (number % 2 == 0):        #则默认删除最后一位
        even.append(number)        #
    else:
        odd.append(number)

实例

count = 0
while (count < 9):
    print 'the count is:',count
    count = count + 1
    
print"Good bye!"
  File "<ipython-input-24-7fa26f4e84a9>", line 3
    print 'the count is:',count
                        ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print('the count is:',count)?

continue 和 break 用法

i = 1
while i < 10:
    i += 1
    if i%2 > 0:      #非双数时跳过输出
        continue
    print i           #输出双数2、4、6、8、10
    
i = 1
while 1:             #循环条件为1必定成立
    print i          #输出1-10
    i += 1
    if i > 10:       #当i大于10时跳出循环
        break
  File "<ipython-input-26-679b36ca4467>", line 6
    print i
          ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(i)?

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值