INT102 2.1 Functions & Recursion
INT102 2.1 Functions & Recursion
Created By
Mr. Prince Verma
School of Computer Science & Engg.
©LPU CSE101 C Python
Programming
Lovely Professional University
@LPU INT102
Type Conversion
• Python defines type conversion functions to directly convert one data type to
another which is useful.
• There are two types of Type Conversion in Python:
1. Implicit Type Conversion:
• the Python interpreter automatically converts one data type to another
without any user involvement.
2. Explicit Type Conversion
• the data type is manually changed by the user as per their requirement
©LPU CSE101
@LPU C Python
INT102 Programming
Type Conversion
• Implicit Type Conversion:
E.g.: x , y = 10 , 10.6
z=x+y
print(“Sum is:”, z)
print("x is of type:", type(x))
print("y is of type:", type(y))
print("z is of type:", type(z))
Output: Sum is: 20.6
x is of type: <class 'int’>
y is of type: <class 'float’>
z is of type: <class 'float'>
©LPU CSE101
@LPU C Python
INT102 Programming
Type Conversion
• Explicit Type Conversion:
E.g.: s = "10010"
a = int(s)
b = float(s)
print ("Converting to integer:",a)
print ("Converting to float :",b)
©LPU CSE101
@LPU C Python
INT102 Programming
Function
• Example:
– When evenodd function is called alongwith any value of x as an argument,
displays an appropriate message.
E.g.: def evenodd(x):
if x%2 == 0:
print(x, "is even ")
else:
print(x, "is odd ")
y = 17
evenodd(y) Output: 17 is odd
evenodd(y+1) 18 is even
©LPU CSE101
@LPU C Python
INT102 Programming
Function with Return Statement
• Example:
– The return statement allows you to terminate the execution of a function before
you reach the end.
E.g.: def evenodd(x):
if x%2:
return(“Odd”)
else:
return(“Even”)
n=int(input(“Enter number:”)
print(n,“is:”,evenodd(n)) Output: Enter number:18
18 is even
©LPU CSE101
@LPU C Python
INT102 Programming
Function with Return Statement
• Example:
Output: m: 200
a: 30
©LPU CSE101
@LPU C Python
INT102 Programming
Positional Arguments in Functions
• Example:
– Functions with Positional Arguments:
©LPU CSE101
@LPU C Python
INT102 Programming
MCQs
• Which keyword is used for function?
a) Fun
b) Define
√ c) Def
d) Function
©LPU CSE101
@LPU C Python
INT102 Programming
Code:
• What will be the output of the following Python code?
def printMax(a, b):
if a > b:
print(a, 'is maximum’)
elif a == b:
print(a, 'is equal to', b)
else: a) 3
print(b, 'is maximum') b) 4
printMax(3, 4)
√ c) 4 is maximum
d) None of the mentioned
©LPU CSE101
@LPU C Python
INT102 Programming
Recursion
• Recursive function:
– is a function that calls or invoke itself
E.g.: def fact(n):
if n==0:
return 1
else:
return n*fact(n-1)
n=int(input("Enter a nunber:"))
print("Factorial is:",fact(n))
Output: Enter a nunber:4
Factorial is: 24
©LPU CSE101
@LPU C Python
INT102 Programming
Math Module
• It is a package which contains useful functions for mathematical operations
• To use the math module we need to import it first by using the following line
of code i.e.
import math
©LPU CSE101
@LPU C Python
INT102 Programming
Math Module
• Common math functions in math module are:
– math.trunc(b)): returns integer value by truncate float value from decimal
places
– math.fabs(c)): returns absolute value of given number
– math.exp(a)): returns exponential value of given power
– math.fsum(d)): returns sum of range,list,etc of elements
– math.factorial(a)): returns factorial of number
– math.fmod(a,b)): returns remainder of a from b
©LPU CSE101
@LPU C Python
INT102 Programming
Math Module
E.g. Output:
import math Square root of 4 is: 2.0
a,b,c,d=4,4.856,-45,[2,4,5] Square root of 4.856 is: 2.203633363334291
print("Square root of",a,"is:",math.sqrt(a)) Square root of 4 is: 2
print("Square root of",b,"is:",math.sqrt(b)) Floor value of 4.856 is: 4
print("Square root of",a,"is:",math.isqrt(a)) Truncate value of 4.856 is: 4
print("Floor value of",b,"is:",math.floor(b)) Absolute value of 4.856 is: 45.0
print("Truncate value of",b,"is:",math.trunc(b)) Exponential value of 4 is: 54.598150033144236
print("Absolute value of",b,"is:",math.fabs(c)) Sum of elements in [2, 4, 5] is: 11.0
print("Exponential value of",a,"is:",math.exp(a)) Factorial of 4 is: 24
print("Sum of elements in",d,"is:",math.fsum(d)) Remainder of 4.856 with 4 is:
print("Factorial of",a,"is:",math.factorial(a)) 0.8559999999999999
print("Remainder of",b,"with",a,"is:",math.fmod(b,a))
©LPU CSE101
@LPU C Python
INT102 Programming
Any
Questions?
©LPU CSE101
@LPU C Python
INT102 Programming