Python-day6-流程控制之条件判断

流程控制之条件判断

条件判断

显式布尔值:True/False

隐式布尔值:所有数据类型,其中0、None、空为假

PS:空不是空格,是没有占位符

not

取反其结果的反向结果

# 不加not
>>> print(2 > 1)
True

# 加上not则结果取反
print(not 2 > 1)
>>> print(not 2 > 1)
False

and

**逻辑与:**多个条件连接,全为真则为真,一个为假则为假

# 条件全为真
>>> print(2 > 1 and 3 > 1 and 4 > 1)
True

# 条件有一个为假,后面的条件就直接不用判断了
>>> print(2 > 1 and 3 > 1 and 4 > 1)
False

or

**逻辑或:**多个条件连接,一个为真则为真,都为假则为假

# 条件全为真
>>> print(2 > 1 or 3 > 1 or 4 > 1)
True

# 条件一个为真
>>> print(1 > 2 or 3 > 1 or 4 > 1)
True

# 条件全为假
>>> print(1 > 2 or 1 > 3 or 1 > 4)
False

逻辑运算符与优先级

not > and > or

如果单独只是一串and/or连接,则只需从左到右判断即可;

如果混着用,则需考虑优先级;

# 例
3 > 4 and not 4 > 3 or 1 == 3 and 'x' == 'x' or 3 > 3

# 用括号标出优先级,进行分级,此时括号外只剩下or,很容易分辨
(3 > 4 and (not 4 > 3)) or (1 == 3 and 'x' == 'x') or 3 > 3

# 1.根据优先级提取判断:
# (3 > 4 and (not 4 > 3)) 
not 4 > 3		# True
3 > 4				# False
True and False	# False

# (1 == 3 and 'x' == 'x')
1 == 3			# False
x == x			# True
False and True	# False

# 3 > 3
3 > 3 			# False

# 2.拼接结果
False or False or False		# False

# 3.运行测试
>>> print(3 > 4 and not 4 > 3 or 1 == 3 and 'x' == 'x' or 3 > 3)
False
>>> print((3 > 4 and (not 4 > 3)) or (1 == 3 and 'x' == 'x') or 3 > 3)
False

成员运算与身份运算

成员运算符

in

判断某个值是否在某个字符串、列表、字典中

# 字符串
print("Peng" in "Hello Peng")		# True
print("P" in "Hello Peng")			# True
print("p" in "Hello Peng")			# False,大小写必须一致

# 列表
print(111 in [111,222,333])			# True
print(1 in [111,222,333])				# False(111为一个整体)

# 字典
print(111 in {'k1':111,'k2':222,'k3':333})	# False(判断key,不判断value)
print('k1' in {'k1':111,'k2':222,'k3':333})	# True

not in

判断某个值是否不在某个字符串、列表、字典中

# 字符串
print("Peng" not in "Hello Peng")		# False
print("P" not in "Hello Peng")			# False
print("p" not in "Hello Peng")			# True

# 列表
print(111 not in [111,222,333])			# False
print(1 not in [111,222,333])				# True

# 字典
print(111 not in {'k1':111,'k2':222,'k3':333})	# True
print('k1' not in {'k1':111,'k2':222,'k3':333})	# False

# PS:虽然效果相同,但推荐使用 not in,这样会更便捷
print("Peng" not in "Hello Peng")	# False
print(not "Peng" in "Hello Peng")	# False

身份运算符

is

判断一个值是否为另一个值

a = 10
b = 10
print(a is b)		# True

a = 10
b = 11
print(a is b)		# False

if判断

判断一个条件是否成立,并基于条件做进一步的处理

代码块缩进,是基于if判断条件之下,乱缩进则会报错

单分支

单个判断条件

条件符合

age = 18
is_beautiful = True
gender = 'female'

if age > 16 and age < 20 and is_beautiful and gender == 'female':
  print('我们在一起吧...')
print('其他顶级代码。。。')
MacBook-Pro:~ Peng$ python3 1.py
我们在一起吧...
其他顶级代码。。。

条件不符合

若将age定义60,则不会运行if内的代码

MacBook-Pro:~ Peng$ python3 1.py
其他顶级代码。。。

双分支

两个判断条件

条件符合

age = 18
is_beautiful = True
gender = 'female'

if age > 16 and age < 20 and is_beautiful and gender == 'female':
  print('我们在一起吧...')
else
	print('阿姨拜拜...')

print('其他顶级代码。。。')
MacBook-Pro:~ Peng$ python3 1.py
我们在一起吧...
其他顶级代码。。。

条件不符合

若将age定义60,则不会运行if内的代码

MacBook-Pro:~ Peng$ python3 1.py
阿姨拜拜...
其他顶级代码。。。

多分支

多个判断条件

条件符合

此时若输入的成绩小于70,则什么都不会打印

# 案例:查成绩
score = input('请输入您的成绩: ')
score = int(score)
if score >= 90:
  print('秀儿')
elif score >= 80:
  print('良好')
elif score >= 70:
  print('普通')
MacBook-Pro:~ Peng$ python3 1.py
请输入您的成绩: 90
秀儿
MacBook-Pro:~ Peng$ python3 1.py
请输入您的成绩: 80
良好
MacBook-Pro:~ Peng$ python3 1.py
请输入您的成绩: 70
普通
MacBook-Pro:~ Peng$ python3 1.py
请输入您的成绩: 60

条件不符合

优化查询除了if、elif条件之外的条件,添加else条件

PS:此语法也是if判断的最完整的语法

# 案例:查成绩
score = input('请输入您的成绩: ')
score = int(score)
if score >= 90:
  print('秀儿')
elif score >= 80:
  print('良好')
elif score >= 70:
  print('普通')
else:
	print('锤子')
MacBook-Pro:~ Peng$ python3 1.py
请输入您的成绩: 60
锤子

if嵌套

在某个if条件内嵌套一层或多层if判断条件

# 案例:查成绩
score = input('请输入您的成绩: ')
score = int(score)
if score >= 90:
  if score <= 100:
    print('秀儿')
  elif score >= 100:
    print('假的,肯定是假的!')
elif score >= 80:
  print('良好')
elif score >= 70:
  print('普通')
else:
	print('锤子')
MacBook-Pro:~ Peng$ python3 1.py
请输入您的成绩: 100
秀儿
MacBook-Pro:~ Peng$ python3 1.py
请输入您的成绩: 101
假的,肯定是假的!
posted @ 2022-06-09 18:26  秋风お亦冷  阅读(36)  评论(0编辑  收藏  举报