布尔类型 基本运算符 if判断

布尔类型

'''
作用:True与False,用于条件判断
定义:
tag=True
tag=False

print(10 > 3)
print(10 < 3)
'''

基本运算符

1、算数运算符
print(10 + 3)
print(10 - 3)
print(10 * 3)
print(10 / 3) # 保留小数部分
print(10 // 3) # 只保留整数部分
print(10 % 3) # 取余数,取模
print(10 ** 3)

2、比较运算符:
x=10
y=10
print(x == y) # =一个等号代表的是赋值

x=3
y=4
print(x != y) # 不等于

x=3
y=4
print(x > y) # False
print(x < y) # True
print(x >= y) # False
print(x <= y) # True

print(10 <= 10) # True

3、赋值运算符
age=18
age=age + 1 # 赋值运算
age+=1 # 赋值运算符,age=age+1
print(age)

age*=10 # age=age*10
age**=10 # age=age**10
age/=10 # age=age/10
age//=10 # age=age//10
age-=10 # age=age-10
print(age)

4、逻辑运算符
and: 逻辑与,and是用来连接左右两个条件,只有在左右两个条件同时为True,最终结果才为True,但凡有一个为False,最终结果就为False

print(10 > 3 and True)
print(10 < 3 and True and 3 > 2 and 1==1)

or:逻辑或,or是用来连接左右两个条件,但凡有一个条件为True,最终结果就为True,除非二者都为False,最终结果才为False
print(True or 10 > 11 or 3 > 4)
print(False or 10 > 11 or 3 > 4)
print(False or 10 > 9 or 3 > 4)

False or (True and True)
False or True
res=(True and False) or (10 > 3 and (3 < 4 or 4==3))
print(res)

not:把紧跟其后那个条件运算的结果取反
print(not 10 > 3)


False or (False and False)
False or False
res=(True and False) or (not 10 > 3 and (not 3 < 4 or 4==3))
print(res)
if判断
'''
代码块:
1、代码块指的是同一级别的代码,在python中用缩进相同的空格数(除了顶级代码块无任何缩进之外,其余代码块都是在原有的基础上缩进4个空格)来标识同一级的代码块

2、同一级别的代码块会按照自上而下的顺序依次运行
'''

'''
语法1:

if 条件: # 条件成立的情况下会运行子代码块
子代码1
子代码2
子代码3
...
'''
'''
print('sdsd')
x=1123
y=456
l=[1,2,3,]
print(x,y,l)
if 10 > 3:
print(1)
print('结束啦')

age = 73
age = 18
sex='female'
is_beautiful=True

if age > 16 and age < 20 and sex=='female' and is_beautiful:
print('开始表白。。。')

print('我是if之后的代码,是顶级代码')
'''
'''
语法2:
if 条件: # 条件成立的情况下会运行子代码块
子代码1
子代码2
子代码3
...
else: # 条件不成立的情况下会运行else包含的子代码块
子代码1
子代码2
子代码3
...
'''
'''
age = 73
# age = 18
sex='female'
is_beautiful=True

if age > 16 and age < 20 and sex=='female' and is_beautiful:
print('开始表白。。。')
else:
print('阿姨好,我们不太合适,还是做朋友吧。。。')

print('我是if之后的代码,是顶级代码')
'''
'''
语法3:
if 条件1: # 条件1成立的情况下会运行子代码块
子代码1
子代码2
子代码3
...
elif 条件2: # 条件2成立的情况下会运行子代码块
子代码1
子代码2
子代码3
...
elif 条件3: # 条件3成立的情况下会运行子代码块
子代码1
子代码2
子代码3
...
......
else: # 上述条件都不成立的情况下会运行else包含的子代码块
子代码1
子代码2
子代码3
...
'''
'''
示范: 如果:成绩>=90,那么:优秀

如果成绩>=80且<90,那么:良好

如果成绩>=70且<80,那么:普通

其他情况:很差

score=input('请输入您的分数进行查询:') # score="abc"
if score.isdigit(): # "99".isdigit()
score=int(score) # 把纯数字的字符串转换成整型,score=int("99")

if score >= 90:
print('成绩的级别为:优秀')
elif score >= 80:
print('成绩的级别为:良好')
elif score >= 70:
print('成绩的级别为:普通')
else:
print('成绩的级别为:很差')

else:
print('必须输入纯数字')
'''

猜你喜欢

转载自www.cnblogs.com/0B0S/p/12340953.html