1、python流程控制,代码是按照顺序来执行的,但是也可以让某些行符合某些条件时执行,也可以让某些行反反复复去执行;
流程控制用到 if 条件 及 循环 for while
2: if 条件格式: if 后面跟表达式,后面的冒号是固定格式,然后下面是代码块;
if expression:
statement
注释:python 使用缩进作为语句分组,建议使用四个空格;
如下代码,因为 1 就表示 ture 正确,所以这个表达式成立,则会返回下面两行内容;
[root@localhost_002 python]# vim 6.py #!/usr/bin/pythonif 1:#if 'a': #写成字符串也成立,写成空着不成立; pinrt "hello,python" print "true"[root@localhost_002 python]# python 6.pyhello pythontrue
如下代码:如果改成 0 则表示错误 flase 表达式不成立,则不会有任何输出显示:
[root@localhost_002 python]# vim 7.py #!/usr/bin/pythonif 0: print "hello python" print "true"[root@localhost_002 python]# python 7.py
当然,写成比较运算符也是可以的; if 1 < 2: print "hello python" print "true" 如下也成立:
[root@localhost_002 python]# cat 7.py #!/usr/bin/pythonif 1 < 2: print "hello python" print 'true'[root@localhost_002 python]# python 7.py hello pythontrue
当然,也可以使用 逻辑 非 not : if not 1 > 2: print "hello python" print "true" 如下命令:
[root@localhost_002 python]# cat 7.py #!/usr/bin/pythonif not 1 > 2: print "hello python" print 'true'[root@localhost_002 python]# python 7.py hello pythontrue
注释:逻辑非 not 表示本来是flase 会变成 true, 所以 1 > 2 本来是flase,不过加上了 not 就变成是true;
也可以添加多个逻辑运算符; and or not ||| if not 1 > 2 and 1 == 1: print "hello python" print "true"
[root@localhost_002 python]# cat 8.py #!/usr/bin/pythonif not 1 > 2 and 1 == 1: print "hello python" print 'true'[root@localhost_002 python]# python 8.py hello pythontrue
注释:因为 逻辑非 not 的运算符比较高,所以会先判断左边的;
3: if 条件 还有一个 else: 选项,当表达式不成立,则怎样: 等同于 shell 脚本;
[root@localhost_002 python]# cat 8.py #!/usr/bin/pythonif 1 > 2: print "hello python" print 'true'else: print "hahah"[root@localhost_002 python]# python 8.py hahah
注释: if 1 > 2: print "hello python" print "true" else: print "hahah" else: 这是固定的语法格式:
注释:python 会根据缩进来区分语句分组:如下在最后一行与 if 平行 输入 print "end" 这行表示无论表示是否成立都会执行:
[root@localhost_002 python]# cat 8.py #!/usr/bin/pythonif 1 > 2: print "hello python" pirnt "true"else: print "hahah"print "end"[root@localhost_002 python]# python 8.py hahahend
注释:python 会根据缩进来区分语句分组,一般以四个空格为准:
4: 当多个条件时,可以写入 elif来判断,当成立时,则会打印出来:如下:通过接受一个变量,来判断打印分数:
[root@localhost_002 python]# cat 9.py #!/usr/bin/pythonscore = raw_input("Please a num: ")if score >= 90: print 'A' print 'very good'elif score >=80: print 'B' print 'good'elif score >=70: print 'C' print 'pass'else: print 'D'print 'end'[root@localhost_002 python]# python 9.py Please a num: 30Avery goodend
注释:看到如上输出有错误,因为输入30,打印 D ,却打印了 A ,因为输入的字符串,当成字符串来判断;
此时需要转换成数值 int 类型: 通过如下命令: int(age) 只有数值才可以转换成int 类型;
In [12]: age='24' #定义个字符串:
In [13]: type(age) #打印这个字符串类型:
Out[13]: strIn [14]: type(int(age)) #通过int 来转换这个字符串类型:
Out[14]: int再次修改以上那个查看分数的脚本:
[root@localhost_002 python]# cat 9.py #!/usr/bin/pythonscore = int(raw_input("Please input num: "))if score > 90: print 'A' print 'very good'elif score >= 80: print 'B' print 'good'elif score >=70: print 'C' print 'pass'else print 'D'print 'end'[root@localhost_002 python]# python 9.py Please a num: 90Avery goodend[root@localhost_002 python]# python 9.py Please a num: 30Dend
注释:if 条件是顺序匹配的,一旦匹配到了,则停止执行下面的命令,不会和 if 平行的命令还是会执行的;
5:逻辑值 也交bool 值:包含 true 和 flase;
true : 表示非空的量,比如 string tuple list set dictonary, 所有非零数;
flase: 表示 0 None 空的值;
if 条件判断:再写一个python 脚本,判断输入 是 [Yes/No]:然后判断回复相应内容:
op = 'raw_input(please input [Yes/No]: )' 这个命令可以判断输入的命令是yes 还是 no;
[root@localhost_002 python]# vim 10.py#!/usr/bin/pythonop = raw_input("please input [Yes/No]: ")op = op.lower()if op == 'y' or op == 'yes': print "program is running..."elif op == 'n' or op == 'no' print 'program is exit'else: print 'please input [Yes/No]'print 'end'[root@localhost_002 python]# python 10.py Please input [Yes/No]: yesprograme is running...[root@localhost_002 python]# python 10.py Please input [Yes/No]: noprograme is exit[root@localhost_002 python]# python 10.py Please input [Yes/No]: sdfsadf
注释:如上用到了op.lower() 这个字符处理方法,这个命令可以把字符串变成大写变成小写;
In [36]: a = 'ABC'
In [37]: a.lower()
Out[37]: 'abc' 当然也可以把小写变成大写; a.upper()In [38]: a = 'abc'
In [39]: a.upper()
Out[39]: 'ABC'6、循环: for while (for 循环是一个结构,程序要循环一定得次数) (while循环是条件循环,当条件为真时,则循环结束)
for 循环对序列进行遍历(字符串 元组 列表) 序列包括:
语法格式:
for 迭代对象 in 序列
print 声明
In [49]: aOut[49]: 'ABC'In [50]: for i in a: ...: print i ...: ABC
如上:默认在print 1 后面会有默认换行符,如果加上逗号,则表示抑制换行符: 中间以空格区分:
In [51]: a = 'ABC'In [52]: for i in a: ...: print i, ...: A B C
当然也而循环列表 list: list1 = [1,2,3,4] for i in list1: print i
In [53]: list1 = [1,2,3,4]In [54]: for i in list1: ...: print i ...: 1234
当然也是可以循环元组 tuple: t = (1,2, 3,4) for i in t: print i
In [55]: t = (1,2, 3, 4)In [56]: type(t)Out[56]: tupleIn [57]: for i in t: ...: print i ...: 1234
7:range()函数: 多用在for循环里,用来打印数字: 最后一个数字不取出来(类似于切片): range(0,10) 表示只能取到 9 了;
In [58]: range(10)Out[58]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
range(0,10,2) 第一位是起始值 第二位是结束值 第三位是步长值(表示隔几位取值); 会打印出: 2 4 6 8
In [67]: range(0,10,2)Out[67]: [0, 2, 4, 6, 8]In [68]: range(2,10,2)Out[68]: [2, 4, 6, 8]In [71]: for i in range(11): ...: print i ...: 012345678910In [72]: t = range(10)In [73]: tOut[73]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
注释:如上:range 返回值是一个列表:
如下:在for循环里的常用格式: for i in range(1,11): print i
[root@localhost_002 python]# cat 11.py #!/usr/bin/pythonfor i in range(1,11): print i运行:[root@localhost_002 python]# python 11.py12345678910
有时候 for 会结合 if 来使用,如下:用range 来打印列表,然后用 if 来判断打印的值是否 除 2 取 0 ,是则打印出来:也就是打印偶数:
#!/usr/bin/pythonfor i in range(1,11) if i % 2 == 0 print i运行:[root@localhost_002 python]# python 12.py 246810
还可以以下的写法: print [i for i in range(1,11)]
[root@localhost_002 python]# cat 12.py #!/usr/bin/pythonprint [i for i in range(1,11)][root@localhost_002 python]# python 12.py [1, 2, 3, 4, 5, 6, 7, 8, 9, 10][root@localhost_002 python]# vim 12.py #!/usr/bin/pythonprint [i*2 for i in range(1,11)][root@localhost_002 python]# python 12.py [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]一行来写for 和 if 条件:[root@localhost_002 python]# cat 12.py #!/usr/bin/pythonprint [i for i in range(1,11) if i % 2 == 0][root@localhost_002 python]# python 12.py [2, 4, 6, 8, 10]
列表重写: for i in [i**2 for i in range(1,11)]: print i
[root@localhost_002 python]# cat 12.py #!/usr/bin/pythonfor i in [i**2 for i in range(1,11)]: print i,[root@localhost_002 python]# python 12.py 1 4 9 16 25 36 49 64 81 100
9:打印1 到 100 的和:sum = 0 for i in range(1,101): sum = sum + i print sum
[root@localhost_002 python]# cat 14.py #!/bin/pythonsum = 0for i in range(1,101): sum = sum + i# sum += i #用法和sum=sum+i相同print sum[root@localhost_002 python]# python 14.py5050
10:xrange(10) 返回的是一个object对象, 类型显示xrange只能用for 循环来对对象遍历查看;取每个值:
In [75]: a = xrange(10)In [76]: aOut[76]: xrange(10)In [77]: type(a)Out[77]: xrangeIn [78]: for i in a: ...: print i ...: 0123456789
注释:range 和 xrange 的区别:
range(100) 是一个列表,里面是元素,这样子会占用内存,
xrange(100) 产生的是一个对象,不会占用内存,只有在遍历对象的时候查看值,才会占用内存;