"""
真值表达式
if数据:
语句
本质就是使用bool函数操作数据
条件表达式
"""
# 1.真值表达式 if "a": # if bool ("a"): print("真值") str_input = input("请输入:") if str_input: print("输入的字符串不是空的")
# 2.条件表达式:有选择性的为变量进行赋值 sex = None if input("请输入性别:") == "男": sex = 1 else: sex = 0 print(sex)
练习题:
# 练习1:在控制台中获取一个整数, # 如果是偶数为变量state赋值“偶数”,否则赋值“奇数” # 方法一: number = int(input("请输入一个整数:")) if number % 2 == 1: state = "奇数" else: state = "偶数" print(state)
# 方法一: result = year % 4 == 0 and year % 100 != 0 or year % 400 == 0 if result: day = 29 else: day = 28 print(result) # 方法二:代码可读性很差 # if not year % 4 == 0 and year % 100 or not year % 400: if year % 4 == 0 and year % 100 != 0 or year % 400 == 0: day = 29 else: day = 28 print(day) # 方法三: day = 29 if year % 4 == 0 and year % 100 != 0 or year % 400 == 0 else 28 print(day)
标签:真值,Python,else,year,print,input,day,表达式 From: https://www.cnblogs.com/Remick/p/17066648.html