-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path08_Funtions.py
More file actions
133 lines (102 loc) · 2.67 KB
/
08_Funtions.py
File metadata and controls
133 lines (102 loc) · 2.67 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Functions
def func():
print("Geeks for geeks")
func()
def evenOdd(x):
if x % 2 == 0:
return "Even"
else:
return "Odd"
print(evenOdd(10))
print(evenOdd(11))
# Function with parameters
def func1(name):
print("Hello " + name)
func1("Geeks")
# Function with multiple parameters
def func2(name, age):
print("Hello " + name + ", you are " + str(age) + " years old.")
func2("Geeks", 5)
# Function with default parameter
def func3(name, age=20):
print("Hello " + name + ", you are " + str(age) + " years old.")
func3("Geeks")
func3("Akhil")
def myfunc(x, y=10):
print("x:", x)
print("y:", y)
myfunc(5)
myfunc(5,9)
# keyword Arguments
def Student(fname,lname):
print(fname,lname)
Student(fname="Geeks", lname="Practice")
Student(fname="Practice", lname="Geeks")
# Postional Argumrments
def nameAge(name, age):
print("Hi, I am", name)
print("My age is ", age)
print("Case-1:")
nameAge("suraj",32)
print("\nCase-2:")
nameAge(34,"akhil")
# Arbitary keyword Arguments
# *args -> *argv allows you to pass a variable number of positional arguments to a function and becomes a tuple.
def myFunc(*argv):
for i in argv:
print(i)
print(type(argv))
myFunc("akhil","vishnu","suresh")
# **kwargs -> **argv allows you to pass a variable number of keyword arguments and becomes a dictionary.
def myFunc(**kwargs):
for key,value in kwargs.items():
print("%s == %s" % (key, value))
myFunc(first="Akhil", mid="for", last="Kamale")
# Docstring
def evenOdd(x):
"""Function to check if the number is even or odd"""
if (x % 2 == 0):
print("even")
else:
print("odd")
print(evenOdd.__doc__)
# Python Function within Functions
def f1():
s="i love you!"
def f2():
print("Inside f2")
print(s)
f2()
print("Inside f1")
f1()
# Anonymous Functions
def cube(x): return x*x*x #without lambda
cube1 = lambda x : x*x*x #with lambda
print(cube(7))
print(cube1(3))
# Return Statement
def square_value(num):
return num**3
print(square_value(9))
print(square_value(2))
# Pass by Value -> immutable types (int, str, tuple)
def change_num(num):
num = num + 10
print("Value inside function:", num)
num = 5
change_num(num)
print("Value outside function:", num + num)
# Pass by Reference -> mutable types (list, dict, set)
def add_item(mylist):
mylist.append(4)
print("List inside function:", mylist)
lst = [1, 2, 3]
add_item(lst)
print("Outside function:", lst)
# Recursive Function
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n*factorial(n-1)
print(factorial(7)) # Output: 120