pythonifand_Python之if语句及条件语句

更新时间:2023-06-26 18:27:25 阅读: 评论:0

pythonifand_Python之if语句及条件语句其实各个语⾔的基础都是相通的。
words = ['soften', 'civilize', 'personalization', 'bakery', strain', 'glimp', 'royals']
for word in words:
if word == 'civilize':
print(word.upper()) #全部⼤写输出
el:
print(word.title()) #⾸字母⼤写输出
result:
Soften
CIVILIZE
Personalization
Bakery有焦虑症怎么办
Strain
Glimp
Royals
*
条件语句包括:!=\<=\>=\>\
chl = 21
凌晨三点fsy = 32
chl >=20 and fsy <=31 #检查两个条件语句,使⽤关键词and result:Fal
(chl >=20) and (fsy <=31) #也可将每个测试放在⼀对括号内
chl >=20 or fay <=31 #使⽤or result:True
*
word = ['bride', 'castle', 'masterpieces', 'mystery', 'tragedy', 'bakery', 'strain', 'glimp']
'castle' in word #使⽤关键词in让python检查列表中是否有'castle'动态路由配置
words = ['emperor', 'castle', 'royal', 'diplomat', 'bakery', 'strain', 'glimp']
日常周记
chl = 'emperors'
if chl not in words: #if not in
print("The word is not in words list!")
*
age = 12
if age <= 4:
print("I like desrt and trade!")
elif age <= 18:    #python这点不⼀样哈,有个elif,可以有若⼲个elif语句
print("I like diplomat and dumpling!")
el:
print("I like nobility and sketch!")
result:
最接近天堂的地方
徐州公积金电话I like diplimat and dumpling!
触控板失灵*
age = 12
if age <= 4:
price = 0
elif age <= 18:
price = 10
el:
price = 20
print("You admission cost is $ " + str(price) +"!") #price要str函数告诉python输出字符**当其中⼀个if语句通过测试,python会跳过其他的if语句测试。
eg:寻找两个列表共同的元素(交集)
chls = ['emperor', 'nobility', 'bakery', 'sauce', 'royal']
fsys = ['emperor', 'Spring Festival', 'approaching', 'nobility']
for chl in chls:
if chl in fsys:
print("We can discover " + chl +"!")
el:
print("Oh my god! The Spring Festival is approaching, prepare " + chl + "!")
result:
We can discover emperor!
内容拼音Oh my god! The Spring Festival is approaching, prepare nobility!
Oh my god! The Spring Festival is approaching, prepare bakery!
Oh my god! The Spring Festival is approaching, prepare sauce!
Oh my god! The Spring Festival is approaching, prepare royal!

本文发布于:2023-06-26 18:27:25,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1056085.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:语句   输出   列表   检查   条件   徐州   地方
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图