Day6 可变不可变类型,条件,逻辑、成员、身份运算符 if 判断
1.可变不可变类型
可变类型:值改变,ID不变。(int float str bool)
不可变类型:值改变,ID也随之改变(list dict(字典里的key必须是不可变类型,valuekey是随便类型))
2.条件
第一大类:显式布尔值
①比较运算符
age = 18
print(age > 16) # 条件判断之后会得到一个布尔值
②true false
is_beautiful=True
print(is_beautiful)
第二大类:隐式布尔值
0 None 空(空字符串 空列表 空字典)=====>代表的为false 其余的为真
3.逻辑运算符
⒊.1 not and or的基本使用
not:将紧跟其后的那个条件结果取反
and:逻辑与,and用来链接左右两个条件,两个条件同时为True,最终结果才为真
or:逻辑或,or用来链接左右两个条件,两个条件但凡有一个为True,最终结果就为True,
两个条件都为False的情况下,最终结果才为False
3.2 优先级 not>and>or
res=3>4 and not 4>3 or 1==3 and 'x' == 'x' or 3 >3
与
res=(3>4 and (not 4>3)) or (1==3 and 'x' == 'x') or 3 >3
是一样的
4.成员运算符
#判断一个字符串是否存在一个大字符串中
print('lrr' in 'lrr is good')
5.身份运算符
is #判断ID是否相等
6.流程控制之if判断
语法1
if条件:
代码1
代码2
代码3
语法2
if 条件:
代码1
代码2
else
代码1
代码2
语法3
if 条件:
代码1
代码2
elif
代码1
代码2
elif
代码1
代码2
....(elif的数目不做限制)
else
代码1
代码2
例:
score = input('请输入您的成绩:') # score="18"
score=int(score)
if score >= 90:
print('优秀')
elif score >= 80:
print('良好')
elif score >= 70:
print('普通')
else:
print('很差,小垃圾')
print('=====>')
温故知新(知新部分)
格式化输出
# print('my name is %s age is %s' %('egon',18))
# print('成功的概率 %s%% ' % (97,)) %用‘%%’
# x='egon'
# y=18
# res=f'name:{{{x}}} age {y}'
# print(res) {}用‘{{}}’
# format新增(了解):
# print('{x}=============='.format(x='开始执行')) # 开始执行******
开始执行==============
# print('{x:=<10}'.format(x='开始执行')) # 开始执行******
======开始执行
# print('{x:=>10}'.format(x='开始执行')) # 开始执行******
开始执行======
# print('{x:=^10}'.format(x='开始执行')) # 开始执行******
===开始执行===
# 四舍五入
# print('{salary:.3f}'.format(salary=1232132.12351)) #精确到小数点后3位,四舍五入,结果为:1232132.124
# 了解f的新用法:{}内的字符串可以被当做表达式运行
# res=f'{10+3}'
# print(res)
# f'{print("aaaa")}'
浙公网安备 33010602011771号