flag=true python_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="-")   # \n  \r\n \r

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_

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值