Loading

day 6 可变不可变,if ,条件运算

1 可变不可变

1.21可变不可变类型

可变类型:值改变,id 不变,证明改的是原值,证明原值是可以被改变的

不可变类型:值改变,id 也变了,证明是产生新的值,压根没有改变原值,证明原值是不可以被修改的

1.2 验证

1.2.1 int 是不可变类型

 x=10

 print(id(x))

 x=11 # 产生新值

 print(id(x))

1.2.2 float 是不可变类型

 x=3.1

 print(id(x))

 x=3.2

 print(id(x))

1.2.3 str 是不可变类型

 x="abc"

 print(id(x))

 x='gggg'

 print(id(x

小结:int、float、str 都被设计成了不可分割的整体,不能够被改变

1.2.4 list 是可变类型

 l=['aaa','bbb','ccc']

 print(id(l))

 l[0]='AAA'

 print(l)

 print(id(l))

1.2.5 dict 是可变类型

 dic={'k1':111,'k2':222}

 print(id(dic))

 dic['k1']=3333333333

 print(dic)

 print(id(dic))

关于字典补充:

定义:{}内用逗号分隔开多 key : value,其中 value 可以是任意类型,但是 key 必须是不可变类型

 dic={
 'k1':111,
 'k2':3.1,
 'k3':[333,],
 'k4':{'name':'egon'}
 }
------------------------------
 dic={
 2222:111,
 3.3:3.1,
 'k3':[333,],
 'k4':{'name':'egon'}
 }

 print(dic[3.3])
 dic={[1,2,3]:33333333}
 dic={{'a':1}:33333333}

1.2.6 bool 不可变

2 流程控制if判断

语法 1:if

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

 if age > 16 and age < 20 and is_beautiful and star == '水平座':
 		print('我喜欢,我们在一起吧。。。')

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

语法 2:if...else

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

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

语法 3:if...elif

 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('普通')

语法 4:if...elif...else

3 条件逻辑运算

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

3.1.1第一大类:显式布尔值

条件可以是:比较运算符

age = 18

print(age > 16) # 条件判断之后会得到一个布尔值

条件可以是:True、False

is_beautiful=True

print(is_beautiful)

3.1.2 第二大类:隐式布尔值,所有的值都可以当成条件去用

其中 0、None、空(空字符串、空列表、空字典)=》代表的布尔值为 False,其余都为真

3.2 not、and、or 的基本使用

not:逻辑非

就是把紧跟其后的那个条件结果取反

注意:not 与紧跟其后的那个条件是一个不可分割的整体

 print(not 16 > 13)

 print(not True)

 print(not False)

 print(not 10)

 print(not 0)

 print(not None)

 print(not '')

and:逻辑与

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

 print(True and 10 > 3)

 print(True and 10 > 3 and 10 and 0) # 条件全为真,最终结果才为 True

 print( 10 > 3 and 10 and 0 and 1 > 3 and 4 == 4 and 3 != 3) # 偷懒原则

or:逻辑或

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

 print(3 > 2 or 0)

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

优先级 not>and>or

如果单独就只是一串 and 链接,或者说单独就只是一串 or 链接,按照从左到右的顺讯依次运算即可(偷懒原则)

如果是混用,则需要考虑优先级了

 res=3>4 and not 4>3 or 1==3 and 'x' == 'x' or 3 >3
 print(res)
 #False False False
-------------------------------------------------------
 res=(3>4 and (not 4>3)) or (1==3 and 'x' == 'x') or 3 >3
 print(res)
-------------------------------------------------------
res=3>4 and ((not 4>3) or 1==3) and ('x' == 'x' or 3 >3)
print(res)  

4 成员运算符 in

 print("egon" in "hello egon") # 判断一个字符串是否存在于一个大字符串中

 print("e" in "hello egon") # 判断一个字符串是否存在于一个大字符串中

 print(111 in [111,222,33]) # 判断元素是否存在于列表
---------------------------------------------------------
 判断 key 是否存在于字典

 print(111 in {"k1":111,'k2':222})

 print("k1" in {"k1":111,'k2':222})
---------------------------------------------------------
 not in

 print("egon" not in "hello egon") # 推荐使用

 print(not "egon" in "hello egon") # 逻辑同上,但语义不明确,不推荐

5 身份运算符 is

is # 判断的是 id 是否相等

posted @ 2021-12-03 09:42  maju  阅读(46)  评论(0)    收藏  举报