-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path10. Functions.py
130 lines (102 loc) · 3.32 KB
/
10. 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
'''
Function:- A function is a block of code that performs a specific task whenever it is called.
Two Types :-
Built-in Functions:- The functions that are pre-defined and pre-coded in python.
min(), max(), len(), sum(), type(), range(), dict(), list(), tuple(), set(), print().
User Definded Functions:- The functions we create to perform specific tasks as per our needs.
Calling a function:- We call a function by the function name, followed by parameters (if any) inside the parenthesis.
'''
def Out():
print("No Parameters Required")
def Add(a, b): # Here a and b are Parameters and Arguments are passed to these Parameters
Sum = (a+b)
print(Sum)
def Bigger(a, b):
if(a>b):
print("First Number is Bigger")
else:
print("Second number is Bigger")
def Small(a, b):
if(a>b):
print("Second Number is Smaller")
else:
print("First Number is Smaller")
a= 12
b= 89
Out()
Bigger(a, b)
Add(a, b)
c= 82
d= 7
Small(c, d)
Add(c, d)
'''
Arguments in Python are of four Types:
Default Arguments
Keyword Arguments
Variable Length Arguments
Required Arguments
'''
'''
Default Argument:- We provide a default value while creating a function.
'''
def Add(a= 5, b= 7):
print("Sum is:- ",a+b)
Add()
'''
Keyword Arguemnts:- Arguments with key = value.
By this the interpreter recognizes the arguments by the parameter name.
So the order in which the arguments are passed is not relevant.
'''
def Multiplication(a, b, c):
print("a= ", a, "b= ", b, "c= ", c)
print("Product is ", a*b*c)
Multiplication(c= 12, b= 15, a= 11)
'''
Variable-length arguments:- Used to pass more arguments than defined in the actual function.
'''
# Arbitrary Arguments
def name(*credit):
print("Hi,\n", credit[0], credit[1], credit[2])
name("Roshan ", "Rihan ", "Reetha ")
# Keyword Arbitrary Arguments
def name(**debit):
print("Hi,\n", debit["debitor1"], debit["debitor3"], debit["debitor3"])
name(debitor1 = "Yamla ", debitor2 = "Pagla ", debitor3 = "Deewana ")
'''
Required Arguments:- Conventionally, it is necessary to pass the arguments in the correct positional order
and the number of arguments passed should match with arguments of function .
'''
def Student(Name, Class, Roll_No):
print("Hello,\n", Name, Class, Roll_No)
Student(Name= "Hermione Granger", Class= "First-Year Magic Student ", Roll_No= 3)
# Return Statement
'''
2 Uses - 1. End the function, 2. Send Value Back to the program
'''
def Advice(a):
return "Honesty is the best " + a + "!"
Saying = Advice("Policy")
print(Saying)
# Docstring : String Literals used to define a function, method, class or module
def Multiplication(a,b):
'''This function takes two integers and the product of the two number is given as output'''
print(a*b)
Multiplication(4,6)
print(Multiplication.__doc__)
# PEP 8 is Python Enhancement Proposal a document that provides guidelines and best practices on how to write Python Code.
# The Zen of Python consists of nineteen aphorisms, some of which favor one specific trait over another,
# providing opinions about what makes your code better
# Can be accessed by writing import this
# Recursive Function
# When a Function Repeats itself or calls some other functions.
#Lambda
Square= lambda a: a*a
b= Square(7)
print(b)
Average= lambda a,b,c: (a+b+c)/3
print(Average(2,4,6))
def Sqad(a,b):
print(a(b))
# Anonymous Function
Sqad(lambda a: a*a,4)