python_9(获取字典值、函数参数、函数返回值、lambda的函数用法)

如果我们需要获取字典值的话,我们有两种方法,一个是通过dict[‘key’],另外一个就是dict.get()方法

#获取字典值
info = {'1':'first','2':'second','3':'third'}
number = raw_input('input type you number:')
print info.get(number,'error')

#实例1
#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 函数的定义
from __future__ import division
def arithmetic(x, y, operator):
    result = {
        "+" : x + y,
        "-" : x - y,
        "*" : x * y,
        "/" : x / y 
    }
    return result.get(operator)     # 返回计算结果

# 函数的调用
print arithmetic(1, 2, "+")

———————–函数的参数—————————

#!/usr/bin/python
# -*- coding: UTF-8 -*-

from __future__ import division

def append(args=[]):
    if len(args) <= 0:    
        args = []
    args.append(0)
    print args

append()
append([1])
append()

# 函数的缺省参数
def arithmetic(x=1, y=1, operator="+"):
    result = {
        "+" : x + y,
        "-" : x - y,
        "*" : x * y,
        "/" : x / y 
    }
    return result.get(operator)     # 返回计算结果

print arithmetic(1, 2)
print arithmetic(1, 2, "-")
print arithmetic(y=3, operator="-")
print arithmetic(x=4, operator="-")
print arithmetic(y=3, x=4, operator="-")



# 列表作为参数传递
def arithmetic(args=[], operator="+"):
    x = args[0]
    y = args[1]
    result = {
        "+" : x + y,
        "-" : x - y,
        "*" : x * y,
        "/" : x / y 
    }
    return result.get(operator)     # 返回计算结果

print arithmetic([1, 2])

# 传递可变参数
def func(*args):    
    print args

func(1, 2, 3)    

# 传递可变参数
def search(*t, **d):
    keys = d.keys()
    values = d.values()
    print keys
    print values
    for arg in t: 
        for key in keys: 
            if arg == key:
                print "find:",d[key]

search("one", "three", one="1",two="2",three="3")

#输出结果
>>> 
[0]
[1, 0]
[0]
3
-1
-2
3
1
3
(1, 2, 3)
['three', 'two', 'one']
['3', '2', '1']
find: 1
find: 3
>>> 

————————函数返回值——————————

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# return语句
from __future__ import division
def arithmetic(x, y, operator):
    result = {
        "+" : x + y,
        "-" : x - y,
        "*" : x * y,
        "/" : x / y 
    }
    return result.get(operator)     # 返回计算结果

# 没有return语句的函数返回None
def func():
    pass

print func()

# return后不带参数
def func():
    return

print func()

# return返回多个值
def func(x, y, z):
    l = [x, y, z]
    l.reverse()
    numbers = tuple(l)
    return numbers

x, y, z = func(0, 1, 2)
print x, y, z

# return返回多个值
def func(x, y, z):
    l = [x, y, z]
    l.reverse()
    a, b, c = tuple(l)   
    return a, b, c

x, y, z = func(0, 1, 2)
print x, y, z    

# 多个return语句
def func(x):
    if x > 0:
        return "x > 0"
    elif x == 0:
        return "x == 0"
    else:
        return "x < 0"

print func(-2)

# 多个return语句的重构
def func(x):
    if x > 0:
        result = "x > 0"
    elif x == 0:
        result = "x == 0"
    else:
        result = "x < 0"
    return result    

print func(-2)   

——————-嵌套函数、内部函数———————————-

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 嵌套函数
def sum(a, b):
    return a + b
def sub(a, b):
    return a - b
def func():
    x = 1
    y = 2
    m = 3
    n = 4
    return sum(x, y) * sub(m, n)

print func()

# 调用内部函数
def func():
    x = 1
    y = 2
    m = 3
    n = 4
    def sum(a, b):          # 内部函数
        return a + b
    def sub(a, b):          # 内部函数
        return a - b
    return sum(x, y) * sub(m, n)

print func()


# 内部函数直接使用外层函数的变量
def func():
    x = 1
    y = 2
    m = 3
    n = 4
    def sum():              # 内部函数
        return x + y
    def sub():          # 内部函数
        return m - n
    return sum() * sub()

print func()

—————-计算阶乘——————-

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 计算阶乘
def refunc(n):
    i = 1
    if n > 1:                       # 递归的结束判断
        i = n
        n = n * refunc(n-1)         # 递推
    print "%d! =" %i, n
    return n                        # 回归

refunc(5)

# 使用reduce计算阶乘
print "5! =", reduce(lambda x, y: x * y, range(1, 6))

—————-lambda的函数用法—————————-

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# lambda
def func():
    x = 1
    y = 2
    m = 3
    n = 4
    sum = lambda x, y : x + y
    print sum
    sub = lambda m, n : m - n
    print sub
    return sum(x, y) * sub(m, n)

print func()    


# lambda的函数用法   
print (lambda x: -x)(-2)

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 定义Generator函数
def func(n):
    for i in range(n):
        yield i
# 在for循环中输出
for i in func(3):
    print i
# 使用next()输出
r =  func(3)
print r.next()
print r.next()
print r.next()
#print r.next()


# yield与return区别
def func(n):
    for i in range(n):
        return i
def func2(n):
    for i in range(n):
        yield i

print func(3)
f = func2(3)
print f
print f.next() 
print f.next() 
#输出
>>> 
0
1
2
0
1
2
0
0x0000000002B97CF0>
0
1
>>> 

你可能感兴趣的:(Python)