Chapter 2 - Working With Functions (Revised)
Chapter 2 - Working With Functions (Revised)
1) Built in Functions.
2) Modules.
1) BUILT IN FUNCTIONS
int()
float()
str()
min()
max() ...etc
TYPES OF FUNCTIONS
2) MODULES
i) Import statement
ii) from statement
IMPORTING MODULES – import STATEMENT
for example:
from random import randint
3) USER DEFINED FUNCIONS
def sum_diff(x,y):
add=x+y
diff=x-y
return add,diff
def main():
x=9
y=3
a,b=sum_diff(x,y)
print("Sum = ",a)
print("diff = ",b)
main()
PARAMETERS AND ARGUMENTS IN FUNCTION
1. Positional arguments
2. Default Arguments
3. Keyword Arguments
4. Variable Length Arguments
1. POSITIONAL ARGUMENTS
greet_msg(“Vinay”) # valid
greet_msg() #valid
2. DEFAULT ARGUMENTS
When a function call is made without
arguments, the function has default values for it
for example:
For example:
def greet_msg(name=“Mohan”,msg=“GoodMorning”):
print(name,msg)
greet_msg()
For example:
def greet_msg(name,msg):
print(name,msg)
#calling function
greet_msg(name=“Mohan”,msg=“Hi”): # valid
O/p -> Mohan Hi
3. KEYWORD ARGUMENTS
For example:
#calling function
greet_msg(msg=“Hi”,name=“Mohan”): # valid
O/p -> Mohan Hi
4. VARIABLE LENGTH ARGUMENTS
For Example:
def sum(*n):
total=0
for i in n:
total+=i
print(“Sum = “, total)
sum()
# Calling function
sum() o/p sum=0
sum(10) o/p sum=10
sum(10,20,30,40) o/p sum=100
PASSING ARRAYS/LISTS TO FUNCTIONS
i) Global (Module)
a) Hello World!
Hello World!
b) ‘Hello World!’
‘Hello World!’
c) Hello
Hello
Class Test
print(maximum(2, 3))
a) 2 b) 3 c) The numbers are equal
d) None of the mentioned
Class Test
1 A
2 C
3 A
4 C
5 A
6 B
7 A
8 C
9 B
#Find the output of the following
def change(P,Q=30):
P=P+Q
Q=P-Q
print(P,'#',Q)
return P
A=150
B=100
A=change(A,B)
print(A,'#',B)
B=change(B)
#Find the output of the following
def fun(s):
k=len(s)
m=''
for i in range (0,k):
if(s[i].isupper()):
m=m+s[i].lower()
elif (s[i].isalpha()):
m=m+s[i].upper()
else:
m=m+'bb'
print(m)
fun('@gmail.com')
Thank You