Python逻辑运算符

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的随机数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mylgcs

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值