python的控制语句

来源:互联网 发布:上海市重点软件企业 编辑:程序博客网 时间:2024/06/06 07:12

Python的控制语句

思维导图:

这里写图片描述

顺序结构

从上到下顺序执行

分支结构

  • if:
if 表达式:    语句
  • if…else…
if 表达式:    语句else:    语句
  • if…elif…else…
if 表达式:    语句elif 表达式:    语句else:     语句注意:在python里面不支持switch语句,如果想实现switch的效果,第一种方法就是使用if...elif...elif...else...;

python中实现三目运算符:

C: maxNum = a>b? a:b
Python: maxNum= a if a>b else b

循环结构

for, while, do…while…

for循环

  • range(m,n,x):从m起始到n-1结束(不包含n),x代表步长;
for item in range(m.n,x):    循环的语句for item in 可迭代的类型(eg:字符串.....):    循环的语句
  • 两个关键字:
    • break:跳出循环,不再执行循环;
    • continue:跳出本次循环,继续执行下一个循环;

while循环

while

while 表达式(或者TrueFalse):    循环的语句

while … else …..

while 表达式:    循环语句In [5]: while trycount<3:   ...:     print "login"   ...:     trycount+=1   ...: else:       ...:     print "bigger than 3"   ...:     

pass

只是占一个语句的位置,并无任何操作;

程序 = 算法 + 数据结构

C:数组,结构体,………..
Python: str,list,tuple,set,dict……..

原创粉丝点击