CS (XII) Ch-2 Functions Notes
CS (XII) Ch-2 Functions Notes
Introduction
As a problem becomes complex, it becomes
difficult and cumbersome for a programmer to
keep track of the data and control statements in
the whole program.
Introduction
• Python provides the feature to divide a large
program into different smaller modules. These
modules are known as functions.
2. Modules
3. User Defined
Built In Functions:
• These are predefined functions that are already
available in python.
(iii) Float(): It converts integers and strings into floating point numbers.
Today class : we will discuss
• Recap
• Different built in functions: ( Input(), Eval(),Min() and
max(),Abs(),Type(),Len(),Round(),Range())
• Modules
• Practical Demo
B) Input function
• It enables us to accept an input string from the user without evaluating
its value. It provides the most common way to gather input from the
keyboard.
• It is used to read input text from the user until it encounters a new line.
• Syntax:
name=input(‘enter a name’)
print(‘welcome’, name + “ pleasure to meet you”)
WAP to calculate the selling price of an item.
C) Eval() function
• It is used to evaluate the value of a string. It takes a string as an
argument, evaluates this string as a number, return the numeric result.
• x= eval (‘45+10’)
• print(x)
D) Min() and Max() function
• The max() function takes two or more arguments and return the largest
one.
• max(9,12,6,15)
• The min() function takes two or more arguments and return the
minimum value.
• min(7,26,0,4)
E) Abs function
• It returns the absolute value of single number. It takes
an integer or float number as a argument and always
returns a positive value.
• abs(-50) returns 50
• abs(-30.6) returns 30.6
F) Type function
• If you wish to determine the type of a variable i. e what type of value
does hold then type() function can be used.
• type(10)
• type(8.2)
• type (“hello”)
G) Len function
• It returns the length of an object
• Str=“Hello world”
• len(str)
• returns 11
• X=[1,2,3,4]
• len(x)
• Returns 4
Round() function
• The round () function is used to get the result up to a specified number
of digits.
• Import random
• Random.randange(30)
• Random(): It generates a random number from 0 to 1 such as 0.56438.
It takes no arguments.
Shuffle(list)
Programs
• To select a random subject from a list of subjects.
●Global Scope
●Local Scope
GLOBAL SCOPE
● With global scope, variable can be used
anywhere in the program
eg:
x=50
def test ( ):
print(“inside test x is “, x)
print(“value of x is “, x)
Output:
inside test x is 50
value of x is 50
LOCAL SCOPE
● With local scope, variable can be used only within the
function / block that it is created .
Eg:
X=50
def test ( ):
y = 20
print(‘value of x is ’, X, ‘ y is ’ , y)
print(‘value of x is ’, X, ‘ y is ‘ , y)
On executing the code we will get
Value of x is 50 y is 20
The next print statement will produce an error, because the variable y is
not accessible outside the def()
MORE ON SCOPE OF
VARIABLES
To access global variable inside the function prefix
keyword global with the variable
Eg:
x=50
def test ( ):
global x =5
y =2
print(‘value of x & y inside the function are ‘ , x , y)
Print(‘value of x outside function is ‘ ‘, )
Eg.
def greet (message, times=1):
print message * times
Output:
Welcome
HelloHello
RECURSION
It is a function calling itself again and again.