zoukankan      html  css  js  c++  java
  • python04 while循环、flag、break、continue

    1 运算符

    2 表达式和运算符

    算数运算符:+-*/ //整除  %取余 **次方

    比较运算符:>< >=  <= !=不等于 ==等于     #单个等于表示赋值

    赋值运算符:=  += -= *=  /= //=整除等于 %=取余等于 **=次方等于


    num+=2      num=num+2 #等价于
    num-=2       num=num-2
    num*=2        num=num*2
    num/=2        num=num/2
    num//=2       num=num//2
    num%=2        num=num%2
    num**=2        num=num**2

    逻辑运算符:and  or  not      #没有优先级,实际操作中用()来添加优先级。

    条件1  and  条件2

    条件1  or  条件2

    短路原则:对于and,如果前面一个为假,那么这个and前后两个条件组成的表达式的计算结果就一定为假,第二个条件就不会被计算。

                     对于or,如果前面一个为真,那么这个or前后两个条件组成的表达式的计算结果就一定为真,第二个条件就不会被计算。

    表达式与运算符:

    什么是表达式?3+4*5就是表达式,这里的加号、乘号就是运算符,3、4、5就是操作数。三个数结果等于23.  3+4*5=23.  我们可以将计算结果保存在一个变量里,如number=3+4*所以 表达式即使操作数和运算符组成的一句代码或者语句,表达式可以求值,可以放在“=”右边,用来给变量赋值。.

    找出三个数中的最大值:

    a=input("number1:")
    b=input("number2:")
    c=input("number3:")

    if int(a)>int(b):
    if int(a)>int(c):
      print("a is the biggest")
    else:
      print("c is the biggest")

    else:
    int(b)>int(a)
    if int(b)>int(c):
      print("b is the biggest")
    else:
      print("c is the biggest!")

    #里面有太多int转换,直接把它在定义的时候就转换如下:

    a=int(input("number1:"))
    b=int(input("number2:"))
    c=int(input("number3:"))
    if a>b:
    if a>c:
      print("a is the biggest")
    else:
      print("c is the biggest")

    else:
    b>a
    if b>c:
      print("b is the biggest")
    else:
      print("c is the biggest!")

    用while来实现循环。

    age=66
    #guess_age=int(input("your age: "))


    while True:
    guess_age=int(input("your age: "))
      if guess_age == age:
        print("yes")
        break #终止
      elif guess_age>age:
        print ("is bigger")
      else:
        print("is smaller")

    print("end")


    flag = True

    while flag:
      guess_age=int(input("your age: "))
      if guess_age == age:
        print("yes")
        flag=False
      elif guess_age>age:
        print ("is bigger")
      else:
        print("is smaller")

    print("end")

    # continue(满足条件就跳过)
    num = 1
    while num<=10:
      num += 1
      if num == 3:
        continue
      print(num)

    输出结果:

    2
    4
    5
    6
    7
    8
    9
    10
    11

    # continue
    num = 1
    while num<=10:
    num += 1
    if num == 3:
    continue
    print(num)

    else:         #只有break中止的时候不执行
    print("this is else statement")

    输出内容为:

    2
    4
    5
    6
    7
    8
    9
    10
    11
    this is else statement

    # continue
    num = 1
    while num<=10:
      num += 1
      if num == 5:
        break
      print(num)

    else:           #只有break中止的时候不执行
      print("this is else statement")

    输出结果为

    2
    3
    4

    while 条件:
    ....
    else:
    ....

      

    print("helloworld")
    print("helloworld")
    print("helloworld")

    输出结果为:

    helloworld
    helloworld
    helloworld

    print("helloworld",end="-")   #  
    print("helloworld",end="-")
    print("helloworld",end="-")

    输出结果为:

    helloworld-helloworld-helloworld-

    while条件1

      ......

      while 条件2  

      ......

    num1=0
    while num1<=5:
    print(num1,end="-")
    num2=0
    while num2<=7:
    print(num2,end="_")
    num2+=1
    print()
    num1+=1

    输出结果为:

    1-0_1_2_3_4_5_6_7_
    2-0_1_2_3_4_5_6_7_
    3-0_1_2_3_4_5_6_7_
    4-0_1_2_3_4_5_6_7_
    5-0_1_2_3_4_5_6_7_

  • 相关阅读:
    SpringBoot 2.X整合Mybatis
    SpringBoot 2.X集成 jdbc自动配置原理探究
    15 道 Spring Boot 高频面试题,看完直接当面霸【入门实用】
    SpringBoot整合Thymeleaf-基于SpringBoot2.X版本
    vue-manage-system 后台管理系统开发总结
    Node.js 应用:Koa2 使用 JWT 进行鉴权
    Parcel:常见技术栈的集成方式
    HTML5 桌面通知:Notification API
    Electron 实战桌面计算器应用
    Node.js 入门:Express + Mongoose 基础使用
  • 原文地址:https://www.cnblogs.com/minkillmax/p/7822364.html
Copyright © 2011-2022 走看看