• Python基础


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

    while 条件:

        代码块

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

    代码1:

    while True:
        print('Hello world')

    代码2:

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

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

    举个一般的栗子:

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

    2、continue语句

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

    代码段:

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

    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('Wrong user name, try again')

    从执行结果可以看出,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循环正常结束')

    代码块2:

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

    从代码块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)

    format的用法:

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

    输出结果:

    my name is jason
    i'm %s years 25
    i'm %d years old25

    三、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
     
  • 相关阅读:
    开发进度1
    人月神话阅读笔记(1)
    仓库管理系统
    课程管理系统教程
    流与文件课后作业2加解密工作
    流与文件课后作业3
    今日学习时间记录
    Java字符串切片
    java_ 集合
    json基础教程|理解Json
  • 原文地址:https://www.cnblogs.com/shuai1993/p/9273773.html
Copyright © 2020-2023  润新知