Python逻辑运算符
与、或、非
# 逻辑 与 and 理解成乘法 true 表示1 false 0
print(True and True) # True
print(True and False) # False
print(False and True) # False
print(False and False) # False
# 逻辑 或 or 理解成加法
print(True or True) # True
print(True or False) # True
print(False or True) # True
print(False or False) # False
# 逻辑 非
print(not True) # False
print(not False) # True
and 表示如果一个为false结果为false、or表示有一个为true结果为true、 not取反
if的使用格式
age = 10
if age >= 18:
print('已成年')
if-else
n = 2
if n % 2 == 0:
print(f'{n} 是偶数')
else:
print(f'{n} 是奇数')
if-elif-else
def is_week_day(day):
if day == '1' or day == '一':
print('星期一')
elif day == '2' or day == '二':
print('星期二')
elif day == '3' or day == '三':
print('星期三')
elif day == '4' or day == '四':
print('星期四')
elif day == '5' or day == '五':
print('星期五')
elif day == '6' or day == '六':
print('星期六')
elif day == '7' or day == '七':
print('星期七')
else:
print('输入的日期不对')
d = input('请输入日期:')
is_week_day(d)
if 语句的嵌套
def is_socre_level(socre):
if 0 <= socre <= 100:
if socre >= 90:
print(f'得分{socre}, 优')
elif socre >= 80:
print(f'得分{socre}, 良')
elif socre >= 80:
print(f'得分{socre}, 中')
elif socre >= 80:
print(f'得分{socre}, 差')
else:
print(f'得分{socre}, 不及格')
else:
print('请输入有限的成绩')
s = int(input('input socre:'))
is_socre_level(s)
随机数
import random # python自带模块
n = random.randint(0, 99)
print(n)
0-99的随机数