一、 可变不可变类型
可变类型(字典、列表)
值改变,id(堆中对应的内存地址)不变,说明改的是原值
不可变类型(int float str bool)
值改变了,id也改变了,证明产生的是新的值,没有改变原值,证明原值是不可以被修改的
# int float 均为不可变类型
X = 10
print(id(x))
X = 12
print(id(x))
验证
列表的元素是可变的(仅限通过元素位置重新赋值的方式)
# 1、列表类型
list1 = ['aaa', 12, 'okk']
print(id(list1))
list1 = ['bbb', 12, 'okk']
print(id(list1))
list1[0]='ccccc'
print(list1)
print(id(list1))
运行结果
字典验证
# 2、字典类型
dic = {
'name':"paiapi",
'age':18,
'like':'eat'
}
print(dic)
print(id(dic))
dic['name']='Achao'
print(dic)
print(id(dic))
执行结果
bool不可变,仅字典跟列表这样的容器类型可变
关于字典补充
字典的key一般都是字符串(key必须是不可变的)
一般来说,字典的key都是str类型,再不济是整数,但必须要有一个底线,字典的key必须是不可变类型的,否则会报错
user_info = {
'name': "paipai",
[1, 2, 3]: 'ahha',
'age': 18
}
print(user_info)
验证截图
字典、列表都是可变类型,可变类型是不可哈希的,不可变类型是可以哈希的;
二、条件
条件:要满足一件事必须要做什么事情的前提下才可以?
1、显式布尔值
1.1 条件可以是比较运算符
age=18
print(age>16) #条件判断之后得到一个布尔值
2.2 条件可以是:true false
is_beautiful=true
print(is_beautiful)
2、隐式布尔值
所有的值都可以当成条件去用
其中,0, None,Null(空字符串、空列表、空字典)代表的布尔值为false,剩余的值代表的都为真,int、float、str,正常列表,正常字典均为真
三、逻辑运算符与优先级
1、逻辑运算符基本使用(not and or)
运算符 | 解释说明 |
---|---|
not | 把紧跟其后的条件结果取反,注意,not与紧跟其后的条件是一个不可分割的整体 |
and | 逻辑与,and用来连接左右两个条件,左右两个条件同时为true,最终结果才为true |
or | 逻辑或,用来连接左右两个条件,当左右两个条件其中一个为true,无论另外一个的结果是什么,最终结果都为true |
# 逻辑运算符==========
# 1、not
print(not 18 > 19)
print(not 18 < 19)
print(not 0)
print(not 12)
print(not None)
print(not '')
# 2、and
print(1 < 2 and 2 < 3)
print(1>2 and 2<3)
# 3、or
print(1>2 or 2<3)
print(1>2 or 2>3)
print(1<2 or 2<3)
2、优先级
当出现三个逻辑运算符混用时,按照 not>and>or的优先级进行执行
若只出现一种表达式,则从做到右执行
res=3>4 and not 4>3 or 1==3 and 'x'=='x' or 3 >3
执行结果
四、 成员运算符(in/not in)
# 成员运算符
print('pai' in 'paipai')
print('p' in 'paipai')
print('p' in ['paipai', 111, 222])
print('paipai' in ['paipai', 111, 222]) # 判断元素是否在列表中
print('p' not in 'paipai')
print('A' not in 'paipai')
print(
'name' in
{
'age': 18,
'name': 'paipai',
'like': 'eat'
}
)#判断key是否在字典中,判断value同理
print(
'name' not in
{
'age': 18,
'name': 'paipai',
'like': 'eat'
}
)#判断key是否在字典中
print(
'eat' in
{
'age': 18,
'name': 'paipai',
'like': 'eat'
}
) #判断key是否在字典中
无法通过过value判断值在在字典中
print(111 in {"k1": 111, 'k2': 222})
print(111 not in {"k1": 111, 'k2': 222})
📢 无法通过value值判断值是否在字典中
五、 身份运算符
is 判断id是否相等
六、if 判断
语法1
if 条件:
代码1
代码2
print xxxxx
……
其它代码块
age = 60
is_beautiful = True
star = ’水瓶座‘
if age>16 and age<20 and is_beautiful and star = '水瓶座':
print("like you")
执行结果
为了表达print跟if是在同一个代码块中,print是必须要缩进2行的
语法2
age = 21
is_beautiful = True
star = "水瓶座"
if 16 < age < 20 and is_beautiful and star == "水瓶座":
print("like you")
else:
print("sorry,see you latter");
print("继续执行后续的代码")
执行结果
语法3
正常语法
score = 73
if score >= 90:
print("优秀")
elif score>=80 and score<90:
print("良好")
elif score>=70 and score<80:
print("中等")
elif score>60:
print("及格")
print("继续执行其它代码")
优化
score = 99
if score >= 90:
print("优秀")
elif score>=80 :
print("良好")
elif score>=70 :
print("中等")
elif score>60:
print("及格")
print("继续执行其它代码")
语法4(最完整)
’‘’
if 条件1:
代码1
代码2
elif 条件2:
代码1
代码2
……
else:
代码1
代码2
'''
# 继续优化:增加上与用户的联动
score = input("请输入你的成绩:")
score = int(score) # 确保用户输入的int类型字符串
if score > 90:
print("优秀")
elif score > 80:
print("良好")
elif score > 70:
print("中等")
elif score > 60:
print("差点就不及格了")
else: # 如果上述的条件都不满足,就执行else语句
print("朋友,你挂科了")
print("继续执行后面的代码")
执行截图
if嵌套
# if 嵌套if
age = 18
is_beautiful=True
star = '水瓶座'
if 16< age <30 and is_beautiful and star=='水瓶座':
print("i like you")
answer=input("你愿意吗:")
answer1=answer
if answer1=="愿意":
print("圆满结局")
else:
print("很遗憾,失败了")
else:
print('sorry see you latter')
print("=======继续执行其它代码啦")
执行结果