-
Notifications
You must be signed in to change notification settings - Fork 0
/
methods.py
60 lines (41 loc) · 1.16 KB
/
methods.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
"""method.py: writting and using methods"""
# keywords: def, return, pass
# syntax:
#
# def method_name([params]):
# pass
#
a = 100
b = 200
c = 300
#above variables are global and are accessible all from the entire module.
def adder(a,b):
"""adds two numbers, strings or iterables"""
#a, b are local to this method.
# they cannot be accessed outside this method.
#global c
c = 10
return (a+b+c, a, b)
print(adder(10,20))
print("global c:"+str(c))
def multiplier(a,b):
if a>b:
return a
return a*b
def demo_method(a,b,c, call_type="", call_for=""):
print(a+b+c)
demo_method('a','b','c')
demo_method( a='a', c='c', b='b')
#variable arguments
def var_args_method(a,b, *args, **kwargs):
print("a :" + str(a) )
print("b : %s" % (b,))
for arg in args:
print("var arg: %s" % arg)
for key, kwarg in list(kwargs.items()):
print("%s : %s" % (key, kwarg))
var_args_method('a','b')
var_args_method("a", 10, 12,3,4,5, name="John", age=23, country="USA")
print("a: {}, b: {}, c : {}".format(a,b,c))
print("a: {1}, b : {0}, c : {2} ".format(b,a,c))
print("a: {a}, b:{b}, c:{c}".format(a=a, b="hello", c=c))