在Python中切换大小写(替换)
在本教程中,我们将编写一个在Python中充当开关案例的程序。
其他编程语言(例如C,C++,Java等)可以切换,而Python则没有。让我们编写一个在Python中充当开关案例的程序。
我们将使用dict数据结构在Python中编写切换用例。让我们看下面的代码。
示例
# dictionary with keys and values
days = {
0: 'Sunday',
1: 'Monday',
2: 'Tuesday',
3: 'Wednesday',
4: 'Thursday',
5: 'Friday',
6: 'Saturday'
}
# we will use 'get' method to access the days
# if we provide the correct key, then we will get the corresponding value
# if we provide a key that's not in the dictionary, then we will get the defaul
lue
print(days.get(0, 'Bad day'))
print(days.get(10, 'Bad day'))输出结果
如果运行上面的代码,则将得到以下结果。
Sunday Bad day
我们可以用任何东西代替值观。get方法的默认值表示切换情况下的默认关键字。
结论
如果您对本教程有任何疑问,请在评论部分中提及。
热门推荐
5 短祝福语简短暖心
10 结婚祝福语粤语大全简短
11 晚上祝福语女生文案简短
12 法语妈妈生日祝福语简短
13 药厂开工祝福语大全简短
14 蛋糕节日祝福语简短英文
15 跨年的生日祝福语简短
16 文案祝福语英文短句简短
17 在家聚餐婚礼祝福语简短
18 学生节祝福语大全简短