【2020Python修炼记10】Python语法入门[7]_流程控制,


一、条件

 灵魂三问:

什么是条件?什么可以当做条件?为何要用条件?

1、 条件,就是判断依据,判断前提……

2、条件的类型

# 第一大类:显式布尔值

条件可以是:比较运算符       # age = 18 # print(age > 16)  # 条件判断之后会得到一个布尔值

条件可以是:True、False     # is_beautiful=True # print(is_beautiful)

 

# 第二大类:隐式布尔值

所有的值都可以当成条件去用     # 其中0、None、空(空字符串、空列表、空字典),代表的布尔值为False

其余都为True

 3、为何要用条件

 条件有助于程序按照人的思想去执行指令,得出人想要的结果。也能够提高代码的效率和价值。

 

二、逻辑运算符:not、and、or

注意:(不是 not at all)

1、not、and、or的基本使用

# not:就是把紧跟其后的那个条件结果取反
# ps:not与紧跟其后的那个条件是一个不可分割的整体

>>> print(not 16 > 13)
False
>>> print(not True)
False
>>> print(not False)
True
>>> print(not 10)
False
>>> print(not 0)
True
>>> print(not None)
True
>>> print(not '')#'' 空值
True
>>> 

# and:逻辑与,and用来链接左右两个条件,两个条件同时为True,最终结果才为真

>>> print(True and 10 > 3)
True
>>> print(True and 10 > 3 and 10 and 0) # 条件全为真,最终结果才为True
0
>>> print( 10 > 3 and 10 and 0 and 1 > 3 and 4 == 4 and 3 != 3)  # 偷懒原则
0
>>> 

# or:逻辑或,or用来链接左右两个条件,两个条件但凡有一个为True,最终结果就为True,
#            两个条件都为False的情况下,最终结果才为False

>>> print(3 > 2 or 0)
True
>>> print(3 > 4 or False or 3 != 2 or 3 > 2 or True) # 偷懒原则
True
>>> 

三句话总结:

not 就是:真变假,假变真

and 就是:全真为真,一假即假

or 就是:一真即真,全假为假 

2、优先级  not>and>or

# ps:
# 如果单独就只是一串and连接,或者说单独就只是一串or连接,按照从左到右的顺讯依次运算即可(偷懒原则)
# 如果是混用,则需要考虑优先级了

>>> res=3>4 and not 4>3 or 1==3 and 'x' == 'x' or 3 >3
>>> print(res)
False
>>> #       False                 False              False
>>> res=(3>4 and (not 4>3)) or (1==3 and 'x' == 'x') or 3 >3
>>> print(res)
False
>>> #规范表达,尽量用and连接
>>> res=3>4 and ((not 4>3) or 1==3) and ('x' == 'x' or 3 >3)
>>> print(res)
False
>>>

三、成员运算符,身份运算符

1、成员运算符  in

>>> print("egon" in "hello egon") # 判断一个字符串是否存在于一个大字符串中
True
>>> print("e" in "hello egon") # 判断一个字符串是否存在于一个大字符串中
True
>>> print(111 in [111,222,33]) # 判断元素是否存在于列表
True
>>> # 判断key是否存在于字典
>>> print(111 in {"k1":111,'k2':222})
False
>>> print("k1" in {"k1":111,'k2':222})
True
>>> # not in
>>> print("egon" not in "hello egon") # 推荐使用
False
>>> print(not "egon" in "hello egon") # 逻辑同上,但语义不明确,不推荐
False
>>> 

2、身份运算符  is

>>> 
>>> #is # 判断的是id是否相等
>>> x=10
>>> y=23
>>> x is y
False
>>> 

 

四、流程控制

1、if  条件判断

 

语法一:if

 

if 条件:

(四个空格)代码1

(四个空格)代码2

(四个空格)代码3

 (代码前的空格,是代码等级的区分标志) 

# print(1)
# print(2)
# print(3)
# if 条件:
#     代码1
#     代码2
#     代码3
# print(4)
# print(5)
>>> 
>>> age = 60
>>> is_beautiful = True
>>> if (age > 16 and age < 20 and is_beautiful):
        print('我喜欢,我们在一起吧')
    print('其他代码.............')
>>> 

 

语法二:if-else

 

if(条件):

(四个空格)代码1

else:

(四个空格)代码2 

if 条件:
    代码1
    代码2
    代码3
else:
    代码1
    代码2
    代码3


#
age = 60 # is_beautiful = True # star = '水平座' # # if age > 16 and age < 20 and is_beautiful and star == '水平座': # print('我喜欢,我们在一起吧。。。') # else: # print('阿姨好,我逗你玩呢,深藏功与名') # # print('其他代码.............')

语法三:if---elif

 

if(条件):

(四个空格)代码1

elif(条件):

(四个空格)代码2

elif(条件):

(四个空格)代码2

 

 ——elif(条件)——上一条件不满足时,才会继续执行 elif(条件),else的命令

'''
语法3:
if 条件1:
    代码1
    代码2
    代码3
elif 条件2:
    代码1
    代码2
    代码3
elif 条件3:
    代码1
    代码2
    代码3
'''
# score=73
# if score >= 90:
#     print('优秀')
# elif score >= 80 and score < 90:
#     print('良好')
# elif score >= 70 and score < 80:
#     print('普通')

# 改进
# score = input('请输入您的成绩:') # score="18"
# score=int(score)
#
# if score >= 90:
#     print('优秀')
# elif score >= 80:
#     print('良好')
# elif score >= 70:
#     print('普通')
'''
语法3:
if 条件1:
    代码1
    代码2
    代码3
elif 条件2:
    代码1
    代码2
    代码3
elif 条件3:
    代码1
    代码2
    代码3
...
else:
    代码1
    代码2
    代码3
'''
# score = input('请输入您的成绩:') # score="18"
# score=int(score)
#
# if score >= 90:
#     print('优秀')
# elif score >= 80:
#     print('良好')
# elif score >= 70:
#     print('普通')
# else:
#     print('很差,小垃圾')
#
# print('=====>')

语法四:if 嵌套if

age = 17
is_beautiful = True
star = '水平座'

if 16 < age < 20 and is_beautiful and star == '水平座':
    print('开始表白。。。。。')
    is_successful = True
    if is_successful:
        print('两个从此过上幸福的生活。。。')
else:
    print('阿姨好')

print('其他代码.............')

 

2、for——(下回分解)

 

3、while

 

相关内容

    暂无相关文章

评论关闭