-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc_args.py
70 lines (55 loc) · 1.45 KB
/
func_args.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from collections import Iterable
def my_abs(x):
if not isinstance(x, (int, float)):
raise TypeError('bad operand type')
if x >= 0:
return x
else:
return -x
def power(x, n=2):
s = 1
while n > 0:
s *= x
n -= 1
print(pow(2, 4))
def add_end(L=None):
if L is None:
L = []
L.append('END')
return L
def calc(*numbers):
sum = 0
for n in numbers:
sum += n * n
return sum
L = [1, 2]
print(calc(*L))
def f1(a, b, c=0, *args, **kw):
print('a=', a, 'b=', b, 'c=', c, 'args=', args, 'kw=', kw)
def f2(a, b, c=0, *, d, **kw):
print('a=', a, 'b=', b, 'c=', c, 'd=', d, 'kw=', kw)
def print_scores(**kw):
print(' Name Score ')
print('------------------')
for name, score in kw.items():
print('%10s %d' % (name, score))
print()
print_scores(Adam=99, Lisa=88, Bart=77)
data = {
'Adam Lee': 99,
'Lisa S': 88,
'F.Bart': 77
}
print_scores(**data)
def print_info(name, *, gender, city='Beijing', age):
print('Personal Info')
print('---------------')
print(' Name: %s' % name)
print(' Gender: %s' % gender)
print(' City: %s' % city)
print(' Age: %s' % age)
print()
print_info('Bob', gender='male', age=20)
print_info('Lisa', gender='female', city='Shanghai', age=18)