SlideShare a Scribd company logo
5
Keyword def that marks the start of the
function header.
def
Myprog.py
A function name to uniquely identify the
function. Function naming follows the same
rules of writing identifiers in Python.
myfunction()
Parameters (arguments) through which we
pass values to a function. They are optional.
A colon (:) to mark the end of the function
header.
:
One or more valid python statements that
make up the function body. Statements must
have the same indentation level (usually 4
spaces).
statement1
statement2
statement3
myfunction()
When we execute the program Myprog.py
then myfunction() called
It jump inside the function at the top
[header] and read all the statements inside
the function called
Most read
10
Example: To define function add() to accept two number and display sum.
def add():
x=int(input(“Enter value of x:”))
y=int(input(“Enter value of y:”))
sum=x + y
print(“x+y=“,sum)
add()
---Output-----
Enter value of x: 10
Enter value of y: 20
x+y=30
Declaration and definition part
Calling function
Most read
13
Example: To define function mult() to accept two number and display Multiply.
def mult():
x=int(input(“Enter value of x:”))
y=int(input(“Enter value of y:”))
s=x * y
print(“x*y=“,s)
mult()
---Output-----
Enter value of x: 20
Enter value of y: 10
x*y=200
Declaration and definition part
Calling function
Most read
FUNCTION IN PYTHON
CLASS: XII
COMPUTER SCIENCE -083
USER DEFINED FUNCTIONS
PART-3
Syntax to define User Defined Functions:
def Function_name (parameters or arguments):
[statements……]
…………………………….
…………………………….
Its keyword and
used to define the
function in python
Statements inside the def begin after four
spaces, this is called Indentation.
These parameters
are optional
Example To define User Defined Functions:
def myname():
print(“JAMES”)
This is function definition
To call this function in a program
myname() This is function calling
----Output-----
JAMES
Full python code:
def myname():
print(“JAMES”)
myname()
Example to define: User Defined Functions:
Above shown is a function definition that consists of the following components.
Keyword def that marks the start of the function header.
A function name to uniquely identify the function. Function naming follows the same
rules of writing identifiers in Python.
Parameters (arguments) through which we pass values to a function. They are
optional.
A colon (:) to mark the end of the function header.
One or more valid python statements that make up the function body. Statements
must have the same indentation level (usually 4 spaces).
An optional return statement to return a value from the function.
Keyword def that marks the start of the
function header.
def
Myprog.py
A function name to uniquely identify the
function. Function naming follows the same
rules of writing identifiers in Python.
myfunction()
Parameters (arguments) through which we
pass values to a function. They are optional.
A colon (:) to mark the end of the function
header.
:
One or more valid python statements that
make up the function body. Statements must
have the same indentation level (usually 4
spaces).
statement1
statement2
statement3
myfunction()
When we execute the program Myprog.py
then myfunction() called
It jump inside the function at the top
[header] and read all the statements inside
the function called
Types of User Defined Functions:
Default function
Parameterized functions
Function with parameters and Return type
Default function
Function without any parameter or parameters and without return type.
Syntax:
def function_name():
……..statements……
……..statements……
function_name()
Example:To display the message “Hello World” five time without loop.
def mymsg():
print(“Hello World”)
mymsg()
mymsg()
mymsg()
mymsg()
mymsg()
----OUTPUT------
Hello World
Hello World
Hello World
Hello World
Hello World
Example:To display the message “Hello World” five time without loop.
Full python code:
def mymsg():
print(“Hello World”)
mymsg()
mymsg()
mymsg()
mymsg()
mymsg()
But if you do want to use to write so many
time the function name , but want to print the
message “Hello World” 15 or 20 or 40 times
then use loop
def mymsg():
print(“Hello World”)
for x in range(1,31):
mymsg()
Example: To define function add() to accept two number and display sum.
def add():
x=int(input(“Enter value of x:”))
y=int(input(“Enter value of y:”))
sum=x + y
print(“x+y=“,sum)
add()
---Output-----
Enter value of x: 10
Enter value of y: 20
x+y=30
Declaration and definition part
Calling function
Example: To define function add() to accept two number and display sum.
def add():
x=int(input(“Enter value of x:”))
y=int(input(“Enter value of y:”))
sum=x + y
print(“x+y=“,sum)
add()
---Output-----
Enter value of x: 10
Enter value of y: 20
x+y=30
Declaration and definition part
Calling function
Example: To define function sub() to accept two number and display subtraction.
def sub():
x=int(input(“Enter value of x:”))
y=int(input(“Enter value of y:”))
s=x - y
print(“x-y=“,s)
sub()
---Output-----
Enter value of x: 20
Enter value of y: 10
x-y=10
Declaration and definition part
Calling function
Example: To define function mult() to accept two number and display Multiply.
def mult():
x=int(input(“Enter value of x:”))
y=int(input(“Enter value of y:”))
s=x * y
print(“x*y=“,s)
mult()
---Output-----
Enter value of x: 20
Enter value of y: 10
x*y=200
Declaration and definition part
Calling function
Example: To define function div() to accept two number and display Division.
def div():
x=int(input(“Enter value of x:”))
y=int(input(“Enter value of y:”))
s=x / y
print(“x/y=“,s)
div()
---Output-----
Enter value of x: 15
Enter value of y: 2
x/y=7.5
Declaration and definition part
Calling function
Now how we combine all the function in one program and ask user to enter
the choice and according to choice add, subtract, multiply and divide.
----Main Menu--------
1- Addition
2- Subtraction
3- Multiplication
4- Division
5- To exit from Program
Please enter the choice
Program should work like the output given below on the basis of users choice:
def add():
x=int(input(“Enter value x:”))
y=int(input(“Enter value y:”))
print(“x+y=“,x+y)
def sub():
x=int(input(“Enter value x:”))
y=int(input(“Enter value y:”))
print(“x-y=“,x-y)
def mult():
x=int(input(“Enter value x:”))
y=int(input(“Enter value y:”))
print(“x*y=“,x*y)
def div():
x=int(input(“Enter value x:”))
y=int(input(“Enter value y:”))
print(“x//y=“,x//y)
def add():
x=int(input("Enter value x:"))
y=int(input("Enter value y:"))
print("x+y=",x+y)
def sub():
x=int(input("Enter value x:"))
y=int(input("Enter value y:"))
print("x-y=",x-y)
def mul():
x=int(input("Enter value x:"))
y=int(input("Enter value y:"))
print("x*y=",x*y)
def div():
x=int(input("Enter value x:"))
y=int(input("Enter value y:"))
print("x//y=",x//y)
ch=0
while True:
print("1- Addition")
print("2- Subtraction")
print("3- Multiplication")
print("4- Division")
print("5- To exit from Program")
ch=int(input("Please enter the choice"))
if ch==1:
add()
elif ch==2:
sub()
elif ch==3:
mul()
elif ch==4:
div()
else:
break
Full python code for the previous question:
Example:To create a function checkevenodd() , that accept the number
and check its an even or odd
def checkevenodd():
no=int(input(“Enter the value:”))
if no%2 == 0:
print(“Its an even no”)
else:
print(“Its an odd number”)
----Output----
Enter the value: 12
Its an even no
----Output----
Enter the value: 17
Its an Odd no
Example:To display number from 1 to 20 using function display1to20()
Example:To display number table of 3 using function display3()
Example:To display even number between 2 to 40 using function
displayeven()
Example:Program to print triangle using loops and display it through
function displaytri()
1
12
123
1234
12345
Example:To display number from 1 to 20 using function display1to20()
def display1to20():
for x in range(1,21):
print(x,sep=“,”,end=“”)
display1to20()
---Output----
1,2,3,4,5,6……….,20
Example:To display number table of 3 using function display3()
def display3():
for x in range(1,11):
print(x*3)
display3()
---Output----
3
6
9
12
15
.
.
.
30
Example:To display number from 1 to 20 using function display1to20()
Example:To display number table of 3 using function display3()
Example:To display even number between 2 to 40 using function
displayeven()
Example:Program to print triangle using loops and display it through
function displaytri()
1
12
123
1234
12345
Example:To display number from 1 to 20 using function display1to20()
Example:To display number table of 3 using function display3()
Example:To display even number between 2 to 40 using function
displayeven()
Example:Program to print triangle using loops and display it through
function displaytri()
1
12
123
1234
12345

More Related Content

What's hot (20)

Python functions
Python functionsPython functions
Python functions
Prof. Dr. K. Adisesha
 
Python ppt
Python pptPython ppt
Python ppt
Mohita Pandey
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
Devashish Kumar
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
Soba Arjun
 
Python Programming ppt
Python Programming pptPython Programming ppt
Python Programming ppt
ismailmrribi
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
RaginiJain21
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
Mohammed Sikander
 
Python programming : List and tuples
Python programming : List and tuplesPython programming : List and tuples
Python programming : List and tuples
Emertxe Information Technologies Pvt Ltd
 
Built in function
Built in functionBuilt in function
Built in function
MD. Rayhanul Islam Sayket
 
Python strings presentation
Python strings presentationPython strings presentation
Python strings presentation
VedaGayathri1
 
Python strings
Python stringsPython strings
Python strings
Mohammed Sikander
 
Call by value or call by reference in C++
Call by value or call by reference in C++Call by value or call by reference in C++
Call by value or call by reference in C++
Sachin Yadav
 
Python - An Introduction
Python - An IntroductionPython - An Introduction
Python - An Introduction
Swarit Wadhe
 
Python : Data Types
Python : Data TypesPython : Data Types
Python : Data Types
Emertxe Information Technologies Pvt Ltd
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
TMARAGATHAM
 
Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)
Pedro Rodrigues
 
Python final ppt
Python final pptPython final ppt
Python final ppt
Ripal Ranpara
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
Ritika Sharma
 
Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)
Paige Bailey
 
Print input-presentation
Print input-presentationPrint input-presentation
Print input-presentation
Martin McBride
 

Similar to USER DEFINE FUNCTIONS IN PYTHON (20)

Python_Functions_Unit1.pptx
Python_Functions_Unit1.pptxPython_Functions_Unit1.pptx
Python_Functions_Unit1.pptx
Koteswari Kasireddy
 
Chapter Functions for grade 12 computer Science
Chapter Functions for grade 12 computer ScienceChapter Functions for grade 12 computer Science
Chapter Functions for grade 12 computer Science
KrithikaTM
 
Working with functions.pptx. Hb.
Working with functions.pptx.          Hb.Working with functions.pptx.          Hb.
Working with functions.pptx. Hb.
sabarivelan111007
 
Functions_new.pptx
Functions_new.pptxFunctions_new.pptx
Functions_new.pptx
Yagna15
 
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
prasadmutkule1
 
functions.pptx
functions.pptxfunctions.pptx
functions.pptx
KavithaChekuri3
 
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
vikram mahendra
 
Functions.docx
Functions.docxFunctions.docx
Functions.docx
VandanaGoyal21
 
Introduction to Python Prog. - Lecture 2
Introduction to Python Prog. - Lecture 2Introduction to Python Prog. - Lecture 2
Introduction to Python Prog. - Lecture 2
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
pYTHONMYSQLCOMPUTERSCIENCECLSS12WORDDOCUMENT
pYTHONMYSQLCOMPUTERSCIENCECLSS12WORDDOCUMENTpYTHONMYSQLCOMPUTERSCIENCECLSS12WORDDOCUMENT
pYTHONMYSQLCOMPUTERSCIENCECLSS12WORDDOCUMENT
SufianNarwade
 
Python functions PYTHON FUNCTIONS1234567
Python functions PYTHON FUNCTIONS1234567Python functions PYTHON FUNCTIONS1234567
Python functions PYTHON FUNCTIONS1234567
AnjaneyuluKunchala1
 
Python for Data Science function third module ppt.pptx
Python for Data Science  function third module ppt.pptxPython for Data Science  function third module ppt.pptx
Python for Data Science function third module ppt.pptx
bmit1
 
Functions in python3
Functions in python3Functions in python3
Functions in python3
Lakshmi Sarvani Videla
 
Introduction to Python Programming | InsideAIML
Introduction to Python Programming | InsideAIMLIntroduction to Python Programming | InsideAIML
Introduction to Python Programming | InsideAIML
VijaySharma802
 
JNTUK python programming python unit 3.pptx
JNTUK python programming python unit 3.pptxJNTUK python programming python unit 3.pptx
JNTUK python programming python unit 3.pptx
Venkateswara Babu Ravipati
 
ForLoopandUserDefinedFunctions.pptx
ForLoopandUserDefinedFunctions.pptxForLoopandUserDefinedFunctions.pptx
ForLoopandUserDefinedFunctions.pptx
AaliyanShaikh
 
Functions and Modules.pptx
Functions and Modules.pptxFunctions and Modules.pptx
Functions and Modules.pptx
Ashwini Raut
 
Python Lecture 4
Python Lecture 4Python Lecture 4
Python Lecture 4
Inzamam Baig
 
powerpoint 2-7.pptx
powerpoint 2-7.pptxpowerpoint 2-7.pptx
powerpoint 2-7.pptx
JuanPicasso7
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdf
prasnt1
 
Chapter Functions for grade 12 computer Science
Chapter Functions for grade 12 computer ScienceChapter Functions for grade 12 computer Science
Chapter Functions for grade 12 computer Science
KrithikaTM
 
Working with functions.pptx. Hb.
Working with functions.pptx.          Hb.Working with functions.pptx.          Hb.
Working with functions.pptx. Hb.
sabarivelan111007
 
Functions_new.pptx
Functions_new.pptxFunctions_new.pptx
Functions_new.pptx
Yagna15
 
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
prasadmutkule1
 
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
vikram mahendra
 
pYTHONMYSQLCOMPUTERSCIENCECLSS12WORDDOCUMENT
pYTHONMYSQLCOMPUTERSCIENCECLSS12WORDDOCUMENTpYTHONMYSQLCOMPUTERSCIENCECLSS12WORDDOCUMENT
pYTHONMYSQLCOMPUTERSCIENCECLSS12WORDDOCUMENT
SufianNarwade
 
Python functions PYTHON FUNCTIONS1234567
Python functions PYTHON FUNCTIONS1234567Python functions PYTHON FUNCTIONS1234567
Python functions PYTHON FUNCTIONS1234567
AnjaneyuluKunchala1
 
Python for Data Science function third module ppt.pptx
Python for Data Science  function third module ppt.pptxPython for Data Science  function third module ppt.pptx
Python for Data Science function third module ppt.pptx
bmit1
 
Introduction to Python Programming | InsideAIML
Introduction to Python Programming | InsideAIMLIntroduction to Python Programming | InsideAIML
Introduction to Python Programming | InsideAIML
VijaySharma802
 
ForLoopandUserDefinedFunctions.pptx
ForLoopandUserDefinedFunctions.pptxForLoopandUserDefinedFunctions.pptx
ForLoopandUserDefinedFunctions.pptx
AaliyanShaikh
 
Functions and Modules.pptx
Functions and Modules.pptxFunctions and Modules.pptx
Functions and Modules.pptx
Ashwini Raut
 
powerpoint 2-7.pptx
powerpoint 2-7.pptxpowerpoint 2-7.pptx
powerpoint 2-7.pptx
JuanPicasso7
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdf
prasnt1
 
Ad

More from vikram mahendra (20)

Communication skill
Communication skillCommunication skill
Communication skill
vikram mahendra
 
Python Project On Cosmetic Shop system
Python Project On Cosmetic Shop systemPython Project On Cosmetic Shop system
Python Project On Cosmetic Shop system
vikram mahendra
 
Python Project on Computer Shop
Python Project on Computer ShopPython Project on Computer Shop
Python Project on Computer Shop
vikram mahendra
 
PYTHON PROJECT ON CARSHOP SYSTEM
PYTHON PROJECT ON CARSHOP SYSTEMPYTHON PROJECT ON CARSHOP SYSTEM
PYTHON PROJECT ON CARSHOP SYSTEM
vikram mahendra
 
BOOK SHOP SYSTEM Project in Python
BOOK SHOP SYSTEM Project in PythonBOOK SHOP SYSTEM Project in Python
BOOK SHOP SYSTEM Project in Python
vikram mahendra
 
FLOWOFCONTROL-IF..ELSE PYTHON
FLOWOFCONTROL-IF..ELSE PYTHONFLOWOFCONTROL-IF..ELSE PYTHON
FLOWOFCONTROL-IF..ELSE PYTHON
vikram mahendra
 
FLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONFLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHON
vikram mahendra
 
OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1
vikram mahendra
 
OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2
vikram mahendra
 
USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2
vikram mahendra
 
DATA TYPE IN PYTHON
DATA TYPE IN PYTHONDATA TYPE IN PYTHON
DATA TYPE IN PYTHON
vikram mahendra
 
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
vikram mahendra
 
INTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHONINTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHON
vikram mahendra
 
Python Introduction
Python IntroductionPython Introduction
Python Introduction
vikram mahendra
 
GREEN SKILL[PART-2]
GREEN SKILL[PART-2]GREEN SKILL[PART-2]
GREEN SKILL[PART-2]
vikram mahendra
 
GREEN SKILLS[PART-1]
GREEN SKILLS[PART-1]GREEN SKILLS[PART-1]
GREEN SKILLS[PART-1]
vikram mahendra
 
Dictionary in python
Dictionary in pythonDictionary in python
Dictionary in python
vikram mahendra
 
Entrepreneurial skills
Entrepreneurial skillsEntrepreneurial skills
Entrepreneurial skills
vikram mahendra
 
Boolean logic
Boolean logicBoolean logic
Boolean logic
vikram mahendra
 
Tuple in python
Tuple in pythonTuple in python
Tuple in python
vikram mahendra
 
Python Project On Cosmetic Shop system
Python Project On Cosmetic Shop systemPython Project On Cosmetic Shop system
Python Project On Cosmetic Shop system
vikram mahendra
 
Python Project on Computer Shop
Python Project on Computer ShopPython Project on Computer Shop
Python Project on Computer Shop
vikram mahendra
 
PYTHON PROJECT ON CARSHOP SYSTEM
PYTHON PROJECT ON CARSHOP SYSTEMPYTHON PROJECT ON CARSHOP SYSTEM
PYTHON PROJECT ON CARSHOP SYSTEM
vikram mahendra
 
BOOK SHOP SYSTEM Project in Python
BOOK SHOP SYSTEM Project in PythonBOOK SHOP SYSTEM Project in Python
BOOK SHOP SYSTEM Project in Python
vikram mahendra
 
FLOWOFCONTROL-IF..ELSE PYTHON
FLOWOFCONTROL-IF..ELSE PYTHONFLOWOFCONTROL-IF..ELSE PYTHON
FLOWOFCONTROL-IF..ELSE PYTHON
vikram mahendra
 
FLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONFLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHON
vikram mahendra
 
OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1
vikram mahendra
 
OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2
vikram mahendra
 
USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2
vikram mahendra
 
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
vikram mahendra
 
INTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHONINTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHON
vikram mahendra
 
Ad

Recently uploaded (20)

How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
What are the benefits that dance brings?
What are the benefits that dance brings?What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.
jmansha170
 
How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18
Celine George
 
Optimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptxOptimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptx
UrmiPrajapati3
 
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
Quiz Club of PSG College of Arts & Science
 
Hemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptxHemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptx
Arshad Shaikh
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
National Information Standards Organization (NISO)
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptxCapitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
IDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptxIDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptx
ArneeAgligar
 
Parenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independenceParenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independence
Pooky Knightsmith
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_HyderabadWebcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
LDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad UpdatesLDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad Updates
LDM & Mia eStudios
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptxWhat is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
GeorgeDiamandis11
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 EmployeeHow to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
What are the benefits that dance brings?
What are the benefits that dance brings?What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.
jmansha170
 
How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18
Celine George
 
Optimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptxOptimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptx
UrmiPrajapati3
 
Hemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptxHemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptx
Arshad Shaikh
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptxCapitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
IDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptxIDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptx
ArneeAgligar
 
Parenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independenceParenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independence
Pooky Knightsmith
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_HyderabadWebcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
LDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad UpdatesLDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad Updates
LDM & Mia eStudios
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptxWhat is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
GeorgeDiamandis11
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 EmployeeHow to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 

USER DEFINE FUNCTIONS IN PYTHON

  • 1. FUNCTION IN PYTHON CLASS: XII COMPUTER SCIENCE -083 USER DEFINED FUNCTIONS PART-3
  • 2. Syntax to define User Defined Functions: def Function_name (parameters or arguments): [statements……] ……………………………. ……………………………. Its keyword and used to define the function in python Statements inside the def begin after four spaces, this is called Indentation. These parameters are optional
  • 3. Example To define User Defined Functions: def myname(): print(“JAMES”) This is function definition To call this function in a program myname() This is function calling ----Output----- JAMES Full python code: def myname(): print(“JAMES”) myname()
  • 4. Example to define: User Defined Functions: Above shown is a function definition that consists of the following components. Keyword def that marks the start of the function header. A function name to uniquely identify the function. Function naming follows the same rules of writing identifiers in Python. Parameters (arguments) through which we pass values to a function. They are optional. A colon (:) to mark the end of the function header. One or more valid python statements that make up the function body. Statements must have the same indentation level (usually 4 spaces). An optional return statement to return a value from the function.
  • 5. Keyword def that marks the start of the function header. def Myprog.py A function name to uniquely identify the function. Function naming follows the same rules of writing identifiers in Python. myfunction() Parameters (arguments) through which we pass values to a function. They are optional. A colon (:) to mark the end of the function header. : One or more valid python statements that make up the function body. Statements must have the same indentation level (usually 4 spaces). statement1 statement2 statement3 myfunction() When we execute the program Myprog.py then myfunction() called It jump inside the function at the top [header] and read all the statements inside the function called
  • 6. Types of User Defined Functions: Default function Parameterized functions Function with parameters and Return type
  • 7. Default function Function without any parameter or parameters and without return type. Syntax: def function_name(): ……..statements…… ……..statements…… function_name()
  • 8. Example:To display the message “Hello World” five time without loop. def mymsg(): print(“Hello World”) mymsg() mymsg() mymsg() mymsg() mymsg() ----OUTPUT------ Hello World Hello World Hello World Hello World Hello World
  • 9. Example:To display the message “Hello World” five time without loop. Full python code: def mymsg(): print(“Hello World”) mymsg() mymsg() mymsg() mymsg() mymsg() But if you do want to use to write so many time the function name , but want to print the message “Hello World” 15 or 20 or 40 times then use loop def mymsg(): print(“Hello World”) for x in range(1,31): mymsg()
  • 10. Example: To define function add() to accept two number and display sum. def add(): x=int(input(“Enter value of x:”)) y=int(input(“Enter value of y:”)) sum=x + y print(“x+y=“,sum) add() ---Output----- Enter value of x: 10 Enter value of y: 20 x+y=30 Declaration and definition part Calling function
  • 11. Example: To define function add() to accept two number and display sum. def add(): x=int(input(“Enter value of x:”)) y=int(input(“Enter value of y:”)) sum=x + y print(“x+y=“,sum) add() ---Output----- Enter value of x: 10 Enter value of y: 20 x+y=30 Declaration and definition part Calling function
  • 12. Example: To define function sub() to accept two number and display subtraction. def sub(): x=int(input(“Enter value of x:”)) y=int(input(“Enter value of y:”)) s=x - y print(“x-y=“,s) sub() ---Output----- Enter value of x: 20 Enter value of y: 10 x-y=10 Declaration and definition part Calling function
  • 13. Example: To define function mult() to accept two number and display Multiply. def mult(): x=int(input(“Enter value of x:”)) y=int(input(“Enter value of y:”)) s=x * y print(“x*y=“,s) mult() ---Output----- Enter value of x: 20 Enter value of y: 10 x*y=200 Declaration and definition part Calling function
  • 14. Example: To define function div() to accept two number and display Division. def div(): x=int(input(“Enter value of x:”)) y=int(input(“Enter value of y:”)) s=x / y print(“x/y=“,s) div() ---Output----- Enter value of x: 15 Enter value of y: 2 x/y=7.5 Declaration and definition part Calling function
  • 15. Now how we combine all the function in one program and ask user to enter the choice and according to choice add, subtract, multiply and divide. ----Main Menu-------- 1- Addition 2- Subtraction 3- Multiplication 4- Division 5- To exit from Program Please enter the choice Program should work like the output given below on the basis of users choice: def add(): x=int(input(“Enter value x:”)) y=int(input(“Enter value y:”)) print(“x+y=“,x+y) def sub(): x=int(input(“Enter value x:”)) y=int(input(“Enter value y:”)) print(“x-y=“,x-y) def mult(): x=int(input(“Enter value x:”)) y=int(input(“Enter value y:”)) print(“x*y=“,x*y) def div(): x=int(input(“Enter value x:”)) y=int(input(“Enter value y:”)) print(“x//y=“,x//y)
  • 16. def add(): x=int(input("Enter value x:")) y=int(input("Enter value y:")) print("x+y=",x+y) def sub(): x=int(input("Enter value x:")) y=int(input("Enter value y:")) print("x-y=",x-y) def mul(): x=int(input("Enter value x:")) y=int(input("Enter value y:")) print("x*y=",x*y) def div(): x=int(input("Enter value x:")) y=int(input("Enter value y:")) print("x//y=",x//y) ch=0 while True: print("1- Addition") print("2- Subtraction") print("3- Multiplication") print("4- Division") print("5- To exit from Program") ch=int(input("Please enter the choice")) if ch==1: add() elif ch==2: sub() elif ch==3: mul() elif ch==4: div() else: break Full python code for the previous question:
  • 17. Example:To create a function checkevenodd() , that accept the number and check its an even or odd def checkevenodd(): no=int(input(“Enter the value:”)) if no%2 == 0: print(“Its an even no”) else: print(“Its an odd number”) ----Output---- Enter the value: 12 Its an even no ----Output---- Enter the value: 17 Its an Odd no
  • 18. Example:To display number from 1 to 20 using function display1to20() Example:To display number table of 3 using function display3() Example:To display even number between 2 to 40 using function displayeven() Example:Program to print triangle using loops and display it through function displaytri() 1 12 123 1234 12345
  • 19. Example:To display number from 1 to 20 using function display1to20() def display1to20(): for x in range(1,21): print(x,sep=“,”,end=“”) display1to20() ---Output---- 1,2,3,4,5,6……….,20
  • 20. Example:To display number table of 3 using function display3() def display3(): for x in range(1,11): print(x*3) display3() ---Output---- 3 6 9 12 15 . . . 30
  • 21. Example:To display number from 1 to 20 using function display1to20() Example:To display number table of 3 using function display3() Example:To display even number between 2 to 40 using function displayeven() Example:Program to print triangle using loops and display it through function displaytri() 1 12 123 1234 12345
  • 22. Example:To display number from 1 to 20 using function display1to20() Example:To display number table of 3 using function display3() Example:To display even number between 2 to 40 using function displayeven() Example:Program to print triangle using loops and display it through function displaytri() 1 12 123 1234 12345