python学习笔记3—流程控制if、f

  • 2020 年 1 月 10 日
  • 筆記

流程控制if

if 语句

if expression:

    statement(s)

else

else语句:

if 语句,else语句

if expression:

    statement(s)

else:

    statement(s)

elif语句:

if expression1:

    statement1(s)

elif expression2(s):

    statements2(s)

else:

    statement2(s)

注:Python使用缩进作为其语法分组的方法,建议使用4个空格

逻辑值(bool)包含了两个值:

True:表示非空的量(比如:string,tuple,list,set,dictonary)所有非零数。

false:表示0,None,空的量等,包括空的元组、空的列表、空的字典。

[root@localhost ~]# vim 1.py  #!/usr/bin/python  score = int(raw_input("Please input you score"))  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 fail'  print 'End'  [root@localhost ~]# python 1.py   Please input you score90  A  very good  End  [root@localhost ~]# python 1.py   Please input you score80  B  good  End  [root@localhost ~]# python 1.py   Please input you score70  C  pass  End  [root@localhost ~]# python 1.py   Please input you score10  D fail  End
[root@localhost ~]# vim 2.py   #!/usr/bin/python  yn = raw_input("Please input [Yes/No]")  yn = yn.lower()  if yn == 'y' or yn == 'yes':      print "program is running"  elif yn == 'n' or yn == 'no':      print "program is exit"  else:      print "please input [Yes/No]"        [root@localhost ~]# python 2.py   Please input [Yes/No]yes  program is running  [root@localhost ~]# python 2.py   Please input [Yes/No]no  program is exit

流程控制for循环

for循环:

在序列里面,使用for循环遍历

语法:

for interating_var in sequence:

    statement(s)

#对序列做一个遍历  [root@localhost ~]# vim 3.py  #!/usr/bin/python  for i in range(1,11):      print (i)  [root@localhost ~]# python 3.py   1  2  3  4  5  6  7  8  9  10    #打印range(1,11),也就是打印1-10  [root@localhost ~]# vim 3.py  #!/usr/bin/python  print [i for i in range(1,11)]  [root@localhost ~]# python 3.py   [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]    #打印range(1,11)*2,也就是打印(1-10)*2  [root@localhost ~]# vim 3.py  #!/usr/bin/python  print [i*2 for i in range(1,11)]  [root@localhost ~]# python 3.py   [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]    #判断是不是偶数,是的话放在列表中,并打印出来  [root@localhost ~]# vim 3.py  #!/usr/bin/python  print [i for i in range(1,11) if i%2 == 0 ]  [root@localhost ~]# python 3.py   [2, 4, 6, 8, 10]    #打印range(1,11)的三次方,也就是打印(1-10)的三次方  [root@localhost ~]# vim 3.py  #!/usr/bin/python  print [i**3 for i in range(1,11)]  [root@localhost ~]# python 3.py   [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]    #列表重写,打印range(1,11)的三次方,也就是打印(1-10)的三次方  [root@localhost ~]# vim 3.py  #!/usr/bin/python  for j in [i**3 for i in range(1,11)]:      print j,  [root@localhost ~]# python 3.py   1 8 27 64 125 216 343 512 729 1000    #列表重写,只是奇数的range(1,11)的三次方,打印1,3,5,7,9的三次方  [root@localhost ~]# vim 3.py  #!/usr/bin/pytho  for j in [i**3 for i in range(1,11) if i % 2 !=0 ]:      print j,  [root@localhost ~]# python 3.py   1 27 125 343 729    xrange object对象,不占用内存,遍历才能取到值

#用shell实现乘法口诀,", print"  ,表示不换行,print表示换行

[root@localhost ~]# vim 6.py  #!/usr/bin/python  for i in xrange(1,10):      for j in xrange(1,i+1):          print "%s * %s = %s" %(j, i, j*i),      print  [root@localhost ~]# python 6.py   1 * 1 = 1  1 * 2 = 2 2 * 2 = 4  1 * 3 = 3 2 * 3 = 6 3 * 3 = 9  1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16  1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25  1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36  1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49  1 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64  1 * 9 = 9 2 * 9 = 18 3 * 9 = 27 4 * 9 = 36 5 * 9 = 45 6 * 9 = 54 7 * 9 = 63 8 * 9 = 72 9 * 9 = 81

for循环

for

else

for循环如果正常结束,才会执行else语句,

break退出整个循环,break异常退出不执行else内容,

continue退出当前的循环,不执行循环语句内容

pass是占位

import sys    sys.exit()退出整个脚本

import time   [root@localhost ~]# vim 7.py

range(10)直接分配,占用内存资源

b=xrange(10)产生对象,只有对对象做遍历,才能产生值,才会占用内存资源

#!/usr/bin/python  for i in xrange(10):      print i  else:      print 'main end  [root@localhost ~]# python 7.py   0  1  2  3  4  5  6  7  8  9  main end  #ctrl+C或者break异常退出不执行else内容  [root@localhost ~]# vim 7.py  #!/usr/bin/python  import time  for i in xrange(10):      print i      time.sleep (1)      if i==5:          break  else:      print 'main end  [root@localhost ~]# python 7.py   0  1  2  3  4  5

#continue跳出本次i=3的循环,继续i=4的循环

[root@localhost ~]# vim 7.py  #!/usr/bin/python  import time  for i in xrange(10):      if i==3:          continue      elif i==5:          break      elif i==6:              pass                  print i  else:      print 'main end'  [root@localhost ~]# python 7.py   0  1  2  4    [root@133 filevalue]# vim for_else.py     #!/usr/bin/python    import time  for i in xrange(10):      if i == 3:  #i=3的时候跳出本次循环,继续下一次循环,执行i=4          continue      elif i == 5:          break    #i=5的时候break直接跳出循环      elif i == 6:          pass      time.sleep(1)      print i  else:      print "main end"        [root@133 filevalue]# python for_else.py   0  1  2  4

现在让我们用python写一个练习题:

系统随机生成一个20以内的随机整数,

玩家有6次机会进行猜猜看,每次猜测都有反馈(猜大了,猜小了,猜对了,结束)

6次中,猜对了,玩家赢了

否则,系统赢了

[root@133 filevalue]# vim random20.py  #!/usr/bin/python  import random  import sys    print 'The random have been created'  random_num = random.randint(1,20)    for i in xrange(1,7) :      enter_num = int(raw_input('Please input guess num'))      if random_num < enter_num:          print 'biger'      elif random_num > enter_num:          print 'lower'      elif random_num == enter_num:          print 'you are win'          sys.exit()  print 'game over'  [root@133 filevalue]# python random20.py   The random have been created  Please input guess num10  lower  Please input guess num15  lower  Please input guess num18  you are win

while与for相比

for循环用在有次数的循环上

while循环用在有条件的控制上

while循环

while循环,直到表达式为假,才退出while循环,表达式是一个逻辑表达式,必须返回一个True或者False,注意是大写,不是小写

语法:

while expression:

    statement(s)

[root@localhost ~]# vim 11.py  #!/usr/bin/python  n=0  while True:      if n==10:          break      print n,'hello'      n+=1  [root@localhost ~]# python 11.py   0 hello  1 hello  2 hello  3 hello  4 hello  5 hello  6 hello  7 hello  8 hello  9 hell

while循环可以使用break退出,例子是用户输入=q的时候,break退出

[root@localhost ~]# vim 11.py  #!/usr/bin/python  while True:      print 'hello'      input = raw_input("please input somethong,q for quit:")      if input=="q":          break  [root@localhost ~]# python 11.py   hello  please input somethong,q for quit:a  hello  please input somethong,q for quit:b  hello  please input somethong,q for quit:q    [root@localhost ~]# vim 11.py  #!/usr/bin/python  x=''  while x!='q': #输入值不为q,x!='q'为真True,输出hello,输入为q,x!='q'为假False,执行else,输出hello world      print 'hello'      x = raw_input("please input something,q for quit:")      if not x: #当输入为空时,not x为true,if true break,直接跳出          break  else:      print "hello world"  [root@localhost ~]# python 11.py   hello  please input somethong,q for quit:a  hello  please input somethong,q for quit:q  hello world  [root@localhost ~]# python 11.py   hello  please input somethong,q for quit:
#用continue,条件满足,不执行continue的循环内容,重新开始循环  [root@localhost ~]# vim 11.py  #!/usr/bin/python  x=''  while x!='q':      print 'hello'      x = raw_input("please input somethong,q for quit:")      if not x:          break      if x=='quit':          continue      print 'continue'  else:      print "hello world"  [root@localhost ~]# python 11.py   hello  please input somethong,q for quit:a  continue  hello  please input somethong,q for quit:quit  hello  please input somethong,q for quit:q  continue   hello world #x=q,所以不执行while循环,而是执行esle: 输出hello world