PRE-DEFINED FUNCTION
s="Python a=10 print("Hello")
Programming" b=10 Hello
len(s) c=20
18 print(id(a))
print(id(b))
type("Hello") 140704304518464
print(id(c)) 140704304518464
<class 'str'> 140704304518784
FUNCTION DEFINED IN MATHS MODULE
import math
sqrt(144) math.ceil(14.58)
NameError: name 15
'sqrt' is not defined
import math import math
math.sqrt(144) math.floor(14.58)
12.0 14
Statements
to be executed
KEYWORDS NAME OF FUNCTION
(must follow the
Rules of identifier
def Display():
print("Never Give Up!!!")
print("....................")
Display() # function calling Never Give Up!!!
Display() ....................
Never Give Up!!!
....................
def Display3():
print("Never Give Up!!!")
print("....................")
Good Night !!!
def Display1(): ===============
print("Good Morning !!!")
print("++++++++++++++++++") Never Give Up!!!
....................
def Display2(): Good Morning !!!
print("Good Night !!!")
print("===============")
++++++++++++++++++
Display2() # function calling
Display3()
Display1()
def add(): # function definition/Header
num1=int(input("Enter First Number: "))
num2=int(input("Enter Second Number: "))
result=num1+num2
print("Addition=",result)
add() Enter First Number: 45
Enter Second Number: 45
Addition= 90
# Function Definition with Formal Parameters (a and b)
OUTPUT
def add_numbers(a, b):
sum_result = a + b
print("Sum:", sum_result)
# Function Call with Actual Parameters (5 and 10)
15
add_numbers(5, 10)
Function with no argument and no return value
def add(): NO PARAMETER
x = int(input("Number 1:")) # Take user input
y = int(input("Number 2:")) # Take user input
print("Addition =", x + y)
add() NO ARGUMENT
Number 1: 12
Number 2: 15
Addition = 27
Function with no argument and no return value
def add():
print("Addition =", x + y)
#global variable x and y
# as it is outside add()
x = int(input("Number 1:"))
y = int(input("Number 2:")) Number 1: 12
add() # Call the 'add' function
Number 2: 15
Addition = 27
Function with argument but no return value
def add(a, b): FORMAL PARAMETERS
print("Addition =", a + b) Note:
First value will be passed to first argument
x = int(input("Number 1:")) Second value will be passed to 2nd argument
y = int(input("Number 2:"))
add(x,y) ACTUAL ARGUMENTS
Number 1: 12
Number 2: 15
Addition = 27
Function with argument but no return value
def sub(y,x): # Function definition with parameters
print("Subtraction =", y - x) Note:
First value will be passed to first argument
Second value will be passed to 2nd argument
x = 20
y = 30
sub(x,y) # Calling function with Actual Argument
Subtraction = -10
Function with argument but no return value
def sub(x,y): # Function definition with parameters
print("Subtraction =", y - x) Note:
First value will be passed to first argument
Second value will be passed to 2nd argument
x = 20
y = 30
sub(x,y) # Calling function with Actual Argument
Subtraction = 10
Function with argument and return value
def sub(x,y): # Formal parameters
return x-y This line of statement will not executed
print("Subtraction =", x-y)
As return control pass to outside the
function , from where it is called so after
return no statement will execute
x = 20
y = 30
result=sub(x,y) # calling function
print(result) -10
Function with no argument but return value
def sub():
x = 20
y = 30
return x-y
result=sub() # calling function
print(result) -10
Function with no argument but return value
def sub():
x = 20
y = 30
-10
return x-y
print(sub()) # calling function and printing
def add():
x = 20 Note:
y = 30 The statements return after
return x+y
return statement will not be
print("How are your") executed.
print("Iam fine")
print("What about your")
OUTPUT 50
print(add())
def sum(a): def sum(a):
return(a*a*a) print(a)
print(sum(10)) print(sum(10))
OUTPUT OUTPUT
1000 10
None
def sum(a): def sum():
return(a) return 1,2,3,4
a=sum(10) a=sum()
print(a) print(a)
OUTPUT OUTPUT
10 (1,2,3,4)
def sum(): def sum():
return (1,2,3,4) return (1*4,2)
a,b,c,d=sum() a=b=c=d=sum()
print(a,b,c,d) print(a,b)
OUTPUT
OUTPUT
(4,2) (4,2)
1,2,3,4
def sum():
return (1,2,3,4)
a=b=c=d=sum()
print(a,b,c,d)
OUTPUT
(1, 2, 3, 4) (1, 2, 3, 4) (1, 2, 3, 4) (1, 2, 3, 4)
Function with no argument no return value
def display(): Note:
print("Class 12...") If function does not return
r=display() any value then by default
print(r)
it will return None
Class 12...
print(print("Hello"))
None
Hello
None
Function with no argument no return value
def display(): Note:
print("Class 12...") If function return blank/nothing
return value then by default
r=display()
it will return None
print(r)
Class 12...
None
Function with no argument no return value
print(print(print("Hello"),"how are your"))
Note:
Hello If function does not return
None how are your any value then by default
it will return None
None
print(print("Hello"))
Hello
None
def greater(a, b): Enter First Number:20
if a > b: Enter Second Number:12
return a # Return 'a' if it is greater than 'b'
Greater number is: 20
else:
return b # Return 'b' if it is greater than or equal to 'a'
print("Statement after return statement") # This line won't be executed
Return statement will transfer the
# Taking input from the user control outside the function.
num1 = int(input("Enter First Number:"))
num2 = int(input("Enter Second Number:")) If a function does not return any value,
by default it return None.
# Calling the function and storing the result in 'result'
result = greater(num1, num2) The return statement ends the function
execution even if it is in the middle of
# Printing the result the function. The statement written
print("Greater number is:", result) after return statement will not get
executed.
def greater(a, b): Enter First Number:12
if a > b: Enter Second Number:25
return a # Return 'a' if it is greater than 'b'
Greater number is: 25
else:
return b # Return 'b' if it is greater than or equal to 'a'
print("Statement after return statement") # This line won't be executed
Return statement will transfer the
# Taking input from the user control outside the function.
num1 = int(input("Enter First Number:"))
num2 = int(input("Enter Second Number:")) If a function does not return any value,
by default it return None.
# Calling the function and storing the result in 'result'
result = greater(num1, num2) The return statement ends the function
execution even if it is in the middle of
# Printing the result the function. The statement written
print("Greater number is:", result) after return statement will not get
executed.
def operation(a, b):
add = a + b
sub = a - b
mul = a * b
div = a / b
return add, sub, mul, div # Return the results as a tuple
# Taking input from the user
num1 = int(input("Enter First Number:"))
num2 = int(input("Enter Second Number:"))
# Calling the function and storing the result in 'result'
result = operation(num1, num2)
Enter First Number:20
# Printing the result and its type Enter Second Number:10
print("Result =", result) Result = (30, 10, 200, 2.0)
print("Type of Result =", type(result)) Type of Result = <class 'tuple'>
def operation(a, b): # Parameters/Formal Argument
add = a + b Enter First Number:20
sub = a - b
mul = a * b Enter Second Number:10
div = a / b Result = (30, 10, 200, 2.0)
return add, sub, mul, div Type of result is = <class 'tuple'>
num1 = int(input("Enter First Number:"))
num2 = int(input("Enter Second Number:"))
=========================
result = operation(num1, num2) # Arguments/Actual Arguments Addition = 30
# Unpacking the returned tuple Subtraction = 10
a, s, m, d = operation(num1, num2)
Multiplication = 200
print("Result =", result)
print("Type of result is =", type(result)) Division = 2.0
print("===================================") Type of a = <class 'int'>
print("Addition =", a) Type of d = <class 'float'>
print("Subtraction =", s)
print("Multiplication =", m)
print("Division =", d)
print("Type of a =", type(a))
print("Type of d =", type(d))
def add(a,b):
print(a+b) ❖A gets 10 and b gets 20.
#Passing argument in order ❖If you miss any argument,
add(10,20) it will throw Type error.
def add(a,b):
print(a+b)
add(10)
TypeError: add() missing 1 required positional
argument: 'b'
def add(a):
print(a+b)
add(10,20)
TypeError: add() takes 1 positional argument
but 2 were given
def DisplayMarks(Name, Marks=10):
Name: Raja
print("Name:",Name)
Marks: 10
print("Marks:",Marks)
DisplayMarks('Raja')
def DisplayMarks(Name, Marks=10):
Name: Raja
print("Name:",Name)
Marks: 90
print("Marks:",Marks)
DisplayMarks('Raja',90)
def DisplayMarks(Name, Marks=10):
Name: Raja
print("Name:",Name)
Marks: 10
print("Marks:",Marks)
DisplayMarks('Raja')
def DisplayMarks(Name, Marks):
Name: 10
print("Name:",Name)
Marks: Komal
print("Marks:",Marks)
DisplayMarks(10,'Komal')
def DisplayMarks(Name, Marks):
Name: Komal
print("Name:",Name)
Marks: 10
print("Marks:",Marks)
DisplayMarks(Marks=10, Name='Komal')
def DisplayMarks(Name, Marks):
print("Name:",Name)
print("Marks:",Marks)
DisplayMarks(marks=10, Name='Komal')
TypeError: DisplayMarks() got an unexpected
keyword argument 'marks'
def DisplayMarks(Name, Marks):
Name: Komal
print("Name:",Name)
Marks: 10
print("Marks:",Marks)
DisplayMarks(Marks=10, Name='Komal')
def DisplayMarks(Name, Marks):
print("Name:",Name)
print("Marks:",Marks)
DisplayMarks(Name='Komal',Marks)
def DisplayMarks(Marks=20,Name):
print("Name:",Name)
print("Marks:",Marks)
DisplayMarks(50,'Rohit')
90 98 90 90
def Example(b=90,a): def Example(a=90,b):
print(a) print(a)
Example(b=10,a=90) Example(10,a=90)
def Example(a=90,b=90):
print(a)
Example(10,a=90)
Example(10,a=90)
TypeError: Example() got multiple values
for argument 'a'