"""
if 表达式:
语句块
if 表达式:
语句1
else:
语句2
if 表达式1:
语句块1
elif 表达式2:
语句块2
elif 表达式n:
语句块n
else:
语句块n+1
match 变量:
case 变量1:
语句块1
case 变量2:
语句块2
case 变量n:
语句块n
case _:
语句n+1
可用于结构模式匹配
同步迭代
match data1,data2:
case data1,data2:
pass
"""
number=int(eval(input("输入一个整数:")))
if number%2==0:
print(number,"是偶数")
if number%2!=0:
print(number,"是奇数")
if number%2==0:
print(number,"是偶数")
else:
print(number,"是奇数")
result="是偶数" if number%2==0 else"是奇数"
print(number,result)
print(number,"是偶数" if number%2==0 else "是奇数")
if number%2==0:
print(number,"是偶数")
elif number%2!=0:
print(number,"是奇数")
if number>0:
print(number,"是正数")
elif number<0:
print(number,"是负数")
else:
print(number,"既不是正数,也不是负数")
character=input("输入:")
if character:
try:
if float(character) == 0:
print("是数字串,空")
else:
print("是数字串,非空")
except ValueError:
print("是字符串,非空")
else:
print("是空串")
score=input("输入等级:")
match score:
case 'a':
print("优秀")
case 'b':
print("良好")
case 'c':
print("及格")
case 'd':
print("不及格")
case _:
print("类型错误")
data=eval(input("输入数据"))
match data:
case [0,1,2,3]:
print("列表")
case (0,1,2,3):
print("元组")
case {0:'hello',1:'world',2:'by',3:'python'}:
print("字典")
case _:
print("类型错误")
data1=['hello','world','python']
data2=[0,1,1]
for d1,d2 in zip(data1,data2):#压缩
match d1,d2:
case 'hello',0:
print(data1[0])
case 'world',1:
print(data1[1])
case 'pycharm',1:
print(data1[2])
运行结果