运算符 if和while的使用

运算符:

1)算术运算符

+ - * / %(取余) //(地板除,取整)**(幂运算) ,返回一个值

2)比较运算符

3) > >= < <= ==(比较值是否相等) !=(比较值是否不相等) ,返回一个布尔值

4) 赋值运算符

= += -= *= /= **=

5)逻辑运算符(把多个条件同时叠加)

and or not  ,加括号优先级最高

6)身份运算符(id)

x= 257
y = x
z= 257

print(id(x) == (id(y)))  # True
print(x is y) # is比较的是内存地址 True
print(x is not y)  # is not判断是否不等于
print(not x is y) #False
print(id(x) == id(z)) #False
print(x is z) #False

7) 位运算符

60 13  十进制 0,1,2,3,4,5,6,7,8,9,10
0,1,2,3,4,5,6,7,8,9,逢十进一位,10,11,12,13,...19,20...90,91,92,..99,100
0和1 二进制

0,1,逢二进一位,10,11,100,101,111,1000

0 # 0000 0000 --》0
1 # 0000 0001 --》 1
10 # 0000 0010 --》 2
11 # 0000 0011 --》 3
100 # 0000 0100 --》 4
101 --》 5
110 --> 6
111 --> 7
0100 0011 -->
方法一,计算器:67
方法二:手工计算
9892 == 2*10**0 + 9*10**1 + 8*10**2 + 9*10**3
print(2*10**0 + 9*10**1 + 8*10**2 + 9*10**3)
01000011 == 1*2**0 + 1*2**1 + 0 + 0 + 0 + 0 + 1*2**6 + 0
print(1*2**0 + 1*2**1 + 0 + 0 + 0 + 0 + 1*2**6 + 0)

8)成员运算符:判断元素是否在容器类元素里面(字符串)
class_student_lt = ['s1','s2','s3']
print('s1' in class_student_lt) # True
print('s1' not in class_student_lt) # False
print('s4' in class_student_lt) # False
s = 'nick'
print('n' in 'nick')

2. 流程控制之if判断

多分支结构1:

if 条件1:
    code1 条件1成立执行code1
elif 条件2:
    code2 条件1不成立条件2成立执行code2
elif 条件3:
    code3 条件1和2不成立,条件3成立执行code3
elif可以有无限个。。。
    coden
else:
    code4 所有条件都不成立,执行code4
 1 # elif事例
 2 height=input('请输入你的身高》》》:').strip()
 3 height=int(height)
 4 if height>130:
 5     print('全票')
 6 elif height>70:
 7     print('半票')
 8 elif height>30:
 9     print('可能你需要付费一点点!')
10 else:
11     print('免费')

多分支结构1:

if 条件1:
    code1 条件1成立执行code1,再判断下面的if条件是否成立
if 条件2:
     code2 条件2成立执行code2,再判断下面的if条件是否成立
if 条件3:
    code3 条件3成立执行code3,再判断下面的if条件是否成立
if可以有无限个。。。
    coden
else:
    code4 所有条件都不成立,执行code4
# if 事例

height=input('请输入你的身高》》》:').strip()
height=int(height)
if height>130:
    print('全票')
if height>70 and height<=130:
    print('半票')
if height>30 and height<=70:
    print('可能你需要付费一点点!')
else:
    print('免费')

多个同一级别if和elif建立多分支结构的区别:

如果程序中判断事件过多,全部用if的话,会遍历整个程序,用elif程序运行时,只要if或后续某一个elif之一满足逻辑值为True,则程序执行完对应输出语句后会自动结束该轮if—elif,即不会再冗余地执行后续的elif或else,提高了程序的运行效率。

3.流程控制之while循环

break:结束当前层while循环,continue:跳出本次while循环,进入下一次循环

while 条件:# 条件成立运行代码,不成立结束while循环

  代码  # 代码执行结束后会进入下一次循环(再一次判断条件)

 1 # while+条件
 2 
 3 
 4 count=0
 5 t_age=18
 6 while count<3:
 7     # if count==3:
 8     #     print('不好意思,只有三次机会!')
 9     #     break
10     age=input('请输入你的年龄:')
11     age=int(age)
12     if age>t_age:
13         print('太大了')
14     elif age<t_age:
15         print('太小了')
16     else:
17         print('恭喜你猜对了!')
18     count += 1
 1 # while + break
 2 
 3 
 4 count = 0
 5 while 1:
 6     if count == 100:
 7         break  # break终止循环
 8     count += 1
 9     print(count)
10 
11 print('bzr')
# while + continue 不打印50

count = 0
while 1:
    if count == 100:
        break  # break终止循环
    count += 1
    if count == 50:
        continue  # continue跳出本次循环,不执行下面的代码
    print(count)

print('bzr')
# 打印1-100内偶数(不包括[22,46,68,98])的和
# 分解题目


count = 0
sum_count = 0
while True:

    if count == 100:
        break

    count += 2
    if count in [22, 46, 68, 98]:
        continue
    sum_count += count
print(sum_count)
# while + else 仅作了解(非用不可可以使用,不要和if。。else混了)

count = 0
while count < 100:
    count += 1
    print(count)
else:
    print('没有被break干掉我就能出来')


#打印结果:除了1~100之外,没有break掉,else内print也能打印
# while + else
count=0
while True:
    count+=1
    print(count)
    if count==100:
        break
else:  # 没有被break干掉就执行,被break终止了就不执行了
    print('没有被break干掉我就能出来')  # 可以判断while是否被break终止


# 打印结果: else 内的print不能打印,上面有break终止循环了。
 1 #  猜年龄游戏 只有三次机会
 2 
 3 count=0
 4 t_age=18
 5 while count<3:
 6     age=input('请输入你的年龄:')
 7     age=int(age)
 8     if age>t_age:
 9         print('太大了')
10     elif age<t_age:
11         print('太小了')
12     else:
13         print('恭喜你猜对了!')
14     count += 1


 

原文地址:https://www.cnblogs.com/zhangchaocoming/p/11508988.html