5月29日 学习笔记 可变不可变类型 成员运算符和身份运算符 条件 流程控制之if判断

# 可变不可变类型

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

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

# 2、验证
# 整型:不可变类型
# x=10
# print(id(x)) #140719883294656
# x=11 #产生新值
# print(id(x))#140719883294688

# 2.2 flot:不可变类型
# x=3.1
# print(id(x))
# x=3.2
# print(id(x))

# 2.3 str:不可变类型

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

# 2.4 list 是可变类型
# e=['aaa','bbb','ccc']
# print(id(e))
# e[0]='AAA'
# print(e)
# print(id(e))

# 2.5 dict是可变类型
# dic={'k1':'111','k2':'222'}
# print(id(dic))
# dic['k1']=33333
# print(dic)
# print(id(dic))

# 2.6 bool 不可变类型


# 关于字典的补充:
# :{}内用逗号分隔开key:value,其中value可以是任意类型 但是key必须是不可变类型
# 不可哈希类型不可以当key



# 1、成员运# print('egon' in 'hello egon') #判断一个字符串是否存在于一个大字符串中
# print('e' in 'hello  egon') #判断一个字符串是否存在于一个大字符串中

#print(111 in [111,222,333]) #判断元素是否在列表里 #True

# 判断Key是否存在于字典
# print(111 in {'k1':111,'k2':222,'k3':333}) #False
# print('k1' in {'k1':111,'k2':222,'k3':333}) #True

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






# 2、身份运算符
# is # 判断id是否相等

# 什么是条件?为何要用条件?什么可以当做条件?
# 条件可以是:比较运算符
# age=18
# print(age>16) #条件判断后会得到一个布尔值

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

# 第二大类:隐式布尔值,所有的值都可以当做条件去用
# 其中0、None、空为假(空字符串、空列表、空字典)=》代表的布尔值为false,其余的类型都为真
# 空(假): "" 、[] 、{} 空格也算是字符

# 什么是条件?为何要用条件?什么可以当做条件?
# 条件可以是:比较运算符
# age=18
# print(age>16) #条件判断后会得到一个布尔值

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

# 第二大类:隐式布尔值,所有的值都可以当做条件去用
# 其中0、None、空为假(空字符串、空列表、空字典)=》代表的布尔值为false,其余的类型都为真
# 空(假): "" 、[] 、{} 空格也算是字符
# 什么是条件?为何要用条件?什么可以当做条件?
# 条件可以是:比较运算符
# age=18
# print(age>16) #条件判断后会得到一个布尔值

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

# 第二大类:隐式布尔值,所有的值都可以当做条件去用
# 其中0、None、空为假(空字符串、空列表、空字典)=》代表的布尔值为false,其余的类型都为真
# 空(假): "" 、[] 、{} 空格也算是字符



# 流程控制之if判断
"""
语法1:
if条件:
代码1
代码2
代码3



print(1)
print(2)
print(3)
if条件:
代码1
代码2
代码3
print(4)
print(5)
"""
# age =52
# is_beautiful=True
# star='水平座'
#
# if age>16 and age<20 and is_beautiful and star=='水平座':
# print('我喜欢你,我们在一起吧。。。')
#
# print('其他代码。。。。。。')

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

"""
语法3:
if条件:
代码1
代码2
代码3
elif 条件2:
代码1
代码2
代码3
"""
# score = 82
# 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
语法3:
if条件:
代码1
代码2
代码3
elif 条件2:
代码1
代码2
代码3
elif 条件2:
代码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 可以嵌套
"""
# age = 18
# is_beautiful = True
# star = '水平座'
#
# if 16 < age < 20 and is_beautiful and star == '水平座':
# print('开始表白')
# is_beautiful = True
# if is_beautiful:
# print('过上幸福的生活')
# else:
# print('阿姨你好,我逗你玩呢,深藏功与名')
#
# print('其他代码。。。。。。')


# x1='egon'
# y1='123'
#
# tag=True
# while tag:
# x = input('请输入账号:')
# y = input('请输入密码:')
# if x==x1 and y==y1:
# print('登录成功')
# tag=False
# else:
# print('您输入的账号或密码错误,请重新输入')
# print('====end====')



# x1='egon'
# y1='123'
# tag=True
#
# while tag:
# x = input('请输入账号:')
# y = input('请输入密码:')
# if x==x1 and y==y1:
# print('登录成功')
#
# print('====end====')
# while tag:
# cmd=input('请输入命令:')
# if cmd=='q':
# tag=False
#
# else:
# print('命令{x}正在运行'.format(x=cmd))
#
# else:
#
# print('您输入的账号或密码错误,请重新输入')


















原文地址:https://www.cnblogs.com/ltyc/p/12992304.html