码迷,mamicode.com
首页 > 编程语言 > 详细

python学习[第十三篇] 条件和循环

时间:2018-07-23 16:28:07      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:操作符   delete   操作   and   span   字典   结果   没有   Suite   

python学习[第十三篇] 条件和循环

if语句

单一if 语句

if语句有三个部分构成,关键字if本身,判断结果真假的条件表达式,以及表达式为真或非0是执行的代码

if expression:

    expr_true_suite

条件表达式可以是多重的 通过布尔操作符 and or not来实现

单一语句的if 代码块,如果if语句的执行代码只有一行,可以放在一行来写

if expresssion: expr_tru_suite

>>> if True: print True
...
True

 

else 语句

语法如下:

if expression:
    expr_true_suite
else:
    expr_false_suite

 

elf 语句

可以有多个elif ,但只能由一个if 一个else , 语法如下

if expression1:
    expr1_true_suite
elif expression2:
    expr2_true_suite
elif expression3:
    expr3_true_suite
....
elif expressionx:
    exprx_true_suite
else:
     none_of_above_suite

 

python 中没有switch/case语句

我们可以通过字典来实现,注意字典后的值不要加引号,对应方法应在字典前定义好。

def insert_met():
    print "this is insert_met"

def delete_met():
    print "this is delete_met"

def update_met():
    print "this is update_met"

CMDs={"insert":insert_met,"delete":delete_met,"update":update_met}

def choice(m):
    CMDs[m]()

choice(insert)

choice(update)

choice(delete)

 

python中三元操作符的实现

 

python学习[第十三篇] 条件和循环

标签:操作符   delete   操作   and   span   字典   结果   没有   Suite   

原文地址:https://www.cnblogs.com/ryanpan/p/9355162.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!