一、if

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


所有非零数

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


作用:

(1)判断一个字符串是否为空

(2)判断运算结果是否为0

(3)判断一个表达式是否可用


例子1:

#!/usr/bin/python
x = int(raw_input("please input a number:"))
if x>=30:
        print "A"
elif x>=20:
        print "B"
else:
        print "Bad"


执行结果:

[root@localhost tmp]# python if.py
please input a number:33
A
[root@localhost tmp]# python if.py
please input a number:24
B
[root@localhost tmp]# python if.py
please input a number:1
Bad


例子2:

#!/usr/bin/python
x = int(raw_input("please input a number:"))
y = int(raw_input("please input a number:"))
if x>=30 and y>= 30:
        print "A"
elif x>=20:
        print "B"
else:
        print "Bad"


执行结果:

[root@localhost tmp]# python if.py
please input a number:44
please input a number:44
A
[root@localhost tmp]# python if.py
please input a number:20
please input a number:20
B


二、for

例子1:

#!/usr/bin/python
for x in "hello":
    print x
[root@web1 python]# python 1.py
h
e
l
l
o


例子2: 通过索引遍历字符串

#!/usr/bin/python
fruits = ['apple','banana','mango']
for index in range(len(fruits)):
    print 'Current fruit:',fruits[index]
print "Goodbye!"
[root@web1 python]# python 3.py
Current fruit: apple
Current fruit: banana
Current fruit: mango
Goodbye!


例子3: python中的for 有else 的关键字:(只在for循环正常结束后执行else语句)

#!/usr/bin/python
import time
for x in range(5):
        print x
        time.sleep(1)
else:
        print "End"
[root@web1 python]# python 4.py
0
1
2
3
4
End


不正常结束:(执行到中途按crtl+c 强制结束,可以看到else语句是没有执行的

[root@web1 python]# python 4.py
0
1
^CTraceback (most recent call last):
  File "4.py", line 5, in 
    time.sleep(1)
KeyboardInterrupt


例子4:break 关键字

#!/usr/bin/python
for x in range(1,11):
        print x
        if x == 6:
                break
else:
        print "End"
[root@web1 python]# python 5.py
1
2
3
4
5
6


例子5:continue 关键字


使用continue可以在单次循环中跳出下面循环语句

#!/usr/bin/python
for x in range(1,11):
        print x
        if x == 2:
                print "hello 22222"
                continue
        print "#"*20
[root@web1 python]# python 5.py
1
####################
2
hello 22222
3
####################



例子6:假如我要判断x=3但是,我还没想好满足条件要执行什么,这时候不能留空,而应该加个pass的关键字

#!/usr/bin/python
for x in range(1,11):
        print x
        if x == 3:
                pass


三、while

例子1:

#!/usr/bin/python
while True:
        x = raw_input("please input a number:")
        print x
        if x == '10':
                break
[root@web1 python]# python 6.py
please input a number:12
12
please input a number:34
34
please input a number:10
10


也可以这样:

#!/usr/bin/python
x = ""
while not x = "q":
        x = raw_input("please input a number:")
        print x


四、switch

switch 语句用于编写多分支结构的程序,类似于if... elif ... else 语句

switch 语句表达的分支结构比if... elif ... else 语句表达的更清晰,代码可读性更高但是python 并没有提供switch 语句,但我们可以通过字典实现switch 语句的功能,:

1、定义一个字典

2、调用字典的get()获取相应的表达式


例子:

(1)通过if... elif ... else实现

from __future__ import division
def jia(x,y):
        return x+y
def jian(x,y):
        return x-y
def cheng(x,y):
        return x*y
def chu(x,y):
        return x/y
def operator(x, o, y):
        if o == "+":
                print jia(x,y)
        elif o == "-":
                print jian(x,y)
        elif o == "*":
                print cheng(x,y)
        elif o == "/":
                print chu(x,y)
        else:
                pass
operator(10,"/",2)


(2)通过switch 实现

from __future__ import division
def jia(x,y):
        return x+y
def jian(x,y):
        return x-y
def cheng(x,y):
        return x*y
def chu(x,y):
        return x/y
operator = {"+":jia, "-":jian, "*":cheng, "/":chu}
def f(x, o, y):
        print operator.get(o)(x,y)
f(3, "/", 2)


可以看出通过if 来实现的执行效率并不高,比如,一个除法的操作,需要判断4次才能匹配到条件,而通过switch 则不用执行判断