python 运算符和if分支

身份运算符

代码如下:

list1=["a","b","c"]
list2=list1.copy()
print(list1)
print(list2)
print(list1 is list2)#is是对地址判断
print(list1 == list2)#==是对值的判断

运行结果:


python 运算符和if分支

成员运算符

代码如下:

list1=["a","b","c"]
a="a" in list1
print(a)
b="3" in list1
print(b)
dict1={"name":"a","age":"12","sex":"boy"}
s1="height" not in dict1  #判断键是否在字典中
print(s1)
s2="name" in dict1
print(s2)

运行结果:


python 运算符和if分支

逻辑运算符

代码如下:

#and和 所有条件都满足
age=16
s1=age>9 and age<19 and age !=16
print(s1)
# or或 满足一个条件
age=16
s2=age>9 or age<5
print(s2)

运行结果:


python 运算符和if分支

if分支

单个分支

代码如下:

score=99
s1=score<=90
print(s1)
#只有当判断条件为真的时候才进入该分支
if s1:             #一定要有冒号
    print("lose")

s2=77
if s2>80:
    print("lose")
else:
    print("get")

运行结果:


python 运算符和if分支

补充:

代码如下:

s1=77
if s1>80:
    print("lose")
else:
    pass  #无意义,只是占个位置

print("数字为:{}".format(s1))

运行结果:


python 运算符和if分支

多个分支

代码如下:

score=float(input("请输入成绩:"))
if score>=90 and score<=100:
    print("A")
elif score>=80 and score<90:
    print("B")
elif score>=70 and score<80:
    print("C")
elif score>=60 and score<70:
    print("D")
elif score>=0 and score<60:
    print("E")
else :  #上面的所有条件都不满足
    print("输入不规范")

运行结果:


python 运算符和if分支

发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章