码迷,mamicode.com
首页 > 编程语言 > 详细

广东海洋大学 电子1151 孔yanfei python语言程序设计 第三周

时间:2016-06-21 06:43:51      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:

四、流程控制

     在这块,Python与其它大多数语言有个非常不同的地方,Python语言使用缩进块来表示程序逻辑(其它大多数语言使用大括号等)。例如:

if age < 21:

print("你不能买酒。")

print("不过你能买口香糖。")

print(“这句话处于if语句块的外面。”)

     这个代码相当于c语言的:

if (age < 21)

{

print("你不能买酒。")

print("不过你能买口香糖。")

}

print(“这句话处于if语句块的外面。”)

   可以看到,Python语言利用缩进表示语句块的开始和退出(Off-side规则),而非使用花括号或者某种关键字。增加缩进表示语句块的开始(注意前面有个:号),而减少缩进则表示语句块的退出。根据PEP的规定,必须使用4个空格来表示每级缩进(不清楚4个空格的规定如何,在实际编写中可以自定义空格数,但是要满足每级缩进间空格数相等)。使用Tab字符和其它数目的空格虽然都可以编译通过,但不符合编码规范。

   为了使我们自己编写的程序能很好的兼容别人的程序,我们最好还是按规范来,用四个空格来缩减(注意,要么都是空格,要是么都制表符,千万别混用)。

1、if-else

     If-else用来判断一些条件,以执行满足某种条件的代码。

[python] view plain copy
在CODE上查看代码片派生到我的代码片

################################  
######## procedure control #####  
## if else  
if expression: # bool type and do not forget the colon  
    statement(s) # use four space key   

if expression:   
statement(s) # error!!!! should use four space key   

if 1<2:  
    print ‘ok, ‘ # use four space key  
    print ‘yeah‘ # use the same number of space key  

if True: # true should be big letter True  
    print ‘true‘  

def fun():  
    return 1  

if fun():  
    print ‘ok‘  
else:  
    print ‘no‘  

con = int(raw_input(‘please input a number:‘))  
if con < 2:  
    print ‘small‘  
elif con > 3:  
    print ‘big‘  
else:  
    print ‘middle‘  

if 1 < 2:  
    if 2 < 3:  
        print ‘yeah‘  
    else:  
        print ‘no‘    
    print ‘out‘  
else:  
    print ‘bad‘  

if 1<2 and 2<3 or 2 < 4 not 0: # and, or, not  
    print ‘yeah‘  

2、for

     for的作用是循环执行某段代码。还可以用来遍历我们上面所提到的序列类型的变量。

[python] view plain copy
在CODE上查看代码片派生到我的代码片

################################  
######## procedure control #####  
## for  
for iterating_val in sequence:  
    statements(s)  
# sequence type can be string, tuple or list  

for i in "abcd":  
    print i  

for i in [1, 2, 3, 4]:  
    print i  

# range(start, end, step), if not set step, default is 1,   
# if not set start, default is 0, should be noted that it is [start, end), not [start, end]  
range(5) # [0, 1, 2, 3, 4]  
range(1, 5) # [1, 2, 3, 4]  
range(1, 10, 2) # [1, 3, 5, 7, 9]  
for i in range(1, 100, 1):   
    print i  

# ergodic for basis sequence  
fruits = [‘apple‘, ‘banana‘, ‘mango‘]  
for fruit in range(len(fruits)):   
    print ‘current fruit: ‘, fruits[fruit]  

# ergodic for dictionary  
dic = {1: 111, 2: 222, 5: 555}  
for x in dic:  
    print x, ‘: ‘, dic[x]  

dic.items() # return [(1, 111), (2, 222), (5, 555)]  
for key,value in dic.items(): # because we can: a,b=[1,2]  
    print key, ‘: ‘, value  
else:  
    print ‘ending‘  

################################  
import time  
# we also can use: break, continue to control process  
for x in range(1, 11):  
    print x  
    time.sleep(1) # sleep 1s  
    if x == 3:  
        pass # do nothing  
    if x == 2:  
        continue  
    if x == 6:  
        break  
    if x == 7:    
        exit() # exit the whole program  
    print ‘#‘*50  

3、while

     while的用途也是循环。它首先检查在它后边的循环条件,若条件表达式为真,它就执行冒号后面的语句块,然后再次测试循环条件,直至为假。冒号后面的缩近语句块为循环体。

[python] view plain copy
在CODE上查看代码片派生到我的代码片

################################  
######## procedure control #####  
## while  
while expression:  
    statement(s)  

while True:  
    print ‘hello‘  
    x = raw_input(‘please input something, q for quit:‘)  
    if x == ‘q‘:  
        break  
else:  
    print ‘ending‘  

4、switch

     其实Python并没有提供switch结构,但我们可以通过字典和函数轻松的进行构造。例如:

[python] view plain copy
在CODE上查看代码片派生到我的代码片

#############################  
## switch ####  
## this structure do not support by python  
## but we can implement it by using dictionary and function  
## cal.py ##  
#!/usr/local/python  

from __future__ import division  
# if used this, 5/2=2.5, 6/2=3.0  

def add(x, y):  
    return x + y  
def sub(x, y):  
    return x - y  
def mul(x, y):  
    return x * y  
def div(x, y):  
    return x / y  

operator = {"+": add, "-": sub, "*": mul, "/": div}  
operator["+"](1, 2) # the same as add(1, 2)  
operator["%"](1, 2) # error, not have key "%", but the below will not  
operator.get("+")(1, 2) # the same as add(1, 2)  

def cal(x, o, y):  
    print operator.get(o)(x, y)  
cal(2, "+", 3)  
# this method will effect than if-else  

五、函数

1、自定义函数

     在Python中,使用def语句来创建函数:

[python] view plain copy
在CODE上查看代码片派生到我的代码片

################################  
######## function #####   
def functionName(parameters): # no parameters is ok  
    bodyOfFunction  

def add(a, b):  
    return a+b # if we do not use a return, any defined function will return default None   

a = 100  
b = 200  
sum = add(a, b)  

##### function.py #####  
#!/usr/bin/python  
#coding:utf8  # support chinese  
def add(a = 1, b = 2): # default parameters  
    return a+b  # can return any type of data  
# the followings are all ok  
add()  
add(2)  
add(y = 1)  
add(3, 4)  

###### the global and local value #####  
## global value: defined outside any function, and can be used  
##              in anywhere, even in functions, this should be noted  
## local value: defined inside a function, and can only be used  
##              in its own function  
## the local value will cover the global if they have the same name  
val = 100 # global value  
def fun():  
    print val # here will access the val = 100  
print val # here will access the val = 100, too  

def fun():  
    a = 100 # local value  
    print a  
print a # here can not access the a = 100  

def fun():  
    global a = 100 # declare as a global value  
    print a  

print a # here can not access the a = 100, because fun() not be called yet  
fun()  
print a # here can access the a = 100  

############################  
## other types of parameters  
def fun(x):  
    print x  
# the follows are all ok  
fun(10) # int  
fun(‘hello‘) # string  
fun((‘x‘, 2, 3))  # tuple  
fun([1, 2, 3])    # list  
fun({1: 1, 2: 2}) # dictionary  

## tuple  
def fun(x, y):  
    print "%s : %s" % (x,y) # %s stands for string  
fun(‘Zou‘, ‘xiaoyi‘)  
tu = (‘Zou‘, ‘xiaoyi‘)  
fun(*tu)    # can transfer tuple parameter like this  

## dictionary  
def fun(name = "name", age = 0):  
    print "name: %s" % name  
    print "age: " % age  
dic = {name: "xiaoyi", age: 25} # the keys of dictionary should be same as fun()  
fun(**dic) # can transfer dictionary parameter like this  
fun(age = 25, name = ‘xiaoyi‘) # the result is the same  
## the advantage of dictionary is can specify value name  

#############################  
## redundancy parameters ####  
## the tuple  
def fun(x, *args): # the extra parameters will stored in args as tuple type   
    print x  
    print args  
# the follows are ok  
fun(10)  
fun(10, 12, 24) # x = 10, args = (12, 24)  

## the dictionary  
def fun(x, **args): # the extra parameters will stored in args as dictionary type   
    print x  
    print args  
# the follows are ok  
fun(10)  
fun(x = 10, y = 12, z = 15) # x = 10, args = {‘y‘: 12, ‘z‘: 15}  

# mix of tuple and dictionary  
def fun(x, *args, **kwargs):  
    print x  
    print args  
    print kwargs  
fun(1, 2, 3, 4, y = 10, z = 12) # x = 1, args = (2, 3, 4), kwargs = {‘y‘: 10, ‘z‘: 12}  

2、Lambda函数

     Lambda函数用来定义一个单行的函数,其便利在于:

[python] view plain copy
在CODE上查看代码片派生到我的代码片

#############################  
## lambda function ####  
## define a fast single line function  
fun = lambda x,y : x*y # fun is a object of function class  
fun(2, 3)  
# like  
def fun(x, y):  
    return x*y  

## recursion  
# 5=5*4*3*2*1, n!  
def recursion(n):  
    if n > 0:  
        return n * recursion(n-1) ## wrong  

def mul(x, y):  
    return x * y  
numList = range(1, 5)  
reduce(mul, numList) # 5! = 120  
reduce(lambda x,y : x*y, numList) # 5! = 120, the advantage of lambda function avoid defining a function  

### list expression  
numList = [1, 2, 6, 7]  
filter(lambda x : x % 2 == 0, numList)  
print [x for x in numList if x % 2 == 0] # the same as above  
map(lambda x : x * 2 + 10, numList)  
print [x * 2 + 10 for x in numList] # the same as above  

3、Python内置函数

   Python内置了很多函数,他们都是一个个的.py文件,在python的安装目录可以找到。弄清它有那些函数,对我们的高效编程非常有用。这样就可以避免重复的劳动了。下面也只是列出一些常用的:

[python] view plain copy
在CODE上查看代码片派生到我的代码片

###################################  
## built-in function of python ####  
## if do not how to use, please use help()  
abs, max, min, len, divmod, pow, round, callable,  
isinstance, cmp, range, xrange, type, id, int()  
list(), tuple(), hex(), oct(), chr(), ord(), long()  

callable # test a function whether can be called or not, if can, return true  
# or test a function is exit or not  

isinstance # test type  
numList = [1, 2]  
if type(numList) == type([]):  
    print "It is a list"  
if isinstance(numList, list): # the same as above, return true  
    print "It is a list"  

for i in range(1, 10001) # will create a 10000 list, and cost memory  
for i in xrange(1, 10001)# do not create such a list, no memory is cost  

## some basic functions about string  
str = ‘hello world‘  
str.capitalize() # ‘Hello World‘, first letter transfer to big  
str.replace("hello", "good") # ‘good world‘  
ip = "192.168.1.123"  
ip.split(‘.‘) # return [‘192‘, ‘168‘, ‘1‘, ‘123‘]  
help(str.split)  

import string  
str = ‘hello world‘  
string.replace(str, "hello", "good") # ‘good world‘  

## some basic functions about sequence  
len, max, min  
# filter(function or none, sequence)  
def fun(x):  
    if x > 5:  
        return True  
numList = [1, 2, 6, 7]  
filter(fun, numList) # get [6, 7], if fun return True, retain the element, otherwise delete it  
filter(lambda x : x % 2 == 0, numList)  
# zip()  
name = ["me", "you"]  
age = [25, 26]  
tel = ["123", "234"]  
zip(name, age, tel) # return a list: [(‘me‘, 25, ‘123‘), (‘you‘, 26, ‘234‘)]  
# map()  
map(None, name, age, tel) # also return a list: [(‘me‘, 25, ‘123‘), (‘you‘, 26, ‘234‘)]  
test = ["hello1", "hello2", "hello3"]  
zip(name, age, tel, test) # return [(‘me‘, 25, ‘123‘, ‘hello1‘), (‘you‘, 26, ‘234‘, ‘hello2‘)]  
map(None, name, age, tel, test) # return [(‘me‘, 25, ‘123‘, ‘hello1‘), (‘you‘, 26, ‘234‘, ‘hello2‘), (None, None, None, ‘hello3‘)]  
a = [1, 3, 5]  
b = [2, 4, 6]  
def mul(x, y):  
    return x*y  
map(mul, a, b) # return [2, 12, 30]  
# reduce()  
reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) # return ((((1+2)+3)+4)+5)  

广东海洋大学 电子1151 孔yanfei python语言程序设计 第三周

标签:

原文地址:http://blog.csdn.net/sinat_32097435/article/details/51720619

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!