【Python】Python入门——判断语句

Python入门——判断语句。内容包括if语句、条件表达式、三元运算、match语句等。

目录

一、if 语句

1. 基本if-else语句

2. 常用比较运算符

3. if-else连写

4. pass 语句

5. 变量的作用域

二、条件表达式

三、三元运算

四、match语句

五、其他


一、if 语句

1. 基本if-else语句

当条件成立时,执行某些语句;否则执行另一些语句。
注意:

if和else后需要加上冒号:
if语句的代码块需要缩进统一长度,规范写法是缩进4个空格。

a = int(input())

# 正确写法:
if a > 5:
    print("%d is big!" % a)
    print("%d + 1 = %d" % (a, a + 1))
else:
    print("%d is small!" % a)
    print("%d - 1 = %d" % (a, a - 1))

# 错误写法:
if a > 5:
      print("%d is big!" % a)
    print("%d + 1 = %d" % (a, a + 1))
else:
    print("%d is small!" % a)
     print("%d - 1 = %d" % (a, a - 1))
else语句可以省略:

a = int(input())

if a > 5:
    print("%d is big!" % a)
    print("%d + 1 = %d" % (a, a + 1))

当只有一条语句时,可以写在同一行,但这样写不规范,只要了解即可:

a = int(input())

if a > 5: print("%d is big!" % a)
else: print("%d is small!" % a)

【举例】输入一个整数,输出这个数的绝对值。

x = int(input())

if x > 0:
    print(x)
else:
    print(-x)

【举例】输入两个整数,输出两个数中较大的那个。

a, b = map(int, input().split())

if a > b:
    print(a)
else:
    print(b)

if-else语句内也可以是if-else语句。

【举例】输入三个整数,输出三个数中最大的那个。

a, b, c = map(int, input().split())

if a > b:
    if a > c:
        print(a)
    else:
        print(c)
else:
    if b > c:
        print(b)
    else:
        print(c)

2. 常用比较运算符

(1) >          大于
(2) <          小于
(3) >=        大于等于
(4) <=        小于等于
(5) ==        等于
(6) !=         不等于

例如:

a, b = map(int, input().split())

if a > b:
    print("%d > %d" % (a, b))
if a >= b:
    print("%d >= %d" % (a, b))
if a < b:
    print("%d < %d" % (a, b))
if a <= b:
    print("%d <= %d" % (a, b))
if a == b:
    print("%d == %d" % (a, b))
if a != b:
    print("%d != %d" % (a, b))

3. if-else连写

注意:else if要写成elif。

【举例】
输入一个0到100之间的分数,
如果大于等于85,输出A;
如果大于等于70并且小于85,输出B;
如果大于等于60并且小于70,输出C;
如果小于60,输出 D;

s = int(input())

if s >= 85:
    print("A")
elif s >= 70:
    print("B")
elif s >= 60:
    print("C")
else:
    print("D")

【举例】

1.判断闰年。闰年有两种情况:
(1) 能被100整除时,必须能被400整除;
(2) 不能被100整除时,被4整除即可。
输入一个年份,如果是闰年输出yes,否则输出no。

year = int(input())

if year % 100 == 0:
    if year % 400 == 0:
        print("yes")
    else:
        print("no")
else:
    if year % 4 == 0:
        print("yes")
    else:
        print("no")

4. pass 语句

pass 语句不执行任何动作。语法上需要一个语句,但程序毋需执行任何动作时,可以使用该语句。

例如:

x = int(input())

if x > 5:
    pass  # 先空出来等以后再实现
else:
    print(x)

5. 变量的作用域

if语句内部的变量,可以在语句外访问。例如:

a, b = map(int, input().split())

if a > b:
    max_value = a
else:
    max_value = b

print(max_value)

二、条件表达式

(1) 与 and
(2) 或 or
(3) 非 not

注意:运算符优先级:not > and > or。

【举例】用一条if语句,判断闰年。

year = int(input())

if year % 100 != 0 and year % 4 == 0 or year % 400 == 0:
    print("yes")
else:
    print("no")

三、三元运算

类似于C++、Java中的问号表达式。例如:

a, b = map(int, input().split())
max_value = a if a > b else b
print(max_value)

四、match语句

python3.10开始新增了match语句。

注意:只有第一个匹配的模式会被执行。且跟C++、Java不同,匹配后只会执行当前模式,不会顺次执行后面的case。
可以用 | 表示匹配多个模式。
变量名 _ 被作为 通配符 并必定会匹配成功。
如果没有 case 匹配成功,则不会执行任何分支。

status = int(input())

match status:
    case 400:
        print("Bad request")
    case 404:
        print("Not found")
    case 418 | 420 | 422:
        print("I'm a teapot")
    case _:
        print("Something's wrong with the internet")

五、其他

注意if和else语句后一定要加冒号。
判断语句内部的代码一定要记得缩进。
格式化字符串中想表示%时,需要写%%。
Python中交换两个变量,可以用:a, b = b, a。
Python中的比较运算符支持链式操作,这一点跟C++和Java等语言不同。例如,给三个数排序的代码可以这么写:

a, b, c = map(int, input().split())
x, y, z = a, b, c

if a >= b >= c:
    print(c, b, a)
elif a >= c >= b:
    print(b, c, a)
elif b >= a >= c:
    print(c, a, b)
elif b >= c >= a:
    print(a, c, b)
elif c >= a >= b:
    print(b, a, c)
elif c >= b >= a:
    print(a, b, c)

参考:AcWing——Python基础课及讲义、Python教材。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值