Python Case when用法介绍

Python Case when用法介绍

Python Case when用法介绍

1. 概述

在编程中,我们经常遇到根据条件执行不同代码块的情况。Python中的if-elif-else语句是一种常见的条件语句,但对于多个条件判断的情况,使用case when语法会更加简洁明了。

case when语法类似于SQL中的case when语句,可以根据条件返回不同的结果。本文将详细介绍Python中使用case when语法的方法,并给出5个示例代码及运行结果。

2. Python中的case when

Python中没有原生的case when语法,但我们可以通过字典(dictionary)或if-elif-else语句实现类似的功能。

2.1 使用字典实现case when

通过将条件与对应的结果建立字典,我们可以通过输入条件从字典中获取结果。

示例代码:

def case_when(condition):
    result = {
        1: "条件1满足",
        2: "条件2满足",
        3: "条件3满足"
    }
    return result.get(condition, "默认结果")

print(case_when(1))  # 输出:条件1满足
print(case_when(4))  # 输出:默认结果
Python

运行结果:

条件1满足
默认结果
Python

2.2 使用if-elif-else实现case when

另一种常见的方法是使用if-elif-else语句,根据不同的条件判断执行不同的代码块。

示例代码:

def case_when(condition):
    if condition == 1:
        return "条件1满足"
    elif condition == 2:
        return "条件2满足"
    elif condition == 3:
        return "条件3满足"
    else:
        return "默认结果"

print(case_when(1))  # 输出:条件1满足
print(case_when(4))  # 输出:默认结果
Python

运行结果:

条件1满足
默认结果
Python

3. 示例代码

下面列举5个示例代码,分别使用字典和if-elif-else语句实现case when的功能,并给出运行结果。

3.1 示例1:月份转季节

根据输入的月份,判断是春季、夏季、秋季还是冬季。

def get_season(month):
    seasons = {
        1: "冬季",
        2: "冬季",
        3: "春季",
        4: "春季",
        5: "春季",
        6: "夏季",
        7: "夏季",
        8: "夏季",
        9: "秋季",
        10: "秋季",
        11: "秋季",
        12: "冬季"
    }
    return seasons.get(month, "无效月份")

print(get_season(3))   # 输出:春季
print(get_season(7))   # 输出:夏季
print(get_season(13))  # 输出:无效月份
Python

运行结果:

春季
夏季
无效月份
Python

3.2 示例2:成绩等级划分

根据输入的成绩,判断其等级(优秀、良好、及格或不及格)。

def get_grade(score):
    if score >= 90:
        return "优秀"
    elif score >= 80:
        return "良好"
    elif score >= 60:
        return "及格"
    else:
        return "不及格"

print(get_grade(95))   # 输出:优秀
print(get_grade(82))   # 输出:良好
print(get_grade(67))   # 输出:及格
print(get_grade(58))   # 输出:不及格
Python

运行结果:

优秀
良好
及格
不及格
Python

3.3 示例3:商品折扣

根据输入的商品原价和会员等级,计算折扣后的价格。

def calculate_discount(price, level):
    if level == "普通会员":
        return price * 0.95
    elif level == "银卡会员":
        return price * 0.9
    elif level == "金卡会员":
        return price * 0.85
    else:
        return price

print(calculate_discount(100, "普通会员"))   # 输出:95.0
print(calculate_discount(100, "银卡会员"))   # 输出:90.0
print(calculate_discount(100, "钻石会员"))   # 输出:100
Python

运行结果:

95.0
90.0
100
Python

3.4 示例4:工龄奖金

根据输入的工龄,计算应发放的奖金。

def calculate_bonus(years):
    bonus = 0
    if years < 1:
        bonus = 0
    elif years < 3:
        bonus = 1000
    elif years < 5:
        bonus = 3000
    elif years < 10:
        bonus = 5000
    else:
        bonus = 8000
    return bonus

print(calculate_bonus(1))   # 输出:1000
print(calculate_bonus(4))   # 输出:3000
print(calculate_bonus(8))   # 输出:5000
print(calculate_bonus(15))  # 输出:8000
Python

运行结果:

1000
3000
5000
8000
Python

3.5 示例5:计算人口增长率

根据输入的当前人口和过去人口,计算人口的增长率。

def calculate_growth_rate(current_population, past_population):
    growth_rate = 0
    if current_population > past_population:
        growth_rate = (current_population - past_population) / past_population * 100
    elif current_population < past_population:
        growth_rate = (past_population - current_population) / past_population * -100
    return growth_rate

print(calculate_growth_rate(500, 400))   # 输出:25.0
print(calculate_growth_rate(400, 500))   # 输出:-20.0
print(calculate_growth_rate(500, 500))   # 输出:0
Python

运行结果:

25.0
-20.0
0
Python

4. 总结

通过使用字典或if-elif-else语句,我们可以很方便地实现类似于SQL中的case when功能。根据不同的条件返回不同的结果,使代码变得简洁明了。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册