forked from zjucsxxd/python-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10.func.py
More file actions
133 lines (91 loc) · 2.64 KB
/
10.func.py
File metadata and controls
133 lines (91 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# -*- coding: utf-8 -*-
"""
@description:
@author:XuMing
"""
from __future__ import print_function
from __future__ import unicode_literals
def add(a, b):
"""
add two nums
:param a: first num
:param b: second num
:return: result
"""
c = a + b
return c
# 使用函数
# 使用函数时,只需要将参数换成特定的值传给函数。
# Python并没有限定参数的类型,因此可以使用不同的参数类型:
print(add(2, 3))
print(add('foo', 'bar')) # foobar
# 传入参数时,Python提供了两种选项,
# 第一种是上面使用的按照位置传入参数,
# 另一种则是使用关键词模式,显式地指定参数的值:
print(add(a=2, b=3))
print(add(b='morning', a='good'))
print(add(2, b=3)) # 5
# 设定默认参数
def quad(x, a=1, b=0, c=0):
return a * x * x + b * x + c
print(quad(2.0))
print(quad(2.0, b=3))
# 接收不定参数
# 使用如下方法,可以使函数接受不定数目的参数,类似java的..多个参数:
def add(x, *args):
total = x
for arg in args:
total += arg
return total
# *args 表示参数数目不定,可以看成一个元组,
# 把第一个参数后面的参数当作元组中的元素。
print(add(1, 2, 3, 4, 5)) # 15
print(add(1, 2)) # 3
# 使用关键词传入参数
def add(x, **kwargs):
total = x
for arg, val in kwargs.items():
print("adding ", arg)
total += val
return total
# **kwargs 表示参数数目不定,相当于一个字典,关键词和值对应于键值对。
print(add(1, a=2, b=3)) # 6
# 可以接收任意数目的位置参数和键值对参数:
def fun1(*args, **kwargs):
print(args, kwargs)
fun1(2, 3, a="bar", b=10) # (2, 3) {'a': u'bar', 'b': 10}
# 返回多个值
# 函数可以返回多个值:
def to_val(x, y):
r = (x ** 2 + y ** 2) ** 0.5
total = x + y
return r, total
a, b = to_val(3, 4)
print(a, b) # 5.0 7
# 事实上,Python将返回的两个值变成了元组:
print(to_val(3, 4)) # (5.0, 7)
# 列表也有相似的功能,可以用来赋值:
a, b, c = [1, 2, 3]
print(a, b, c)
# 可以将参数用元组传入:
def add(a, b):
return a + b
c = (2, 3)
print(add(*c)) # 5
# 这里的*必须要。
# 还可以用字典传入参数哦:
d = {'a': 2, 'b': 5}
print(add(**d)) # 7
# map 方法生成序列
# map(aFun, aSeq)
def sqr(x):
return x ** 2
a = [2, 3, 4]
print(map(sqr, a)) # [4,9,16]
# 事实上,根据函数参数的多少,map 可以接受多组序列,
# 将其对应的元素作为参数传入函数:
def add(a, b):
return a + b
a = (2, 3, 4)
b = [10, 11, 15]
print(map(add, a, b)) # [12, 14, 19]