我是靠谱客的博主 羞涩牛排,这篇文章主要介绍Python函数(入门6)Python函数,现在分享给大家,希望可以做个参考。

转载请标明出处:
http://www.cnblogs.com/why168888/p/6407970.html

本文出自:【Edwin博客园】


Python函数

1. Python之调用函数

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
print abs(100) print abs(-20) print abs(12.34) print cmp(1, 2) print cmp(2, 1) print cmp(3, 3) print int('123') print int(12.34) print str(123) print str(1.23) L = [] x = 1 while x <= 100: L.append(x * x) x = x + 1 print sum(L)

2. Python之编写函数

复制代码
1
2
3
4
5
6
7
def square_of_sum(L): sum = 0 for x in L: sum = sum + x * x return sum print square_of_sum([1, 2, 3, 4, 5]) print square_of_sum([-5, 0, 5, 15, 25])

3. Python函数之返回多值

复制代码
1
2
3
4
5
6
7
8
x = (-b±√(b²-4ac)) / 2a import math def quadratic_equation(a, b, c): t = math.sqrt(b * b - 4 * a * c) return (-b + t) / (2 * a),( -b - t )/ (2 * a) print quadratic_equation(2, 3, 0) print quadratic_equation(1, -6, 5)

4. Python之递归函数

复制代码
1
2
3
4
5
6
7
8
def move(n, a, b, c): if n ==1: print a, '-->', c return move(n-1, a, c, b) print a, '-->', c move(n-1, b, a, c) move(4, 'A', 'B', 'C')

5. Python之定义默认参数

复制代码
1
2
3
4
def greet(name='world'): print 'Hello, ' + name + '.' greet() greet('Bart')

6. Python之定义可变参数

复制代码
1
2
3
4
5
6
7
8
9
10
def average(*args): sum = 0.0 if len(args) == 0: return sum for x in args: sum = sum + x return sum / len(args) print average() print average(1, 2) print average(1, 2, 2, 3, 4)

转载于:https://www.cnblogs.com/why168888/p/6407970.html

最后

以上就是羞涩牛排最近收集整理的关于Python函数(入门6)Python函数的全部内容,更多相关Python函数(入门6)Python函数内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部