-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctions.py
114 lines (86 loc) · 2.63 KB
/
Functions.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
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
#block of organized, reusable code, modularity
# syntax : def functionname( parameters ):
# "function_docstring"
# function_suite
# return [expression]W
def printme(str):
"this func prints str arg"
print(str)
return
printme("First call")
print("Functin is called")
printme("2nd call")
#calling one funct into another
def hpybday(name):
print("happy bday ",name)
def main():
hpybday('Abhi')
main() #calling main()
#local and global variables
a =20 #global variable
def add(a):
b=30 #local variable
print("Addition",(a+b))
add(a)
def mul(a):
b=2 #local variable
print("Mul",(a*b))
mul(a)
#Local vs. global variables: same variable name -use globals()
a,b = 10,20 #global variables
def add(a,b):
a,b=30,40 #local variables with same name
print("addition of locals is ",(a+b))
print("Add of glabals is ",(globals()['a']+globals()['b']))
add(a,b)
#inner function - declaring function inside the another function
def outer():
name1="Welcome"
def inner():
nonlocal name1
name1="India"
inner() #inner function calling
print(“outer Function – “, name1)
outer() # outer function calling
#Function with default arg
def empDetails(eid=1,ename="Abhi",esal=1000):
print("Emp Id is ",eid)
print("Emp Name is ",ename)
print("Emp sal is ",esal)
empDetails() #default values
empDetails(222)
empDetails(333, "Ram")
empDetails(111, "Raj", 105000)
#Function with Required Args - arguments are the mandatory-
#args must be passed in correct number and order
def show(a,b):
print(a+b)
show(10,20)
#Keyword args or Named Args - during the function call,
# keywords are mentioned along with their corresponding values.
def empDetails(name,role):
print(name,role)
empDetails(name="abhi",role="Dev")
empDetails(name="rahul",role="tester")
#Variable args- design where any number of arguments can be passed
def varlenargs(*argp):
sum=0
for i in argp:
sum=sum+i
print(sum)
varlenargs(10,20,30)
#'None' - special constant to represent absence of value or null value.
#'None' is not a 0 , false, []-
#Void functions that don’t return anything will return None object automatically
def add():
a,b=10,20
c=a+b
x=add() #output is none/null
#If the functions not returns the value but if we are trying to hold the
#values it return NONE as a default value.
#Function return - no datatype is required
def add(x,y):
return x+y
a,b = 10,20
Sum=add(a,b)
print(Sum)