SlideShare a Scribd company logo
CHAPTER 3
SELF LEARNING PRESENTATION.
I T Club of Vidyadhiraja Vidyapeetom Central School
mavelikara
PAGE NO 91 (85 in old text)
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
In programming Language, A function is a block of
statements to perform an action.
In dictionary,
FUNCTION means DEED
( പ്രവർത്തനം )
FUNCTION = പ്രവൃത്തി
കൃത്യം
ആചരണം
ചുമത്ല
ആഘ ോഷം
തത്ോഴില്‍
ധര്‍മ്മം
രരിരോടി
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
Indian constitution
National Pledge India is my country and all Indians are my brothers
and sisters. I love my country and I am proud of its rich and
varied heritage. I shall always strive to be worthy of it. I shall give
respect to my parents, teachers and all elders and treat everyone
with courtesy. To my country and my people, I pledge my
devotion. In their well-being and prosperity alone lies my
happiness. Fundamental Duties : It shall be the duty of every
citizen of India- (a) to abide by the Constitution and respect its
ideals and institutions, the National Flag and the National
Anthem; Fundamental Rights : are defined as the basic human
rights of all citizens. These rights, defined in Part III of the
Constitution, applied irrespective of race, place of birth, religion,
caste, creed, or gender. They are enforceable by the courts,
subject to specific restrictions. The Directive Principles of State
Policy are guidelines for the framing of laws by the government. )
 It is difficult to write much more
statements as a single program
or unit.
 And also difficult to find the
content.
Big matter on a tiny page.
Horrible !
What are my Duties ?
National Pledge : India is my country and all Indians are my brothers
and sisters. I love my country and I am proud of its rich and varied
heritage. I shall always strive to be worthy of it. I shall give respect
to my parents, teachers and all elders and treat everyone with
courtesy. To my country and my people, I pledge my devotion. In
their well-being and prosperity alone lies my happiness.
Fundamental Duties : It shall be the duty of every citizen of India- (a) to
abide by the Constitution and respect its ideals and institutions,
the National Flag and the National Anthem; (b) to cherish and
follow the noble ideals which inspired our national strugg …..
Fundamental Rights : are defined as the basic human rights of all
citizens. These rights, defined in Part III of the Constitution, applied
irrespective of race, place of birth, religion, caste, creed, or gender.
They are enforceable by the courts, subject to specific restrictions.
The Directive Principles of State Policy are guidelines for the
framing of laws by the government.
Easy to read.
REMEMBER 3 THINGS.
TITLE OF PARAGRAPH
FULL COLON
HANGING ( INDENT ) LINES
How I wrote a Paragraph ?.
National Pledge : India is my country and all Indians are my
brothers and sisters. I love my country and I am proud
of its rich and varied heritage. I shall always strive to
be worthy of it. I shall give respect to my parents,
teachers and all elders and treat everyone with
courtesy. To my country and my people, I pledge my
devotion. In their happiness and prosperity alone lies
my happiness.
INDENT = മോര്‍മ്ജിനില്‍ നിന്ന അക ലം വിട്ന ടടപ്ന തച ുക
-----
1
2
3
def greatest ( List ) :
max = List[0]
for val in List[1:] :
if val > max : max = val
return max
eval(input("Ent
greatest(a)
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
DATA OUTPUT
For x = , the result is 2 * 12 = 2
For x = , the result is 2 * 22 = 8
For x = , the result is 2 * 32 = 18
𝑓 𝑥 = 2x2

Click here to watch 
Click here to watch 
Click here to watch 
Passing the value 1 to x
and Returns 2 * 1 ** 1 = 2

Passing the value 2 to x
and Returns 2 * 2 ** 2 = 8

Passing the value 3 to x
and Returns 2 * 3 ** 3 = 18

𝑭𝒖𝒏𝒄𝒕𝒊𝒐𝒏𝒔 𝒄𝒂𝒏
𝒖𝒔𝒆 𝒎𝒂𝒏𝒚 𝒕𝒊𝒎𝒆𝒔 𝒘𝒊𝒕𝒉
𝒅𝒊𝒇𝒇𝒆𝒓𝒆𝒏𝒕 𝒗𝒂𝒍𝒖𝒆𝒔
FUNCTION TAKES DATA
PRODUCE OUTPUT
Different type of functions.
1) Built in Functions.
2) Modules.
3) User - defined functions.
Built in Functions.
The predefined functions are called
built-in functions. It can be execute
by a simplefunction call statement.
 min() Return smallest.
 max() Return largest.
 pow() Returns xy.
 len() Returns length.
 int() Returns integer.
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916
32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license()"
for more information.
>>> min(10,-20)
-20
>>> max(-25,80)
80
>>> pow(2,3)
8
>>> len("ANIL")
4
>>> int(3.14)
3
Modular Functions.
A module is a file that contains functions,
classes, variables, and constants. To use
the functions of a module, you need to
import the module using the import
statement. The math module is an
example. It contains the most popular
mathematical functions.
>>> import math
>>> math.pi
3.141592653589793
>>> math.sqrt(25)
5.0
>>> math.floor(3.14)
3
>>> math.ceil(3.14)
4
>>> import random
>>> random.randomint(10,20)
17
After importing the modules,
use the function.
math.floor() - Drop down to the
nearest integer.
math.ceil() - Grow up to the nearest
integer.
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
statement 1
statement 2
… … … … …
Keyword “def” is used to define
a function. The very first line is
called Function header. It
begins with the keyword “def”
and end with a colon (“:”).
The body consisting of 1 or more
statements is called Function Body
or function suit. Put some space
(Indentation) before statements.
--------
def function Name ( ) :
Statement 1
Statement 2
… … … … …
Function Body
or function suit.
--------
A pair of Brackets “()”
It begins with the keyword “def”
Function Name.
End with a colon (“:”)
Space before statements (Indentation)
2
4( )
def pie ( ) :
return 22 / 7
def root2 ( ) :
return 1.4142
def pledge ( ) :
print ( “India is my country…”)
Click Me 
Click Me 
Pie ( ) will return
result of 22 / 7 =
3.14.
Pledge ( ) will print
few lines of National
Pledge.
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
Function call is a request to perform the action
defined by the function.
I want to read “Cat on Mat”
Get page no and go to that page.
Here we request to do the action
defined by the function name.
def 𝑃𝑖𝑒 ( ) :
return 22/7
𝑝 = Pie( )
print (“Value of Pie is “, p)
Goto the function
definition and return
back with the result.
Function call statement
>>> def pie ( ) :
return 22 / 7
>>> def root2 ( ) :
return 1.4142
>>> def pledge ( ) :
print ( "India is my country…")
>>> pledge()
>>> India is my country…
>>> b = root2()
>>> print ( b )
>>> 1.4142
>>> a = pie()
>>> print ( a )
>>> 3.142857142857143
Function Call
FunctionCall
Function Call
CallingDefinitions
Function
def A ( ) :
print( “Up above the world so high,” )
def B ( ):
print (“TWINKLE, twinkle, little star,”)
def C ( ) :
print ( ”Like a diamond in the sky.” )
def D ( ) :
print ( ”How I wonder what you are! “ )
Click Me 
INVOKE THE FUNCTIONS IN THE
FOLLOWING ORDER
B()
D()
A()
C()

>>> def A ( ) :
print("Up above the world so high,")
C()
>>> def B ( ):
print ("TWINKLE, twinkle, little star,")
D()
>>> def C ( ) :
print ( "Like a diamond in the sky." )
>>> def D ( ) :
print ( "How I wonder what you are! " )
A()
>>> B()
OUTPUT
>>>
TWINKLE, twinkle, little star,
How I wonder what you are!
Up above the world so high,
Like a diamond in the sky.
>>>
# Calling the Function
C()
# Calling the Function
D()
# Calling the Function
A()
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
Arguments act as an input to the function, to
carries out the specified task.
1. Accepts Data
2. Carried out desired process.
3. Produce Output
ARGUMENTS OR PARAMETERS
Arguments act as an input to
the function, to carry out
the specified task.
Function Definition Returns output
For x = , the result is 2 * 12 = 2
For x = , the result is 2 * 22 = 8
For x = , the result is 2 * 32 = 18
𝑓 𝑥 = 2x2
𝑭𝒖𝒏𝒄𝒕𝒊𝒐𝒏𝒔 𝒄𝒂𝒏 𝒖𝒔𝒆
𝒎𝒂𝒏𝒚 𝒕𝒊𝒎𝒆𝒔 𝒘𝒊𝒕𝒉
𝒅𝒊𝒇𝒇𝒆𝒓𝒆𝒏𝒕 𝒗𝒂𝒍𝒖𝒆𝒔

Click here to watch 
Click here to watch 
Click here to watch 
Passing the value 1 to x
and Returns 2 * 1 ** 1 = 2

Passing the value 2 to x
and Returns 2 * 2 ** 2 = 8

Passing the value 3 to x
and Returns 2 * 3 ** 3 = 18

X’ ‘ receives a value,
and f() returns 2 * x2
def 𝑓 𝑥 :
return 2 * 𝑥 * 𝑥
𝑥 = int ( input ( “Enter No ” ) )
a = 𝑓 𝑥
print (“Answer is ”, a )
5
𝑥 becomes 5
And return 2 * 𝑥2 = 50
What is the answer if x = 5 ?
def 𝑓(𝑥) :
return 2 * 𝑥 * 𝑥
𝑥 = int ( input ( “Enter No ” ) )
a = 𝑓(𝑥)
print (“Answer is ”, a )
OUTPUT
>>>
Answer is 50
>>>
# Calling the Function F(5)
by passing the value 5.
# Input the Value 5.
def add ( a , b , c , d ) :
print ( “Sum is “, a+b+c+d )
add ( 10 , 20, 30, 40 )
a=10 b=20 c=30 d=40
Arguments act as an input to the function, to
carry out the specified task.
Difference between Arguments and
Parameters
Arguments are the actual
values passed to the function
when it is called
Parameter are variables,
which receives the values
of passing arguments.
ARGUMENTS OR PARAMETERS
def area ( r ) :
return 3.14 * r ** r
a = area ( 10 )
print ( “Area of the circle is”, a)
Arguments are the actual
values passed to the
function when it is called
Parameter are variables, receiving passing
values within the function definition.
return a+b+c
X = 20
add (10, X, (3*10) )
Literal
Variable
Expression
Literal 10, value of
variable x and the end
result of expression
(3*10) i.e. 30 will be
passed as arguments.
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
functions which will not
return values are called void functions.
def add ( a, b , c , d ) :
print ( “Sum is ”, a + b + c + d )
s = add(10, 20, 30, 40)
Missing Return statement here
Example of
Void Functions.
No Return
def add ( a, b , c , d ) :
print ( “Sum is ”, a + b + c + d )
s = add(10, 20, 30, 40)
avg = s/4
Print the sum
100. No return
or return None.
Causing the error TypeError:
unsupported operand type(s) for /:
'NoneType' and 'int'
DON’ T Do


Give me some
numbers. I'll return
their sum.
The return statement
stops the execution of
a function and returns
to the caller function.
Return statement is used to stop the execution of
a function and come back to called statement.
Return statement returns the result if we needed.
By default return statement returns a None value.
In Python multiple values can be returned.
RETURN ONE VALUE
def add ( a , b , c , d ) :
return a+b+c+d
s = add(10,20,30,40)
avg = s/4
print (“Sum =”, s, “Avg = ”, avg)
Return statement, stop
function execution and
come back. It also returns
the result if we needed.
By default it will return a
None value.
Return 100
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
def add ( a , b ) :
return a + b
s = add(10,20 )
s = add ( 1.5, 2.5 )
s = add(“anil”, “kumar” )
s = add( [10,20], [5,15] )
ALL IN ONE
Python is a dynamically-
typed language. It doesn't
know about the type of the
variable until the code runs.
ALL IN ONE
10 + 20 = 30
1.5 + 2.5 = 4.0
anil + kumar = anilkumar
[10,20,5,15]
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
RETURN MULTIPLE VALUES
def Sum_Avg ( a , b , c , d ) :
t=a+b+c+d
return t, t/4
sum, avg = Sum_Avg (10, 20, 30, 40)
print (“Sum = “, sum , ” Avg = “, avg)
Passing 10,20,30,40
t=100
100 25
10 3020
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on
win32 Type "help", "copyright", "credits" or "license()" for more information.
>>> x , y , z = 10 , 20 , 30
>>> name, roll, Class = “Anil”, 15, “XII”
>>> a = b = c = 0
>>> print (x, y, z )
>>> 10 20 30
>>> print (name, roll, Class)
>>> Anil 15, XII
>>> print(a,b,c)
>>> 0 0 0
There are 3 (4) types of arguments.
and Varying arguments.
1
2
3
These are mandatory arguments and take orderly.
Provides default value to an argument. It will be
used when we omit value during function call.
Keyword arguments are same as default
arguments but it allows you to give
name-value pairs instead of just the value.
1
2
3
POSITIONAL ARGUMENTS
def add ( a , b , c , d ) :
return a + b + c + d
S = add ( 10 , 20 , 30 , 40 )
Positional arguments are mandatory and take values
orderly. That is why we called it Required arguments.
Both the Arguments and Parameters should be matched.
Values 10, 20, 30, 40 will assigned
in a,b,c and d respectively.
POSITIONAL ARGUMENTS
def add ( a , b , c , d ) :
return a + b + c + d
add ( 10 , 20 , 30 , 40 )
add (10 , 20 )
Positional arguments are
mandatory and take values
orderly. That is why we called
it as Required arguments.
Both the Arguments and
Parameters should be
matched.
TypeError: add() missing 2 required
Positional arguments: 'c' and 'd‘
DON’ T Do
Missingc & d

POSITIONAL ARGUMENTS
def add ( a , b , c , d ) :
return a + b + c + d
add ( 10 , 20 , 30 , 40 )
add (10 , 20, 30, 40, 50 )
Positional arguments are
mandatory and take values orderly.
That is why we called it as Required
arguments. Both the Arguments and
Parameters should be matched.
DON’ T Do
TypeError: add() takes 4
positional arguments but 5
were given…..

Example Program.
Interest= Principal Amount * Rate * Years.
prin = 1000, Rate = 0.1
Years = 3
Parameter Values
prin = 1000, Rate = 0.1
Years = 3
def interest (prin, rate, years) :
return prin*rate*years
p = int(input("Enter Principal amoumt "))
r = float(input("Enter rate of interest "))
y = int(input("Enter No of years "))
i = interest(p,r,y)
print("Interest = ", i)
Output
Enter Principal amoumt 1000
Enter rate of interest 0.1
Enter No of years 3
Interest = 300.0
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
What a dilemma !
How I satisfies
both of them
I want sum of
3 Nos(10,20
and 30)
I want sum of 2
Nos(10 and 20)
default (Optional ) arguments.
What a dilemma !
How I satisfies
both of them
My statement is correct.
‘a’ becomes 10 and ‘b’ becomes 20.
I allows 2
entry only.
When we omit some
arguments, it uses the
default values.
𝐜 = 𝟎
def add (a, b, c=0) :
return a + b + c
v1 = int(input("Enter 1st No "))
v2 = int(input("Enter 2nd No "))
v3 = int(input("Enter 3rd No "))
s1 = add(v1, v2)
s2 = add(v1, v2, v3)
print("Sum of 2 Nos = ", s1)
print("Sum of 3 Nos = ", s2)
Output
Enter 1st No 10
Enter 2nd No 20
Enter 3rd No 30
Sum of 2 Nos = 30
Sum of 3 Nos = 60
Since we omit 3rd argument, c becomes 0
def add(a=0, b=0, c=0, d=0, e=0) :
return a+b+c+d+e
No arguments given.
a,b,c,d,e becomes 0
Answer is 0
One value given, that
will be assigned in ‘a’.
Remaining b,c,d,e
becomes 0
Answer is 1
Two values given, which
will be assigned in ‘a’
and b. Remaining
c,d,e becomes 0
Answer is 3
Three values given,
which are assigned in
a,b and c. Remaining
d,e becomes 0.
Answer is 6
Four values given,
which are assigned in
a,b and c. Remaining
d,e becomes 0.
Answer is 10
All arguments are
given. Which are
assigned in a,b,c,d,e
respectively.
Answer is 15
What do you think ?.
Is it correct in all
situations.
If the function is called without argument,
that argument gets its default value.
When you call 3rd
parameter ‘c’ gets 0.
When you call (no missing
arguments) Parameters a,b and c catch our
values 5,6,7. (a=5, b=6, c=7)
def greet (𝐧𝐚𝐦𝐞, 𝐚𝐠𝐞 ) :
print(“Hai ”, name, “, you are”, age, “years old”)
greet (‘Aswin’, 18)
What happened when I call
Click Me 
Parameter Values.
name = 16, roll = ‘Aswin’
Output : Hai 16, you are ‘Aswin’ years old.
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
Aswin,
Roll no 16,
stand up.
I am Aswin,
Roll no 16.
Giving arguments in the form of key-value
pairs are called keyword arguments.
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
def greet (name, roll ):
print (“Hai“,name,“Your Roll No is”, roll)
greet (roll = 16, name = “Aswin”)
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
x = 10 y = 20 The repeating variable
(y=100) will hides the
previous (y=20).
Variable Values
x = 10
y = 20
p = 15
q = 25
Variable Values
a = 5
b = 8
x = 10
y = 100
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
Built-In : It is top level scope.
Objects in this scope are visible
from everywhere.
Global : entire program can
access it, unless duplicated.
Enclosing scope : It is
the scope of nested
functions.
Local Scope : It is
the scope exists
inside a function.
Internal elements
should not be accessed
from the outside.
B G E
Nothing goes out
like a black Hole.
Built-InScope:
GlobalScope:
Enclosing
Local scope
A = 10
B = 20
C = 30
D = 40
Click Me
Only the Objects
defined in the Built
- in scope are
available.
A = 10
Click Me
Click Me 
Click Me
Only the Objects
defined in Built-in
and global scope
are available.
A = 10 and B = 20
All objects defined in
the 'BGE' scope can be
accessed from
Enclosing Scope.
A=10, B=20 and C=30
All objects defined in the
‘LEGB' scope can be
accessed from Local
Scope. A=10, B=20, C=30
and D = 40
Variables, declared
outside of functions
are called
Global and local scope
Variables that
declare within a
function are called
Local Variables Global Variables
It is declared inside a function. It is declared outside the function.
If it is not initialized, a garbage value is
stored
If it is not initialized zero is stored as
default.
It is created when the function starts
execution and lost when the functions
terminate.
It is created before the program's global
execution starts and lost when the
program terminates.
Data sharing is not possible as data of
the local variable can be accessed by
only one function.
Data sharing is possible as multiple
functions can access the same global
variable.
When the value of the local variable is
modified in one function, the changes
are not visible in another function.
When the value of the global variable is
modified in one function changes are
visible in the rest of the program.
Local variables can be accessed inside a
function in which they are declared.
You can access global variables by any
statement in the program.
Global vs local Variables.
variables a, b are local
and limited its access
within this function.
variables x, y are global and
its access is everywhare.
What is Wrong ?
Memory
Creating global
variable ‘a’ at
Memory Id
1418093744
1Run Me
2Run Me
4Run Me
1
3Run Me
Creating a localized
variable ‘a’ in fun1() and
hide global variable a = 1.
Defferent ID
10
Start from below
Output: In main a = 1
Output: In main a = 1
Output: In fun2 a = 1
Output: In fun1 a = 10
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
OUTPUT
In main a = 1
In fun1 a = 10
In fun2 a = 1
In main a = 1
The 'global' keyword is used to
create a global variable in a
local context and allows
changes to the variable.
def ABC() :
print(“In ABC a = “, a)
a = 10
print (“In Main a = “, a)
ABC()
a = 10
def ABC():
a = a + 5
print(“In ABC a = “, a)
print (“In Main a = “, a)
ABC()
Global variable a=10
Global
variable a=10
 
def ABC() :
a = 10
a = a + 5
print("In ABC a = ", a)
a = 10
print ("In Main a = ", a)
ABC()
print ("In Main a = ", a)
def ABC() :
global a
a = a + 5
print("In ABC a = ", a)
a = 10
print ("In Main a = ", a)
ABC()
print ("In Main a = ", a)OUTPUT:
In Main a = 10
In ABC a = 15
In Main a = 10
OUTPUT:
In Main a = 10
In ABC a = 15
In Main a = 15
The above local variable
a = 15 is not accessed here
Creating a local variable ‘a=10’
in ABC() and hide global
variable a = 10.
Docstring
def area ( r ) :
return 3.14 * r * r
𝑟 = int ( input (“Enter radius ”
))
a = 𝐚𝐫𝐞𝐚 𝑟
print (“Area of circle is ”, a )
docstring
def area ( r ) :
return a+b
>>> help(area)
Help on function area in module __main__:
area(r)
Calculate area when giving radius.
>>> def area ( r ) :
''' Calculate area when giving radius.'''
return a+b
>>> help (area)
Help on function area in module __main__:
area(r)
Calculate area when giving radius.
>>>
Val = 73Val = 45
EXERCISES
1. Write function to convert Fahrenheit temperature into Celsius using formula C = (F-32)/1.8.
2. Covert length in feet and inches to centimeters. 1 inch = 2.5 cm & 1 feet = 12 inches.
3. Calculate the sum of elements in a list.
4. Find out the greatest value in a list.
5. Calculate factorial of given no using for loop.
def Celsius ( f ):
c = (f-32)/1.8
return c
a = input("Enter Fahrenheit ")
a = int(a)
c = Celsius(a)
print ("Celsius Temperature = ", c)
Inputing digits as string
Covert into integer
int () convert a Number or
string into an integer.
Syntax is
int ( “123” ) = 123
int (“1010”, 2) = 10
Int ( “11”,16 ) = 17
int ( “12”, 8 ) = 10
Int ( 3.14 ) = 3
“ANIL “ + “KUMAR” = “ANIL KUMAR”
String : “10” + “20” = “1020”
Integer Operation : 10 + 20 = 30
Val = 73Val = 45
def Cal_cm ( F , I ):
f = int ( input("Enter Feet ") )
i = int ( input("Enter Inch "))
print("Total CM = ", )
OUTPUT : Enter Feet 5
Enter Inch 4
Total CM = 160
How many centimeters in
5 feet 4 inches.
1 Feet = 12 inches
5 feet = 5 *12 inches
= 60 inches
Total inches = ( 5 * 12 ) + 4
60 + 4 = 64
1 inch = 2.5 cm
64 inches = 64 * 2.5
= 160 Cm
EXERCISES 2. Covert length in feet and inches to
centimeters. 1 inch = 2.5 cm & 1 feet = 12 inches.
F = 5 and I = 4
Val = 73Val = 45
def sum(List) :
sum = 0
for val in List:
sum += val
return sum
a = (input("Enter a List "))
print("The Sum of List is ", sum ( a ) )
OUTPUT : Enter a List of Nos [5,10,15,20,25]
The Sum of List is 75
“for” loop fetch each item in the list.
List = [ 5, 10, 15, 20, 25 ]
Step 1: val = 5 sum = 5
Step 2: val = 10 sum = 15
Step 3: val = 15 sum = 30
Step 4: val = 20 sum = 50
Step 5: val = 25 sum = 75
EXERCISES 3. Calculate the sum of elements in a list.
Index 0 1 2 3 4
eval() evaluates the string as a Python
expression. Input String [5,10,15,20,25],
evaluated as a list
Val = 73Val = 45
max = List[0]
for val in :
if val > max : max = val
return max
print ("The Greatest Value is ", m)
slice is an act to getting some elements from a collection of elements.
Syntax is list [start : stop : steps] Default value of start is 0,stop is last
index of list and step is 1
List[1: : ] = [-38, 56, -92, 73,45 ]
List [0:3] = [65, -38, 56]
List [1:4] = [-38, 56, -92]
List[::] = [65, -38, 56, -92, 73, 45]
List [::2] = [65, 56, 73]
List [1: ] = [ -38,56,-92, 73,45 ]
EXERCISES 4. Find out the greatest value in a list.
List = [65, -38, 56, -92, 73,45 ]
List = [65, -38, 56, -92, 73, 45 ]
Index 0 1 2 3 4 5
Assume
Passing[65,-38,56,-92,73,45]
Val = 73Val = 45
def greatest ( List ) :
max = List[0]
for val in List[1:] :
if val > max : max = val
return max
a = eval(input("Enter a list of Nos "))
m = greatest(a)
print ("The Greatest Value is ", m)
slice is an act to getting some elements from a collection of elements.
Syntax is list [start : stop : steps] Default value of start is 0,stop is last
index of list and step is 1.
List[1: ] = [-38, 56,-92, 73, 45]
EXERCISES 4. Find out the greatest value in a list.
Step 1: val = -38 max = 65
Step 2: val = 56 max = 65
Step 3: val = -92 sum = 60
Step 4: val = 73 sum = 73
Step 5: val = 45 sum = 73
List = [65, -38, 56, -92, 73,45 ]
Index 0 1 2 3 4 5
Passing[65,-38,56,-92,73,45]
“for” loop fetch each item in the List [1:]=[-38,56,-92,73, 45
Val = 73Val = 45
def greatest ( List ) :
max = List[0]
for val in List[1:] :
if val > max : max = val
return max
a = eval(input("Enter a list of Nos "))
m = greatest(a)
print ("The Greatest Value is ", m)
slice is an act to getting some elements from a collection of elements.
Syntax is list [start : stop : steps] Default value of start is 0,stop is last
index of list and step is 1.
List[1: : ] = [-38, 56, -92, 73,45 ]
EXERCISES 4. Find out the greatest value in a list.
Step 1: val = -38 max = 65
Step 2: val = 56 max = 65
Step 3: val = -92 sum = 60
Step 4: val = 73 sum = 73
Step 5: val = 45 sum = 73
List = [65, -38, 56, -92, 73,45 ]
Index 0 1 2 3 4 5
OUTPUT : Enter a List of Nos [65, -38, 56, -92, 73,45]
The Greatest Value is 73
[65,-38,56,-92,73,45]
Val = 73Val = 45
def fact(n) :
f = 1
for x in range(1,n+1):
f *= x
return f
a = int(input("Enter a No "))
f = fact(a)
print("The Factorial of ", a, " is ", f)
EXERCISES 5. Calculate factorial of given no using for loop.
range() function create a sequence
of values from start to stop-1.
Range(start, stop, step)
Start and Step are optional
range (1, 5 ) = 1, 2, 3, 4
range (5, 10 ) = 5, 6, 7, 8, 9
range ( 1, 10, 2 ) = 1,3,5,7,9
range ( 0, 20, 5 ) = 0, 5, 10, 15
range (5, 0, -1) = 5, 4, 3, 2, 1
range ( 5 ) = 0, 1, 2, 3, 4
Factorial of n (n!) is the product of all numbers <= n.
5! = 1 x 2 x 3 x 4 x 5 = 120.
Assume that
input a = 5
n = 5
Passing5
Val = 73Val = 45
def fact(n) :
f = 1
for x in range(2,n+1):
f *= x
return f
a = int(input("Enter a No "))
f = fact(a)
print("The Factorial of ", a, " is ", f)
EXERCISES 5. Calculate factorial of given no using for loop.
Factorial of n (n!) is the product of all numbers <= n.
5! = 1 x 2 x 3 x 4 x 5 = 120.
Assuming n = 5
range (1, 6 ) = 1, 2, 3, 4, 5
At end of Step 1 : x = 2 f = 2
Step 2 : x = 3 f = 6
Step 3 : x = 4 f = 24
Step 4 : x = 5 f = 120
OUTPUT
Enter a No 5
The Factorial of 5 is 120
Muitiply value of
x with existing
value of f
Assume that
input a = 5n = 5
Passing5
This presentation could not satisfy me. So I urge
everyone to inform me of its shortcomings.
NAMASTHE

More Related Content

What's hot (20)

PPTX
Introduction to Python and TensorFlow
Bayu Aldi Yansyah
 
PDF
Matlab and Python: Basic Operations
Wai Nwe Tun
 
PDF
Introduction to python
Marian Marinov
 
PPTX
Introduction to the basics of Python programming (part 3)
Pedro Rodrigues
 
PPTX
13. Java text processing
Intro C# Book
 
ODP
Python quickstart for programmers: Python Kung Fu
climatewarrior
 
PDF
Learn 90% of Python in 90 Minutes
Matt Harrison
 
PPTX
Python programing
hamzagame
 
PPTX
Python Datatypes by SujithKumar
Sujith Kumar
 
PDF
Begin with Python
Narong Intiruk
 
PDF
Python Cheat Sheet
Muthu Vinayagam
 
PPT
Introduction to Python Language and Data Types
Ravi Shankar
 
PPTX
Learn python in 20 minutes
Sidharth Nadhan
 
PPT
Python
Vishal Sancheti
 
PDF
Python Puzzlers
Tendayi Mawushe
 
PDF
Python Functions (PyAtl Beginners Night)
Rick Copeland
 
PPTX
Python 101++: Let's Get Down to Business!
Paige Bailey
 
ODP
Introduction to Python - Training for Kids
Aimee Maree
 
PPTX
Introduction to the basics of Python programming (part 1)
Pedro Rodrigues
 
PPTX
15. Streams Files and Directories
Intro C# Book
 
Introduction to Python and TensorFlow
Bayu Aldi Yansyah
 
Matlab and Python: Basic Operations
Wai Nwe Tun
 
Introduction to python
Marian Marinov
 
Introduction to the basics of Python programming (part 3)
Pedro Rodrigues
 
13. Java text processing
Intro C# Book
 
Python quickstart for programmers: Python Kung Fu
climatewarrior
 
Learn 90% of Python in 90 Minutes
Matt Harrison
 
Python programing
hamzagame
 
Python Datatypes by SujithKumar
Sujith Kumar
 
Begin with Python
Narong Intiruk
 
Python Cheat Sheet
Muthu Vinayagam
 
Introduction to Python Language and Data Types
Ravi Shankar
 
Learn python in 20 minutes
Sidharth Nadhan
 
Python Puzzlers
Tendayi Mawushe
 
Python Functions (PyAtl Beginners Night)
Rick Copeland
 
Python 101++: Let's Get Down to Business!
Paige Bailey
 
Introduction to Python - Training for Kids
Aimee Maree
 
Introduction to the basics of Python programming (part 1)
Pedro Rodrigues
 
15. Streams Files and Directories
Intro C# Book
 

Similar to FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE (20)

PPTX
Functions in python3
Lakshmi Sarvani Videla
 
PDF
Notes5
hccit
 
PPTX
Functions and Modules.pptx
Ashwini Raut
 
PPTX
Python_Functions_Modules_ User define Functions-
VidhyaB10
 
PPT
functions _
SwatiHans10
 
PPTX
Working with functions.pptx. Hb.
sabarivelan111007
 
PDF
Lecture 8.pdf
HiHi398889
 
PPTX
UNIT-02-pythonfunctions python function using detehdjsjehhdjejdhdjdjdjddjdhdhhd
tony8553004135
 
PPTX
JNTUK python programming python unit 3.pptx
Venkateswara Babu Ravipati
 
PPTX
Functions in Python Programming Language
BeulahS2
 
PPTX
use of Functions to write python program.pptx
rahulsinghsikarwar2
 
DOCX
Functions.docx
VandanaGoyal21
 
PDF
Functions2.pdf
prasnt1
 
PPTX
Functions in python slide share
Devashish Kumar
 
PPTX
CHAPTER 01 FUNCTION in python class 12th.pptx
PreeTVithule1
 
PDF
functions notes.pdf python functions and opp
KirtiGarg71
 
PPTX
functioninpython-1.pptx
SulekhJangra
 
PDF
Functions2.pdf
Daddy84
 
PDF
Unit_2.0_Functions (1).pdfUnit_2.0_Functions (1).pdf
RutviBaraiya
 
Functions in python3
Lakshmi Sarvani Videla
 
Notes5
hccit
 
Functions and Modules.pptx
Ashwini Raut
 
Python_Functions_Modules_ User define Functions-
VidhyaB10
 
functions _
SwatiHans10
 
Working with functions.pptx. Hb.
sabarivelan111007
 
Lecture 8.pdf
HiHi398889
 
UNIT-02-pythonfunctions python function using detehdjsjehhdjejdhdjdjdjddjdhdhhd
tony8553004135
 
JNTUK python programming python unit 3.pptx
Venkateswara Babu Ravipati
 
Functions in Python Programming Language
BeulahS2
 
use of Functions to write python program.pptx
rahulsinghsikarwar2
 
Functions.docx
VandanaGoyal21
 
Functions2.pdf
prasnt1
 
Functions in python slide share
Devashish Kumar
 
CHAPTER 01 FUNCTION in python class 12th.pptx
PreeTVithule1
 
functions notes.pdf python functions and opp
KirtiGarg71
 
functioninpython-1.pptx
SulekhJangra
 
Functions2.pdf
Daddy84
 
Unit_2.0_Functions (1).pdfUnit_2.0_Functions (1).pdf
RutviBaraiya
 
Ad

More from Venugopalavarma Raja (10)

PPT
LINKED LIST IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
Venugopalavarma Raja
 
PPTX
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
Venugopalavarma Raja
 
PPT
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
Venugopalavarma Raja
 
PPT
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
PPT
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
PPT
ARRAYS IN C++ CBSE AND STATE +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
PPT
POINTERS IN C++ CBSE +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
PDF
Be happy Who am I
Venugopalavarma Raja
 
PPTX
Chess for beginers
Venugopalavarma Raja
 
LINKED LIST IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
Venugopalavarma Raja
 
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
Venugopalavarma Raja
 
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
Venugopalavarma Raja
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
ARRAYS IN C++ CBSE AND STATE +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
POINTERS IN C++ CBSE +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
Be happy Who am I
Venugopalavarma Raja
 
Chess for beginers
Venugopalavarma Raja
 
Ad

Recently uploaded (20)

PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
community health nursing question paper 2.pdf
Prince kumar
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 

FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE

  • 1. CHAPTER 3 SELF LEARNING PRESENTATION. I T Club of Vidyadhiraja Vidyapeetom Central School mavelikara PAGE NO 91 (85 in old text)
  • 3. In programming Language, A function is a block of statements to perform an action. In dictionary, FUNCTION means DEED ( പ്രവർത്തനം ) FUNCTION = പ്രവൃത്തി കൃത്യം ആചരണം ചുമത്ല ആഘ ോഷം തത്ോഴില്‍ ധര്‍മ്മം രരിരോടി
  • 5. Indian constitution National Pledge India is my country and all Indians are my brothers and sisters. I love my country and I am proud of its rich and varied heritage. I shall always strive to be worthy of it. I shall give respect to my parents, teachers and all elders and treat everyone with courtesy. To my country and my people, I pledge my devotion. In their well-being and prosperity alone lies my happiness. Fundamental Duties : It shall be the duty of every citizen of India- (a) to abide by the Constitution and respect its ideals and institutions, the National Flag and the National Anthem; Fundamental Rights : are defined as the basic human rights of all citizens. These rights, defined in Part III of the Constitution, applied irrespective of race, place of birth, religion, caste, creed, or gender. They are enforceable by the courts, subject to specific restrictions. The Directive Principles of State Policy are guidelines for the framing of laws by the government. )  It is difficult to write much more statements as a single program or unit.  And also difficult to find the content. Big matter on a tiny page. Horrible ! What are my Duties ?
  • 6. National Pledge : India is my country and all Indians are my brothers and sisters. I love my country and I am proud of its rich and varied heritage. I shall always strive to be worthy of it. I shall give respect to my parents, teachers and all elders and treat everyone with courtesy. To my country and my people, I pledge my devotion. In their well-being and prosperity alone lies my happiness. Fundamental Duties : It shall be the duty of every citizen of India- (a) to abide by the Constitution and respect its ideals and institutions, the National Flag and the National Anthem; (b) to cherish and follow the noble ideals which inspired our national strugg ….. Fundamental Rights : are defined as the basic human rights of all citizens. These rights, defined in Part III of the Constitution, applied irrespective of race, place of birth, religion, caste, creed, or gender. They are enforceable by the courts, subject to specific restrictions. The Directive Principles of State Policy are guidelines for the framing of laws by the government. Easy to read.
  • 7. REMEMBER 3 THINGS. TITLE OF PARAGRAPH FULL COLON HANGING ( INDENT ) LINES How I wrote a Paragraph ?. National Pledge : India is my country and all Indians are my brothers and sisters. I love my country and I am proud of its rich and varied heritage. I shall always strive to be worthy of it. I shall give respect to my parents, teachers and all elders and treat everyone with courtesy. To my country and my people, I pledge my devotion. In their happiness and prosperity alone lies my happiness. INDENT = മോര്‍മ്ജിനില്‍ നിന്ന അക ലം വിട്ന ടടപ്ന തച ുക ----- 1 2 3
  • 8. def greatest ( List ) : max = List[0] for val in List[1:] : if val > max : max = val return max eval(input("Ent greatest(a)
  • 11. For x = , the result is 2 * 12 = 2 For x = , the result is 2 * 22 = 8 For x = , the result is 2 * 32 = 18 𝑓 𝑥 = 2x2  Click here to watch  Click here to watch  Click here to watch  Passing the value 1 to x and Returns 2 * 1 ** 1 = 2  Passing the value 2 to x and Returns 2 * 2 ** 2 = 8  Passing the value 3 to x and Returns 2 * 3 ** 3 = 18  𝑭𝒖𝒏𝒄𝒕𝒊𝒐𝒏𝒔 𝒄𝒂𝒏 𝒖𝒔𝒆 𝒎𝒂𝒏𝒚 𝒕𝒊𝒎𝒆𝒔 𝒘𝒊𝒕𝒉 𝒅𝒊𝒇𝒇𝒆𝒓𝒆𝒏𝒕 𝒗𝒂𝒍𝒖𝒆𝒔 FUNCTION TAKES DATA PRODUCE OUTPUT
  • 12. Different type of functions. 1) Built in Functions. 2) Modules. 3) User - defined functions.
  • 13. Built in Functions. The predefined functions are called built-in functions. It can be execute by a simplefunction call statement.  min() Return smallest.  max() Return largest.  pow() Returns xy.  len() Returns length.  int() Returns integer.
  • 14. Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> min(10,-20) -20 >>> max(-25,80) 80 >>> pow(2,3) 8 >>> len("ANIL") 4 >>> int(3.14) 3
  • 15. Modular Functions. A module is a file that contains functions, classes, variables, and constants. To use the functions of a module, you need to import the module using the import statement. The math module is an example. It contains the most popular mathematical functions.
  • 16. >>> import math >>> math.pi 3.141592653589793 >>> math.sqrt(25) 5.0 >>> math.floor(3.14) 3 >>> math.ceil(3.14) 4 >>> import random >>> random.randomint(10,20) 17 After importing the modules, use the function. math.floor() - Drop down to the nearest integer. math.ceil() - Grow up to the nearest integer.
  • 18. statement 1 statement 2 … … … … … Keyword “def” is used to define a function. The very first line is called Function header. It begins with the keyword “def” and end with a colon (“:”). The body consisting of 1 or more statements is called Function Body or function suit. Put some space (Indentation) before statements. --------
  • 19. def function Name ( ) : Statement 1 Statement 2 … … … … … Function Body or function suit. -------- A pair of Brackets “()” It begins with the keyword “def” Function Name. End with a colon (“:”) Space before statements (Indentation) 2 4( )
  • 20. def pie ( ) : return 22 / 7 def root2 ( ) : return 1.4142 def pledge ( ) : print ( “India is my country…”) Click Me  Click Me  Pie ( ) will return result of 22 / 7 = 3.14. Pledge ( ) will print few lines of National Pledge.
  • 23. Function call is a request to perform the action defined by the function. I want to read “Cat on Mat” Get page no and go to that page. Here we request to do the action defined by the function name.
  • 24. def 𝑃𝑖𝑒 ( ) : return 22/7 𝑝 = Pie( ) print (“Value of Pie is “, p) Goto the function definition and return back with the result. Function call statement
  • 25. >>> def pie ( ) : return 22 / 7 >>> def root2 ( ) : return 1.4142 >>> def pledge ( ) : print ( "India is my country…") >>> pledge() >>> India is my country… >>> b = root2() >>> print ( b ) >>> 1.4142 >>> a = pie() >>> print ( a ) >>> 3.142857142857143 Function Call FunctionCall Function Call CallingDefinitions Function
  • 26. def A ( ) : print( “Up above the world so high,” ) def B ( ): print (“TWINKLE, twinkle, little star,”) def C ( ) : print ( ”Like a diamond in the sky.” ) def D ( ) : print ( ”How I wonder what you are! “ ) Click Me  INVOKE THE FUNCTIONS IN THE FOLLOWING ORDER B() D() A() C() 
  • 27. >>> def A ( ) : print("Up above the world so high,") C() >>> def B ( ): print ("TWINKLE, twinkle, little star,") D() >>> def C ( ) : print ( "Like a diamond in the sky." ) >>> def D ( ) : print ( "How I wonder what you are! " ) A() >>> B() OUTPUT >>> TWINKLE, twinkle, little star, How I wonder what you are! Up above the world so high, Like a diamond in the sky. >>> # Calling the Function C() # Calling the Function D() # Calling the Function A()
  • 30. Arguments act as an input to the function, to carries out the specified task. 1. Accepts Data 2. Carried out desired process. 3. Produce Output
  • 31. ARGUMENTS OR PARAMETERS Arguments act as an input to the function, to carry out the specified task. Function Definition Returns output
  • 32. For x = , the result is 2 * 12 = 2 For x = , the result is 2 * 22 = 8 For x = , the result is 2 * 32 = 18 𝑓 𝑥 = 2x2 𝑭𝒖𝒏𝒄𝒕𝒊𝒐𝒏𝒔 𝒄𝒂𝒏 𝒖𝒔𝒆 𝒎𝒂𝒏𝒚 𝒕𝒊𝒎𝒆𝒔 𝒘𝒊𝒕𝒉 𝒅𝒊𝒇𝒇𝒆𝒓𝒆𝒏𝒕 𝒗𝒂𝒍𝒖𝒆𝒔  Click here to watch  Click here to watch  Click here to watch  Passing the value 1 to x and Returns 2 * 1 ** 1 = 2  Passing the value 2 to x and Returns 2 * 2 ** 2 = 8  Passing the value 3 to x and Returns 2 * 3 ** 3 = 18  X’ ‘ receives a value, and f() returns 2 * x2
  • 33. def 𝑓 𝑥 : return 2 * 𝑥 * 𝑥 𝑥 = int ( input ( “Enter No ” ) ) a = 𝑓 𝑥 print (“Answer is ”, a ) 5 𝑥 becomes 5 And return 2 * 𝑥2 = 50 What is the answer if x = 5 ?
  • 34. def 𝑓(𝑥) : return 2 * 𝑥 * 𝑥 𝑥 = int ( input ( “Enter No ” ) ) a = 𝑓(𝑥) print (“Answer is ”, a ) OUTPUT >>> Answer is 50 >>> # Calling the Function F(5) by passing the value 5. # Input the Value 5.
  • 35. def add ( a , b , c , d ) : print ( “Sum is “, a+b+c+d ) add ( 10 , 20, 30, 40 ) a=10 b=20 c=30 d=40 Arguments act as an input to the function, to carry out the specified task. Difference between Arguments and Parameters Arguments are the actual values passed to the function when it is called Parameter are variables, which receives the values of passing arguments.
  • 36. ARGUMENTS OR PARAMETERS def area ( r ) : return 3.14 * r ** r a = area ( 10 ) print ( “Area of the circle is”, a) Arguments are the actual values passed to the function when it is called Parameter are variables, receiving passing values within the function definition.
  • 37. return a+b+c X = 20 add (10, X, (3*10) ) Literal Variable Expression Literal 10, value of variable x and the end result of expression (3*10) i.e. 30 will be passed as arguments.
  • 39. functions which will not return values are called void functions. def add ( a, b , c , d ) : print ( “Sum is ”, a + b + c + d ) s = add(10, 20, 30, 40) Missing Return statement here Example of Void Functions. No Return
  • 40. def add ( a, b , c , d ) : print ( “Sum is ”, a + b + c + d ) s = add(10, 20, 30, 40) avg = s/4 Print the sum 100. No return or return None. Causing the error TypeError: unsupported operand type(s) for /: 'NoneType' and 'int' DON’ T Do  
  • 41. Give me some numbers. I'll return their sum. The return statement stops the execution of a function and returns to the caller function.
  • 42. Return statement is used to stop the execution of a function and come back to called statement. Return statement returns the result if we needed. By default return statement returns a None value. In Python multiple values can be returned.
  • 43. RETURN ONE VALUE def add ( a , b , c , d ) : return a+b+c+d s = add(10,20,30,40) avg = s/4 print (“Sum =”, s, “Avg = ”, avg) Return statement, stop function execution and come back. It also returns the result if we needed. By default it will return a None value. Return 100
  • 45. def add ( a , b ) : return a + b s = add(10,20 ) s = add ( 1.5, 2.5 ) s = add(“anil”, “kumar” ) s = add( [10,20], [5,15] ) ALL IN ONE Python is a dynamically- typed language. It doesn't know about the type of the variable until the code runs. ALL IN ONE 10 + 20 = 30 1.5 + 2.5 = 4.0 anil + kumar = anilkumar [10,20,5,15]
  • 47. RETURN MULTIPLE VALUES def Sum_Avg ( a , b , c , d ) : t=a+b+c+d return t, t/4 sum, avg = Sum_Avg (10, 20, 30, 40) print (“Sum = “, sum , ” Avg = “, avg) Passing 10,20,30,40 t=100 100 25
  • 49. Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> x , y , z = 10 , 20 , 30 >>> name, roll, Class = “Anil”, 15, “XII” >>> a = b = c = 0 >>> print (x, y, z ) >>> 10 20 30 >>> print (name, roll, Class) >>> Anil 15, XII >>> print(a,b,c) >>> 0 0 0
  • 50. There are 3 (4) types of arguments. and Varying arguments. 1 2 3
  • 51. These are mandatory arguments and take orderly. Provides default value to an argument. It will be used when we omit value during function call. Keyword arguments are same as default arguments but it allows you to give name-value pairs instead of just the value. 1 2 3
  • 52. POSITIONAL ARGUMENTS def add ( a , b , c , d ) : return a + b + c + d S = add ( 10 , 20 , 30 , 40 ) Positional arguments are mandatory and take values orderly. That is why we called it Required arguments. Both the Arguments and Parameters should be matched. Values 10, 20, 30, 40 will assigned in a,b,c and d respectively.
  • 53. POSITIONAL ARGUMENTS def add ( a , b , c , d ) : return a + b + c + d add ( 10 , 20 , 30 , 40 ) add (10 , 20 ) Positional arguments are mandatory and take values orderly. That is why we called it as Required arguments. Both the Arguments and Parameters should be matched. TypeError: add() missing 2 required Positional arguments: 'c' and 'd‘ DON’ T Do Missingc & d 
  • 54. POSITIONAL ARGUMENTS def add ( a , b , c , d ) : return a + b + c + d add ( 10 , 20 , 30 , 40 ) add (10 , 20, 30, 40, 50 ) Positional arguments are mandatory and take values orderly. That is why we called it as Required arguments. Both the Arguments and Parameters should be matched. DON’ T Do TypeError: add() takes 4 positional arguments but 5 were given….. 
  • 55. Example Program. Interest= Principal Amount * Rate * Years. prin = 1000, Rate = 0.1 Years = 3 Parameter Values prin = 1000, Rate = 0.1 Years = 3
  • 56. def interest (prin, rate, years) : return prin*rate*years p = int(input("Enter Principal amoumt ")) r = float(input("Enter rate of interest ")) y = int(input("Enter No of years ")) i = interest(p,r,y) print("Interest = ", i) Output Enter Principal amoumt 1000 Enter rate of interest 0.1 Enter No of years 3 Interest = 300.0
  • 58. What a dilemma ! How I satisfies both of them I want sum of 3 Nos(10,20 and 30) I want sum of 2 Nos(10 and 20)
  • 59. default (Optional ) arguments. What a dilemma ! How I satisfies both of them My statement is correct. ‘a’ becomes 10 and ‘b’ becomes 20. I allows 2 entry only.
  • 60. When we omit some arguments, it uses the default values. 𝐜 = 𝟎
  • 61. def add (a, b, c=0) : return a + b + c v1 = int(input("Enter 1st No ")) v2 = int(input("Enter 2nd No ")) v3 = int(input("Enter 3rd No ")) s1 = add(v1, v2) s2 = add(v1, v2, v3) print("Sum of 2 Nos = ", s1) print("Sum of 3 Nos = ", s2) Output Enter 1st No 10 Enter 2nd No 20 Enter 3rd No 30 Sum of 2 Nos = 30 Sum of 3 Nos = 60 Since we omit 3rd argument, c becomes 0
  • 62. def add(a=0, b=0, c=0, d=0, e=0) : return a+b+c+d+e No arguments given. a,b,c,d,e becomes 0 Answer is 0 One value given, that will be assigned in ‘a’. Remaining b,c,d,e becomes 0 Answer is 1 Two values given, which will be assigned in ‘a’ and b. Remaining c,d,e becomes 0 Answer is 3 Three values given, which are assigned in a,b and c. Remaining d,e becomes 0. Answer is 6 Four values given, which are assigned in a,b and c. Remaining d,e becomes 0. Answer is 10 All arguments are given. Which are assigned in a,b,c,d,e respectively. Answer is 15
  • 63. What do you think ?. Is it correct in all situations. If the function is called without argument, that argument gets its default value. When you call 3rd parameter ‘c’ gets 0. When you call (no missing arguments) Parameters a,b and c catch our values 5,6,7. (a=5, b=6, c=7)
  • 64. def greet (𝐧𝐚𝐦𝐞, 𝐚𝐠𝐞 ) : print(“Hai ”, name, “, you are”, age, “years old”) greet (‘Aswin’, 18) What happened when I call Click Me  Parameter Values. name = 16, roll = ‘Aswin’ Output : Hai 16, you are ‘Aswin’ years old.
  • 66. Aswin, Roll no 16, stand up. I am Aswin, Roll no 16. Giving arguments in the form of key-value pairs are called keyword arguments.
  • 68. def greet (name, roll ): print (“Hai“,name,“Your Roll No is”, roll) greet (roll = 16, name = “Aswin”)
  • 71. x = 10 y = 20 The repeating variable (y=100) will hides the previous (y=20). Variable Values x = 10 y = 20 p = 15 q = 25 Variable Values a = 5 b = 8 x = 10 y = 100
  • 73. Built-In : It is top level scope. Objects in this scope are visible from everywhere. Global : entire program can access it, unless duplicated. Enclosing scope : It is the scope of nested functions. Local Scope : It is the scope exists inside a function. Internal elements should not be accessed from the outside. B G E Nothing goes out like a black Hole.
  • 74. Built-InScope: GlobalScope: Enclosing Local scope A = 10 B = 20 C = 30 D = 40 Click Me Only the Objects defined in the Built - in scope are available. A = 10 Click Me Click Me  Click Me Only the Objects defined in Built-in and global scope are available. A = 10 and B = 20 All objects defined in the 'BGE' scope can be accessed from Enclosing Scope. A=10, B=20 and C=30 All objects defined in the ‘LEGB' scope can be accessed from Local Scope. A=10, B=20, C=30 and D = 40
  • 75. Variables, declared outside of functions are called Global and local scope Variables that declare within a function are called
  • 76. Local Variables Global Variables It is declared inside a function. It is declared outside the function. If it is not initialized, a garbage value is stored If it is not initialized zero is stored as default. It is created when the function starts execution and lost when the functions terminate. It is created before the program's global execution starts and lost when the program terminates. Data sharing is not possible as data of the local variable can be accessed by only one function. Data sharing is possible as multiple functions can access the same global variable. When the value of the local variable is modified in one function, the changes are not visible in another function. When the value of the global variable is modified in one function changes are visible in the rest of the program. Local variables can be accessed inside a function in which they are declared. You can access global variables by any statement in the program. Global vs local Variables.
  • 77. variables a, b are local and limited its access within this function. variables x, y are global and its access is everywhare. What is Wrong ?
  • 78. Memory Creating global variable ‘a’ at Memory Id 1418093744 1Run Me 2Run Me 4Run Me 1 3Run Me Creating a localized variable ‘a’ in fun1() and hide global variable a = 1. Defferent ID 10 Start from below Output: In main a = 1 Output: In main a = 1 Output: In fun2 a = 1 Output: In fun1 a = 10
  • 80. OUTPUT In main a = 1 In fun1 a = 10 In fun2 a = 1 In main a = 1
  • 81. The 'global' keyword is used to create a global variable in a local context and allows changes to the variable.
  • 82. def ABC() : print(“In ABC a = “, a) a = 10 print (“In Main a = “, a) ABC() a = 10 def ABC(): a = a + 5 print(“In ABC a = “, a) print (“In Main a = “, a) ABC() Global variable a=10 Global variable a=10  
  • 83. def ABC() : a = 10 a = a + 5 print("In ABC a = ", a) a = 10 print ("In Main a = ", a) ABC() print ("In Main a = ", a) def ABC() : global a a = a + 5 print("In ABC a = ", a) a = 10 print ("In Main a = ", a) ABC() print ("In Main a = ", a)OUTPUT: In Main a = 10 In ABC a = 15 In Main a = 10 OUTPUT: In Main a = 10 In ABC a = 15 In Main a = 15 The above local variable a = 15 is not accessed here Creating a local variable ‘a=10’ in ABC() and hide global variable a = 10.
  • 84. Docstring def area ( r ) : return 3.14 * r * r 𝑟 = int ( input (“Enter radius ” )) a = 𝐚𝐫𝐞𝐚 𝑟 print (“Area of circle is ”, a )
  • 85. docstring def area ( r ) : return a+b >>> help(area) Help on function area in module __main__: area(r) Calculate area when giving radius.
  • 86. >>> def area ( r ) : ''' Calculate area when giving radius.''' return a+b >>> help (area) Help on function area in module __main__: area(r) Calculate area when giving radius. >>>
  • 87. Val = 73Val = 45 EXERCISES 1. Write function to convert Fahrenheit temperature into Celsius using formula C = (F-32)/1.8. 2. Covert length in feet and inches to centimeters. 1 inch = 2.5 cm & 1 feet = 12 inches. 3. Calculate the sum of elements in a list. 4. Find out the greatest value in a list. 5. Calculate factorial of given no using for loop. def Celsius ( f ): c = (f-32)/1.8 return c a = input("Enter Fahrenheit ") a = int(a) c = Celsius(a) print ("Celsius Temperature = ", c) Inputing digits as string Covert into integer int () convert a Number or string into an integer. Syntax is int ( “123” ) = 123 int (“1010”, 2) = 10 Int ( “11”,16 ) = 17 int ( “12”, 8 ) = 10 Int ( 3.14 ) = 3 “ANIL “ + “KUMAR” = “ANIL KUMAR” String : “10” + “20” = “1020” Integer Operation : 10 + 20 = 30
  • 88. Val = 73Val = 45 def Cal_cm ( F , I ): f = int ( input("Enter Feet ") ) i = int ( input("Enter Inch ")) print("Total CM = ", ) OUTPUT : Enter Feet 5 Enter Inch 4 Total CM = 160 How many centimeters in 5 feet 4 inches. 1 Feet = 12 inches 5 feet = 5 *12 inches = 60 inches Total inches = ( 5 * 12 ) + 4 60 + 4 = 64 1 inch = 2.5 cm 64 inches = 64 * 2.5 = 160 Cm EXERCISES 2. Covert length in feet and inches to centimeters. 1 inch = 2.5 cm & 1 feet = 12 inches. F = 5 and I = 4
  • 89. Val = 73Val = 45 def sum(List) : sum = 0 for val in List: sum += val return sum a = (input("Enter a List ")) print("The Sum of List is ", sum ( a ) ) OUTPUT : Enter a List of Nos [5,10,15,20,25] The Sum of List is 75 “for” loop fetch each item in the list. List = [ 5, 10, 15, 20, 25 ] Step 1: val = 5 sum = 5 Step 2: val = 10 sum = 15 Step 3: val = 15 sum = 30 Step 4: val = 20 sum = 50 Step 5: val = 25 sum = 75 EXERCISES 3. Calculate the sum of elements in a list. Index 0 1 2 3 4 eval() evaluates the string as a Python expression. Input String [5,10,15,20,25], evaluated as a list
  • 90. Val = 73Val = 45 max = List[0] for val in : if val > max : max = val return max print ("The Greatest Value is ", m) slice is an act to getting some elements from a collection of elements. Syntax is list [start : stop : steps] Default value of start is 0,stop is last index of list and step is 1 List[1: : ] = [-38, 56, -92, 73,45 ] List [0:3] = [65, -38, 56] List [1:4] = [-38, 56, -92] List[::] = [65, -38, 56, -92, 73, 45] List [::2] = [65, 56, 73] List [1: ] = [ -38,56,-92, 73,45 ] EXERCISES 4. Find out the greatest value in a list. List = [65, -38, 56, -92, 73,45 ] List = [65, -38, 56, -92, 73, 45 ] Index 0 1 2 3 4 5 Assume Passing[65,-38,56,-92,73,45]
  • 91. Val = 73Val = 45 def greatest ( List ) : max = List[0] for val in List[1:] : if val > max : max = val return max a = eval(input("Enter a list of Nos ")) m = greatest(a) print ("The Greatest Value is ", m) slice is an act to getting some elements from a collection of elements. Syntax is list [start : stop : steps] Default value of start is 0,stop is last index of list and step is 1. List[1: ] = [-38, 56,-92, 73, 45] EXERCISES 4. Find out the greatest value in a list. Step 1: val = -38 max = 65 Step 2: val = 56 max = 65 Step 3: val = -92 sum = 60 Step 4: val = 73 sum = 73 Step 5: val = 45 sum = 73 List = [65, -38, 56, -92, 73,45 ] Index 0 1 2 3 4 5 Passing[65,-38,56,-92,73,45] “for” loop fetch each item in the List [1:]=[-38,56,-92,73, 45
  • 92. Val = 73Val = 45 def greatest ( List ) : max = List[0] for val in List[1:] : if val > max : max = val return max a = eval(input("Enter a list of Nos ")) m = greatest(a) print ("The Greatest Value is ", m) slice is an act to getting some elements from a collection of elements. Syntax is list [start : stop : steps] Default value of start is 0,stop is last index of list and step is 1. List[1: : ] = [-38, 56, -92, 73,45 ] EXERCISES 4. Find out the greatest value in a list. Step 1: val = -38 max = 65 Step 2: val = 56 max = 65 Step 3: val = -92 sum = 60 Step 4: val = 73 sum = 73 Step 5: val = 45 sum = 73 List = [65, -38, 56, -92, 73,45 ] Index 0 1 2 3 4 5 OUTPUT : Enter a List of Nos [65, -38, 56, -92, 73,45] The Greatest Value is 73 [65,-38,56,-92,73,45]
  • 93. Val = 73Val = 45 def fact(n) : f = 1 for x in range(1,n+1): f *= x return f a = int(input("Enter a No ")) f = fact(a) print("The Factorial of ", a, " is ", f) EXERCISES 5. Calculate factorial of given no using for loop. range() function create a sequence of values from start to stop-1. Range(start, stop, step) Start and Step are optional range (1, 5 ) = 1, 2, 3, 4 range (5, 10 ) = 5, 6, 7, 8, 9 range ( 1, 10, 2 ) = 1,3,5,7,9 range ( 0, 20, 5 ) = 0, 5, 10, 15 range (5, 0, -1) = 5, 4, 3, 2, 1 range ( 5 ) = 0, 1, 2, 3, 4 Factorial of n (n!) is the product of all numbers <= n. 5! = 1 x 2 x 3 x 4 x 5 = 120. Assume that input a = 5 n = 5 Passing5
  • 94. Val = 73Val = 45 def fact(n) : f = 1 for x in range(2,n+1): f *= x return f a = int(input("Enter a No ")) f = fact(a) print("The Factorial of ", a, " is ", f) EXERCISES 5. Calculate factorial of given no using for loop. Factorial of n (n!) is the product of all numbers <= n. 5! = 1 x 2 x 3 x 4 x 5 = 120. Assuming n = 5 range (1, 6 ) = 1, 2, 3, 4, 5 At end of Step 1 : x = 2 f = 2 Step 2 : x = 3 f = 6 Step 3 : x = 4 f = 24 Step 4 : x = 5 f = 120 OUTPUT Enter a No 5 The Factorial of 5 is 120 Muitiply value of x with existing value of f Assume that input a = 5n = 5 Passing5
  • 95. This presentation could not satisfy me. So I urge everyone to inform me of its shortcomings. NAMASTHE