0% found this document useful (0 votes)
8 views21 pages

INT102 2.1 Functions & Recursion

The document covers Python programming concepts including type conversion (implicit and explicit), functions, recursion, and the math module. It provides examples for defining functions, using return statements, and handling local and global variables. Additionally, it introduces common math functions and their usage in Python.

Uploaded by

jchikomboya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views21 pages

INT102 2.1 Functions & Recursion

The document covers Python programming concepts including type conversion (implicit and explicit), functions, recursion, and the math module. It provides examples for defining functions, using return statements, and handling local and global variables. Additionally, it introduces common math functions and their usage in Python.

Uploaded by

jchikomboya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

INT-102: Python

Unit 2: Type Conversion, 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)

Output: Converting to integer: 10010


Converting to float : 10010.0
©LPU CSE101
@LPU C Python
INT102 Programming
Function
• It is a block of reusable code that is used to perform a specific task/action.
• It provide better modularity in applications.
• It provide high degree of code reuse.
• In python, function is defined by keyword “def”.
• Syntax for definition:
def function_name(parameter_list):
statements
…..
return expression

©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:

E.g.: def demo(num1,num2):


return num1*num2,num1+num2
m,a=demo(10,20)
print("m:",m)
print("a:",a)

Output: m: 200
a: 30

©LPU CSE101
@LPU C Python
INT102 Programming
Positional Arguments in Functions
• Example:
– Functions with Positional Arguments:

E.g.: def display(name,age):


print("name:",name,", age:",age)
display("ABC",25)
display(32,"CDE")
display(age=34,name="ABC")
Output: name: ABC , age: 25
name: 32 , age: CDE
name: ABC , age: 34
©LPU CSE101
@LPU C Python
INT102 Programming
Default Arguments in Functions
• Example:
– Functions with Default Arguments:

E.g.: def display(a,b=10,c=20):


print("a=",a,"b=",b,"c=",c)
display(15)
display(50,b=30)
display(c=80,a=25,b=35)
Output: a= 15 b= 10 c= 20
a= 50 b= 30 c= 20
a= 25 b= 35 c= 80
©LPU CSE101
@LPU C Python
INT102 Programming
Local and global variable in Python
• Example:

E.g.: p=20 E.g.: a=100


def demo(): def demo():
q=10 a=10
print("local variable: ",q) print(“local a: ”,a)
print("global variable: ",p) demo()
demo() print(“global a: ”,a)
Output: local variable: 10 Output: local a: 10
global variable: 20 global a: 100
©LPU CSE101
@LPU C Python
INT102 Programming
Local and global variable in Python
• Example:

E.g.: def my_func():


x = 10
print("Value inside function:",x)
x = 20
my_func()
print("Value outside function:",x)
Output: Value inside function: 10
Value outside function: 20
©LPU CSE101
@LPU C Python
INT102 Programming
Function call within Functions
• Example:
E.g.: def diff(a,b):
print("Difference in numbers is:", a-b)
def diffoper(a,b):
if a<b:
diff(b,a)
else:
diff(a,b)
diffoper(3,5)
diffoper(5,3) Output: Difference in numbers is: 2
Difference in numbers is: 2
©LPU CSE101
@LPU C Python
INT102 Programming
Function call within Functions
• Example: using return
E.g.: def diff(a,b): Output: Difference in numbers is: 2
return a-b Difference in numbers is: 2
def diffoper(a,b):
if a<b:
c=diff(b,a)
else:
c=diff(a,b)
return c
print("Difference in numbers is:",diffoper(3,5))
print("Difference in numbers is:",diffoper(5,3))

©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

• Common math functions in math module are:


– math.sqrt(a)): returns square root of number a
– math.isqrt(b)):returns integer square root of number a
– math.floor(b)):returns number rounds down to the nearest integer

©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

You might also like