Python中的流程控制:if判断、while、for循环实例

一.Python中的流程控制--if判断语句

1.if 用法举例:

    if语句写法:   

 if expression:

    statement(s)

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

(1)条件为真true (非空的量(string,tuple,list ,set,dictonary),所有非零的数):

    if 1:

        print 'hello world!'

        print 'True'

            

    if 'aaa':

        print 'hello world!'

        print 'True'

(2)条件为假 faulse(0,None,空的量):    

    if  0:

        print 'hello world!'

        print 'True'

   

    if None:

        print 'hello world!'

        print 'True'

   

    

    if  '':

        print 'hello world!'

        print 'True'

   
     if  1>2:

        print 'hello world!'

        print 'True'

(3)组合条件及其他(and /or ):    

    if  not 1>2:

        print 'hello world!'

        print 'True'
    if  not 1>2 and 1 == 1:

        print 'hello world!'

        print 'True'

2.if   else 举例:

if  else写法:

    else语句:

    if expression:

        statement(s)

    else:

        statement(s)
  if 1 < 2:

        print 'hello world'

    else:

        print 'Oh,no,fourse!'

    print 'main'

 

3.if   elif   else写法:

elfi 语句:    

if expression1:

        statement1(s)

    elif expression2:

        statement2(s)

    else:

        statement3(s)
    if 1 < 2:

        print 'hello world'

    elif 'a':

        print 'aaaaa'

    else:

        print 'Oh,no,fourse!'


 

4.举例1:    

#!/usr/bin/env python

    score =int( raw_input(‘Please input a num:’))

    if score >= 90:

        print 'A'

        print 'Very good'

    elif score >=80:

        print 'B'

        print 'good'

    elif score >=60:

        print 'C'

        print 'pass'

    else:

        print 'D'

    print 'END'

5.举例2:and or 应用:

多个条件下判断:

转换大小写:

    a.lower()

    a.upper()   

    #!/usr/bin/env python

    yn = raw_input("Please input [Yes/No]:")

    yn = yn.lower()

    if yn == 'y' or yn == 'yes':

        print "Programe is running..."

    elif yn == 'n' or yn == 'no':

        print "Programe is exit."

    else:

        print "Error,Please input [Yes/No]"

 

二、流程控制-for循环

循环

    循环是一个结构,导致程序要重复一定的次数。

    条件下循环也是如此,当然条件变为假,循环结束。

for循环:

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

语法:    

for iterating_var in sqquence:

    statement(s)

举例:

(例1)for用法举例   

>>> a = "ABC"
>>> for i in a:
...     print(i)
...     
A
B
C

(例2)list的for循环   

>>> list1 = [1,3,4,5]
>>> for i in list1:
...     print(i)
...     
1
3
4
5

(例3)range()函数用法:

    最在取到5

>>> for i in range(1,6):
...     print(i)
...     
1
2
3
4
5

   步长为3 

>>> for i in range(1,11,3):
...     print(i)
...     
1
4
7
10

  求1,10内的偶数:

>>> print ([i for i in range(1,11) if i%2==0])
[2, 4, 6, 8, 10]

  求1到100所有数加到一起的和:

        #!/usr/bin/python

        sum = 0

        for i in range(1,101):

            sum = sum + i

        print sum

    运行结果:       

 [root@localhost python]# python for1.py

  5050

流程控制-for循环(字典)

>>> dic = {'a': '100', 'b': '100', 'c': '100', 'd': '100', 'e': '100', 'f': '100'}
>>> for i in dic.items():print(i)
... 
('a', '100')
('b', '100')
('c', '100')
('d', '100')
('e', '100')
('f', '100')

>>>for k in dic:
    print(k)
    
a
b
c
d
e
f

 

举例乘法口诀:

#!/usr/bin/env python
#python3中执行


for i in range(1,10):

	for j in range(1,i+1):

		print("%sx%s=%s" % (j,i,j*i),end=" ")

	print()

 运行结果:

1x1=1 
1x2=2 2x2=4 
1x3=3 2x3=6 3x3=9 
1x4=4 2x4=8 3x4=12 4x4=16 
1x5=5 2x5=10 3x5=15 4x5=20 5x5=25 
1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36 
1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49 
1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64 
1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81 

三、Python中的while循环举例

while与for相对比:

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

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

while循环:

    while循环,直到表达式变为假,才退出。while循环,表达式是一个逻辑表达式,必须返回一个True或False

语法:

    while expression:

            statement(s)

break :跳出整个循环

continue:跳出本次循环

练习脚本如果下:

脚本1:    

#!/usr/bin/python

n = 0
while 1:
    if n == 10:
        break
    print(str(n)+" hello")
    n += 1

结果:   


0 hello
1 hello
2 hello
3 hello
4 hello
5 hello
6 hello
7 hello
8 hello
9 hello

脚本2:

#!/usr/bin/env python

sth=''
while sth != 'q':
    sth=input("Please input sth,q for exit:")
    if not sth:
        break
    if sth == 'quit':
        continue
        print ('continue')
else:
    print ('GOOD BYE')

 

转载于:https://my.oschina.net/u/3804957/blog/1790110

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值