• python的变量与while循环


    格式化输出

      

    只需要把要打印的格式先准备好, 由于里面的 一些信息是需要用户输入的,你没办法预设知道,因此可以先放置个占位符,再把字符串里的占位符与外部的变量做个映射关系就好啦

    复制代码
    name = input("Name:")
    age = input("Age:")
    job = input("Job:")
    hobbie = input("Hobbie:")
    
    info = '''
    ------------ info of %s ----------- #这里的每个%s就是一个占位符,本行的代表 后面拓号里的 name 
    Name  : %s  #代表 name 
    Age   : %s  #代表 age  
    job   : %s  #代表 job 
    Hobbie: %s  #代表 hobbie 
    ------------- end -----------------
    ''' %(name,name,age,job,hobbie)  # 这行的 % 号就是 把前面的字符串 与拓号 后面的 变量 关联起来 
    
    print(info)
    复制代码

    %s就是代表字符串占位符,除此之外,还有%d,是数字占位符, 如果把上面的age后面的换成%d,就代表你必须只能输入数字.

    基本运算符

      

    以下假设变量:a=10,b=20

    比较运算

    以下假设变量:a=10,b=20

    赋值运算

    以下假设变量:a=10,b=20

    逻辑运算

      

    逻辑运算

    针对逻辑运算的进一步研究:

      1,在没有()的情况下not 优先级高于 and,and优先级高于or,即优先级关系为( )>not>and>or,同一优先级从左往右计算。

    例题:

    判断下列逻辑语句的True,False。

    复制代码
    1,3>4 or 4<3 and 1==1
    2,1 < 2 and 3 < 4 or 1>2 
    3,2 > 1 and 3 < 4 or 4 > 5 and 2 < 1
    4,1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8
    5,1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
    6,not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
    复制代码

      2 ,  x or y , x为真,值就是x,x为假,值是y

                 x and y, x为真,值是y,x为假,值是x

     

    例题:求出下列逻辑语句的值。

    8 or 4
    0 and 3
    0 or 4 and 3 or 7 or 9 and 6

    基本循环

     
    while 条件:
         
        # 循环体
     
        # 如果条件为真,那么循环体则执行
        # 如果条件为假,那么循环体不执行

    循环中止语句 

    如果在循环的过程中,因为某些原因,你不想继续循环了,怎么把它中止掉呢?这就用到break 或 continue 语句

    • break用于完全结束一个循环,跳出循环体执行循环后面的语句
    • continue和break有点类似,区别在于continue只是终止本次循环,接着还执行后面的循环,break则完全终止循环

    例子:break

    复制代码
    count = 0
    while count <= 100 : #只要count<=100就不断执行下面的代码
        print("loop ", count)
        if count == 5:
            break
        count +=1 #每执行一次,就把count+1,要不然就变成死循环啦,因为count一直是0
    
    print("-----out of while loop ------")
    复制代码

    输出

    复制代码
    loop  0
    loop  1
    loop  2
    loop  3
    loop  4
    loop  5
    -----out of while loop ------
    复制代码

    例子:continue

    复制代码
    count = 0
    while count <= 100 : 
        count += 1
        if count > 5 and count < 95: #只要count在6-94之间,就不走下面的print语句,直接进入下一次loop
            continue 
        print("loop ", count)
    
    print("-----out of while loop ------")
    复制代码

    输出

    复制代码
    loop  1
    loop  2
    loop  3
    loop  4
    loop  5
    loop  95
    loop  96
    loop  97
    loop  98
    loop  99
    loop  100
    loop  101
    -----out of while loop ------
    复制代码

    12.3,while ... else ..

    与其它语言else 一般只与if 搭配不同,在Python 中还有个while ...else 语句

    while 后面的else 作用是指,当while 循环正常执行完,中间没有被break 中止的话,就会执行else后面的语句

    复制代码
    count = 0
    while count <= 5 :
        count += 1
        print("Loop",count)
    
    else:
        print("循环正常执行完啦")
    print("-----out of while loop ------")
    复制代码

    输出

    复制代码
    Loop 1
    Loop 2
    Loop 3
    Loop 4
    Loop 5
    Loop 6
    循环正常执行完啦
    -----out of while loop ------
    复制代码

    如果执行过程中被break啦,就不会执行else的语句啦

    复制代码
    count = 0
    while count <= 5 :
        count += 1
        if count == 3:break
        print("Loop",count)
    
    else:
        print("循环正常执行完啦")
    print("-----out of while loop ------")
    复制代码

    输出

    Loop 1
    Loop 2
    -----out of while loop ------
  • 相关阅读:
    第二个Sprint
    第一个Sprint
    “尸体解剖” 报告
    软工水平自我评价表
    小学四则运算APP 第一个冲刺阶段 第五天
    小学四则运算APP 第一个冲刺阶段 第四天
    小学四则运算APP 第一个冲刺阶段 第三天
    小学四则运算APP 第一阶段冲刺 第二天-补
    小学四则运算APP 第一个冲刺 第二天
    小学四则运算APP 第一个冲刺阶段 第一天
  • 原文地址:https://www.cnblogs.com/journey-mk5/p/9382727.html
Copyright © 2020-2023  润新知