SlideShare a Scribd company logo
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
Ad

More Related Content

What's hot (20)

Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
Lovely Professional University
 
Java Data Types
Java Data TypesJava Data Types
Java Data Types
Spotle.ai
 
Python Programming
Python ProgrammingPython Programming
Python Programming
Saravanan T.M
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
Mohammed Sikander
 
File Handling in Python
File Handling in PythonFile Handling in Python
File Handling in Python
International Institute of Information Technology (I²IT)
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
VINOTH R
 
How to use Map() Filter() and Reduce() functions in Python | Edureka
How to use Map() Filter() and Reduce() functions in Python | EdurekaHow to use Map() Filter() and Reduce() functions in Python | Edureka
How to use Map() Filter() and Reduce() functions in Python | Edureka
Edureka!
 
Input output streams
Input output streamsInput output streams
Input output streams
Parthipan Parthi
 
Python-Inheritance.pptx
Python-Inheritance.pptxPython-Inheritance.pptx
Python-Inheritance.pptx
Karudaiyar Ganapathy
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
Jussi Pohjolainen
 
Python tuple
Python   tuplePython   tuple
Python tuple
Mohammed Sikander
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
Abhilash Nair
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
Megha V
 
Dictionaries in Python
Dictionaries in PythonDictionaries in Python
Dictionaries in Python
baabtra.com - No. 1 supplier of quality freshers
 
Built in function
Built in functionBuilt in function
Built in function
MD. Rayhanul Islam Sayket
 
String in java
String in javaString in java
String in java
Ideal Eyes Business College
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
eShikshak
 
Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and Operators
Marwa Ali Eissa
 
Wrapper class
Wrapper classWrapper class
Wrapper class
kamal kotecha
 
Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
Abhilash Nair
 

Similar to USER DEFINE FUNCTIONS IN PYTHON (20)

Working with functions.pptx. Hb.
Working with functions.pptx.          Hb.Working with functions.pptx.          Hb.
Working with functions.pptx. Hb.
sabarivelan111007
 
Python lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce functionPython lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce function
ARVIND PANDE
 
Python_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptxPython_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptx
SahajShrimal1
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
MKalpanaDevi
 
Python crush course
Python crush coursePython crush course
Python crush course
Mohammed El Rafie Tarabay
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
alish sha
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
AnuragBharti27
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
AnuragBharti27
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
Praveen M Jigajinni
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
Saranya saran
 
Functions_19_20.pdf
Functions_19_20.pdfFunctions_19_20.pdf
Functions_19_20.pdf
paijitk
 
Functions in c
Functions in cFunctions in c
Functions in c
KavithaMuralidharan2
 
Python Lecture 4
Python Lecture 4Python Lecture 4
Python Lecture 4
Inzamam Baig
 
Python basic
Python basicPython basic
Python basic
Saifuddin Kaijar
 
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
 
III MCS python lab (1).pdf
III MCS python lab (1).pdfIII MCS python lab (1).pdf
III MCS python lab (1).pdf
srxerox
 
Functions2.pptx
Functions2.pptxFunctions2.pptx
Functions2.pptx
AkhilTyagi42
 
2 Functions2.pptx
2 Functions2.pptx2 Functions2.pptx
2 Functions2.pptx
RohitYadav830391
 
Functions
FunctionsFunctions
Functions
Swarup Boro
 
Python functions PYTHON FUNCTIONS1234567
Python functions PYTHON FUNCTIONS1234567Python functions PYTHON FUNCTIONS1234567
Python functions PYTHON FUNCTIONS1234567
AnjaneyuluKunchala1
 
Working with functions.pptx. Hb.
Working with functions.pptx.          Hb.Working with functions.pptx.          Hb.
Working with functions.pptx. Hb.
sabarivelan111007
 
Python lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce functionPython lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce function
ARVIND PANDE
 
Python_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptxPython_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptx
SahajShrimal1
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
MKalpanaDevi
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
alish sha
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
Saranya saran
 
Functions_19_20.pdf
Functions_19_20.pdfFunctions_19_20.pdf
Functions_19_20.pdf
paijitk
 
III MCS python lab (1).pdf
III MCS python lab (1).pdfIII MCS python lab (1).pdf
III MCS python lab (1).pdf
srxerox
 
Python functions PYTHON FUNCTIONS1234567
Python functions PYTHON FUNCTIONS1234567Python functions PYTHON FUNCTIONS1234567
Python functions PYTHON FUNCTIONS1234567
AnjaneyuluKunchala1
 
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
 
FLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHONFLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS 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
 
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
 
FLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHONFLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS 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)

2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
Dr. Nasir Mustafa
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast BrooklynBridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
i4jd41bk
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
The History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptxThe History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
APGAR SCORE BY sweety Tamanna Mahapatra MSc Pediatric
APGAR SCORE  BY sweety Tamanna Mahapatra MSc PediatricAPGAR SCORE  BY sweety Tamanna Mahapatra MSc Pediatric
APGAR SCORE BY sweety Tamanna Mahapatra MSc Pediatric
SweetytamannaMohapat
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and GuestsLDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDM Mia eStudios
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
Dr. Nasir Mustafa
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast BrooklynBridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
i4jd41bk
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
APGAR SCORE BY sweety Tamanna Mahapatra MSc Pediatric
APGAR SCORE  BY sweety Tamanna Mahapatra MSc PediatricAPGAR SCORE  BY sweety Tamanna Mahapatra MSc Pediatric
APGAR SCORE BY sweety Tamanna Mahapatra MSc Pediatric
SweetytamannaMohapat
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and GuestsLDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDM Mia eStudios
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 

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
  翻译: