python基础之条件语句

1|0检查相等和不等

2|0多个检查条件

age1 = 22
age2 = 19
s1 = age1 > 21 and age2 > 19
print(s1)
s2 = age1 > 21 or age2 > 19
print(s2)

3|0检查是否包含在列表内

requested_toppings = ['mushrooms','onions','pineapple']
s1 = 'mushrooms' in requested_toppings
print(s1)
s2 = 'pepperoni' in requested_toppings
print(s2)

4|0检查不包含

requested_toppings = ['mushrooms','onions','pineapple']
s1 = 'mushrooms' not in requested_toppings
print(s1)
s2 = 'pepperoni' not in requested_toppings
print(s2)

5|0if语句

age = 19
if age >= 18:
print('You are old enough to vote')
if age >= 20:
print('is no print')

6|0if-elif-else语句

age = 12
if age < 4:
price = 0
elif age < 18:
price = 5
elif age < 65:
price = 10
else:
price = 5
print('Your admission cost is $' + str(price) + '.')

7|0检查特殊元素

requested_toppings= ['mushrooms','green peppers','extra cheese']
for requested_topping in requested_toppings:
if requested_topping == 'green peppers':
print('Sorry,we are out of green peppers right now')
else:
print('Adding' + requested_topping + '.')

requested_toppings= []
if requested_toppings:
for requested_topping in requested_toppings:
print('Adding ' + requested_topping + '.')
else:
print('Are you sure you want a plain pizza?')

available_toppings = ['mushrooms','olives','green peppers','pepperoni']
requested_toppings= ['mushrooms','french fries','green peppers']
for requested_topping in requested_toppings:
if requested_topping in available_toppings:
print('Adding ' + requested_topping + '.')
else:
print("Sorry,we don\'t have " + requested_topping + '.')


__EOF__

本文作者Harry
本文链接https://www.cnblogs.com/harry66/p/13764499.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   Harry_666  阅读(201)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示