美文网首页
Python之if语句及条件语句

Python之if语句及条件语句

作者: 龙star180 | 来源:发表于2019-05-09 16:57 被阅读0次

其实各个语言的基础都是相通的。

words = ['soften', 'civilize', 'personalization', 'bakery', strain', 'glimpse', 'royals']

for word in words:
    if word == 'civilize':

        print(word.upper()) #全部大写输出

    else:

        print(word.title()) #首字母大写输出

result:

Soften

CIVILIZE

Personalization

Bakery

Strain

Glimpse

Royals

*

条件语句包括:!=\<=\>=\>\<  #这点python还是做的很好哈。

chl = 21

fsy = 32

chl >=20 and fsy <=31 #检查两个条件语句,使用关键词and result:False

(chl >=20) and (fsy <=31) #也可将每个测试放在一对括号内

chl >=20 or fay <=31 #使用or result:True

*

word = ['bride', 'castle', 'masterpieces', 'mystery', 'tragedy', 'bakery', 'strain', 'glimpse']

'castle' in word #使用关键词in让python检查列表中是否有'castle'

words = ['emperor', 'castle', 'royal', 'diplomat', 'bakery', 'strain', 'glimpse']

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 dessert and trade!")

elif age <= 18:    #python这点不一样哈,有个elif,可以有若干个elif语句

    print("I like diplomat and dumpling!")

else:

    print("I like nobility and sketch!")

result:
I like diplimat and dumpling!

*

age = 12

if age <= 4:

    price = 0

elif age <= 18:
    price = 10

else:

    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 +"!")

    else:

        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!

        

相关文章

  • Python之if语句及条件语句

    其实各个语言的基础都是相通的。 words = ['soften', 'civilize', 'personali...

  • 13.python3条件控制

    python3条件控制之if语句 Python 条件语句是通过一条或多条语句的执行结果(True 或者 False...

  • python三大语句

    Python的条件语句 if语句 1.基本if语句: if 条件: 执行语句 2.if-else结构: if ...

  • Python 语句

    Python 语句包括以下: Python 条件语句if - elseif - elif - ... - else...

  • Python初学者入门随笔 05 Python 条件语句

    Python 条件语句 Python 条件语句是通过一条或多条语句的执行结果( True 或者 False )来决...

  • day4-总结

    python中的if语句(补充) 3.if-elif-if语句 if 条件语句1:代码块1elif 条件语句2:代...

  • Day 3 控制流(Control Flow)

    条件语句 if 语句 冒号后为满足if的条件后要执行的语句 elif 以及else语句 python中用缩进封装代...

  • Python 语句

    关键词:语句 条件语句 或者,有多个判断条件时 python 并不支持 switch 语句 循环语句 while ...

  • 初学python-条件语句

    Python 条件语句 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代...

  • Life is short,you need python!(3

    征服各类语句,通关即可获得刷跳一跳权限。(没错,又有彩蛋) 【1】Python 条件语句 Python条件语句是通...

网友评论

      本文标题:Python之if语句及条件语句

      本文链接:https://www.haomeiwen.com/subject/ugtwoqtx.html