• Python学习-while循环&逻辑运算符


    一、while循环,continue、break语句在while循环中的使用

    1、while循环语句:

    while 条件:

        代码块

    执行过程:判断条件是否为真,如果为真,执行代码块,继续下次循环,继续判断条件真假;如果条件为假,结束当前循环。

    代码1:

    while True:
        print('Hello world')
    View Code

    执行上述代码,程序会持续运行。在实际编程时,很少使用。

    代码2:

    count = 1
    while count <=10:
        print(count)
        count += 1
    View Code

    执行上述代码,会输出1到10。在实际编程中,这种方式经常使用。

    举个一般的栗子:

    执行代码 -> 判断条件 -> 条件为真,执行循环体(循环体中一般会有操作、改变判断条件用到的元素--改变初始值,True变为False等)--> 判断条件有变化 --> 判断条件变为假 --> 跳出当前循环(本层循环)

    2、continue语句

    设想有这样一种需求,循环输出数字1至数字10中的偶数,该怎么实现?此时continue语句就派上用场了

    代码段:

    count = 1
    while count <= 10:
        if count % 2 == 1:    # 对2取余为1,即为奇数,执行if语句对应的代码块
            count += 1
            continue
        print(count)
        count += 1

    执行结果:

    2
    4
    6
    8
    10
    [Finished in 0.0s]

    continue语句作用:结束本次循环,继续下次循环。

    即:如果执行到了continue语句,那么以上代码中的print(count) count+=1这两个语句不会执行,会继续下次循环,即跳到while后的count <= 10处,继续做判断。

    3、break语句

    再设想有这样一个简单的需要,让用户输入登录用户名,如果输入的用户名正确,提示有效用户,退出;如果输入错误,继续让用户输入登录用户名

    代码片段:

    user_in_db = 'jason'
    while True:
        user = input('Login name:')
        if user == user_in_db:
            print('Valid user name')
            break
            print('这句会执行吗')
        print('Wrong user name, try again')
    print('while同级代码块')

    输出结果:

    Login name:jason
    Valid user name
    while同级代码块

    从执行结果可以看出,if判断条件为真,程序执行到break语句,while循环体中break的后续语句都没有执行,跳出while循环,执行后续同级代码块。

    break语句作用:跳出当前循环(本层循环),不再进行while后的条件判断,继续执行后续同级代码块。

    4、while...else语句

    代码块1:

    count = 1
    while count <= 5:
        # if count == 3:
        #     break
        print(count)
        count += 1
    else:
        print('while循环正常结束')

    执行结果:

    1
    2
    3
    4
    5
    while循环正常结束
    [Finished in 0.0s]

    代码块2:

    count = 1
    while count <= 5:
        if count == 3:
            break
        print(count)
        count += 1
    else:
        print('while循环正常结束')

    执行结果:

    1
    2
    [Finished in 0.0s]

    从代码块1和代码块2的执行结果可以看出,执行到break跳出的循环,不会执行else后的语句;正常循环结束的,会执行else后的语句。

    说明:在 python 中,while … else 表示这样的意思,while 中的语句和普通的没有区别,else 中的语句会在循环正常执行完(即 while 不是通过 break 跳出而中断的)的情况下执行,for … else 也是一样。

    二、格式化输出

    %s 字符串的占位符,后边给的参数是数字也可以

    %d 数字的占位符号

    代码片段:

    name = 'jason'
    age = 25
    print('my name is %s' % name)
    print("i'm %s years old" % age)
    print("i'm %d years old" % age)

    输出结果:

    my name is jason
    i'm 25 years old i
    'm 25 years old [Finished in 0.0s]

    格式化输出还有一种format方法,个人也习惯用format的方法去格式化字符串,可以自行百度搜索了解一下。

    三、Python中的逻辑运算符

    (下方截图来自菜鸟教程,可以点击下方图片跳转)

    这里说明一下这三个运算符的优先级:not > and > or

    下面是练习实例,如果能很清楚的算出结果,那么逻辑运算这块没啥大问题。

    1) 6 or 2 > 1 
    2) 3 or 2 > 1 
    3) 0 or 5 < 4 
    4) 5 < 4 or 3 
    5) 2 > 1 or 6
    6) 3 and 2 > 1
    7) 0 and 3 > 1
    8) 2 > 1 and 3
    9) 3 > 1 and 0
    10) 3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2
    
    Answer:
    1)6 or True ==> 6 # 先处理比较运算符,再处理逻辑运算
    2)3 or True ==> 3
    3)0 or False ==> False
    4)False or 3 ==> 3
    5)True or 6 ==> True
    6)3 and True ==> True
    7)0 and True ==> 0
    8)True and 3 ==> 3
    9)True and 0 ==> 0
    10)True and 2 or True and 3 and 4 or True -> 2 or 3 and 4 or True ==> 2 

    ------以上是第二天的学习内容------

  • 相关阅读:
    PerfDog WEB端使用手册
    PerfDog4.0探索,支持用户自建web云
    无AI不测试:人工智能时代背景下,如何发展与应用自动化测试?
    性能测试实践 | PerfDog助力微信小游戏/小程序性能调优
    mysql面向过程学习
    阿里云操作记录
    socket网络编程
    学习慕课广告系统
    xinhuadouxxx总结
    maven+springboot+mybatis快速搭建简单使用
  • 原文地址:https://www.cnblogs.com/gandoufu/p/9260821.html
Copyright © 2020-2023  润新知