python基础(3)—— 程序结构

    python和其他的编程语言一样,也有三种程序结构。顺序结构,选择结构,循环结构。

1.顺序结构

    顺序结构按照顺序执行程序,不做过多解释。

2.选择结构

    2.1 if 语句

        if condition:

            expression

        示例:

[root@willispython]# cat if.py

#!/usr/bin/env python

if 3 < 5:

print "3 less than 5" # 语句块里面可以是多个语句if 3 > 4:

print "3 greate than 4"

[root@yeahmonitor python]# ./if.py

3 less than 5

    2.2else 子句

        if condition:

            expression

        else:

            expression

        if语句里面还可以嵌套if.python是允许语句的嵌套的。else子句总是if语句的最后一个分支。不能出现在elif子句前面。

    2.3 elif 字句

        if condition:

            expresssion

        elif condition:

            expression

        # 可以有多个elif 子句, 但是一旦其中一个分支没执行了,就不会往下再去匹配执行了。

python中没有switch语句。只能用elif模拟switch.

    2.4 逻辑值(bool)

       用来表示诸如:对于错,真与假,空与非空等概念

逻辑值包括两个值:

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

False:表示 0,none,空的量等

作用:主要用于判断语句中,用来判断

一个字符串是否是空的

一个运算结果是否是零

一个表达式是否可用

>>> if True:

... print "ok"

...

ok

>>> if False:

... print "ok"

...

>>> if 1:

... print "ok"

...

ok

>>> if 0:

... print "ok"

...

>>>

例1:

#!/usr/bin/python

def fun():

     return 1

if fun():

    print "ok"

else:print "no"

    if 和 else 中间不可以出现其他不相关的代码

例子2:


#!/usr/bin/python

x=int(raw_input("please input x: "))

y=int(raw_input("please input y: "))

if x>=90:       多个条件判断

    if y>=90:

        print "A"

elif x>=80:

    print "B"

elif x>=70:

    print "C"

else:

    print "no"

例子3:逻辑结构 and, or, not

#!/usr/bin/python

x=int(raw_input("please input x: "))

y=int(raw_input("please input y: "))

if x>=90 and y>=90:print "A"

    elif x>=80:

        print "B"

elif x>=70:

        print "C"

else:

        print "no"


3.循环结构

    3.1while循环

    while 循环

#!/usr/bin/python

    while True:

        print "hello"  #死循环

    

while 循环一定要有条件

#!/usr/bin/python

while True:

    print "hello"

x = raw_input("please input q for quit")

    if x == "q":

    break

或者

#!/usr/bin/python

当 x=q 时循环结束

exit()函数可以跳出整个程序x = ""

while x != "q":

    print "hello"

    x = raw_input("please input q for quit")

又或者

#!/usr/bin/python

    x = ""

    while x != "q":

        print "hello"

    x = raw_input("please input q for quit")

    if not x : 直接回车退出

        break

#!/usr/bin/python

    x = ""

    while x != "q":

        print "hello"

    x = raw_input("please input q for quit")

    if not x : 直接回车退出

        break

    if x == "c": 输入 c 继续循环

        continue

    print "hello world" 如果两次 if 都不满足 print

    else:

        print "ending...."

 while 条件判断失败执行 else 如果是 break 不执行 else

 

   3.2for循环

    for 循环

    for x in [1,2,3,4]

        print x


    迭代序列指数(索引)

    for x in range(100)     默认从 0 开始

        print x


    for x in range(1,11) 不包含最后一个值 11

        print x


    for x in range(1,11,2) 2 为步进值,不指默认为 1

        print x


    #!/usr/bin/python

    num=0

    for x in range(1,101)

        num += x

    print num


    遍历序列

    s="willis"

    for x in range(len(s))

        print s[x]


    l=[1,2,3,'a','b']

    for x in l:

        if x>=2:

    print x


    d={1:111,2:222,3:333}for x in d:

    print d[x]


    for k,v in d.items():

        print k

        print v


    for x in range(1,11):

        print x

    if x==2:

        pass #代码桩,站位

    if x==3:

        print "hello"

        continue

    if x==6:

        #continue 跳过当次循环的余下语句进入下次循环

        break

        #break 结束本次循环

    print "#"*50

 else:

    print "ending"

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值