转载请标明出处:
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
21print 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
7def 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
8x = (-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
8def 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
4def greet(name='world'): print 'Hello, ' + name + '.' greet() greet('Bart')
6. Python之定义可变参数
复制代码
1
2
3
4
5
6
7
8
9
10def 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函数内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复