SlideShare una empresa de Scribd logo
1 de 22
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

Más contenido relacionado

La actualidad más candente

Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in PythonDevashish Kumar
 
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...Edureka!
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide shareDevashish Kumar
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in pythoneShikshak
 
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
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionarySoba Arjun
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in pythonRaginiJain21
 
Modules in Python Programming
Modules in Python ProgrammingModules in Python Programming
Modules in Python Programmingsambitmandal
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in clavanya marichamy
 
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]vikram mahendra
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingMoutaz Haddara
 
Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)Kamal Acharya
 
Data types in python
Data types in pythonData types in python
Data types in pythonRaginiJain21
 

La actualidad más candente (20)

Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
 
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 
Python : Functions
Python : FunctionsPython : Functions
Python : Functions
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
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]
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
 
Modules in Python Programming
Modules in Python ProgrammingModules in Python Programming
Modules in Python Programming
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
 
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented Programming
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
 
Python OOPs
Python OOPsPython OOPs
Python OOPs
 
Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)
 
Object oriented programming in python
Object oriented programming in pythonObject oriented programming in python
Object oriented programming in python
 
Data types in python
Data types in pythonData types in python
Data types in python
 
File in c
File in cFile in c
File in c
 

Similar a USER DEFINE FUNCTIONS IN PYTHON

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 functionARVIND 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.pptxSahajShrimal1
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4MKalpanaDevi
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5alish sha
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4Saranya saran
 
Functions_19_20.pdf
Functions_19_20.pdfFunctions_19_20.pdf
Functions_19_20.pdfpaijitk
 
III MCS python lab (1).pdf
III MCS python lab (1).pdfIII MCS python lab (1).pdf
III MCS python lab (1).pdfsrxerox
 
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱Mohammad Reza Kamalifard
 

Similar a USER DEFINE FUNCTIONS IN PYTHON (20)

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
 
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
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Python crush course
Python crush coursePython crush course
Python crush course
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Functions_19_20.pdf
Functions_19_20.pdfFunctions_19_20.pdf
Functions_19_20.pdf
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Python Lecture 4
Python Lecture 4Python Lecture 4
Python Lecture 4
 
Python basic
Python basicPython basic
Python basic
 
III MCS python lab (1).pdf
III MCS python lab (1).pdfIII MCS python lab (1).pdf
III MCS python lab (1).pdf
 
Functions2.pptx
Functions2.pptxFunctions2.pptx
Functions2.pptx
 
2 Functions2.pptx
2 Functions2.pptx2 Functions2.pptx
2 Functions2.pptx
 
Functions
FunctionsFunctions
Functions
 
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
 
functions
functionsfunctions
functions
 
Functions.docx
Functions.docxFunctions.docx
Functions.docx
 

Más de vikram mahendra

Python Project On Cosmetic Shop system
Python Project On Cosmetic Shop systemPython Project On Cosmetic Shop system
Python Project On Cosmetic Shop systemvikram mahendra
 
Python Project on Computer Shop
Python Project on Computer ShopPython Project on Computer Shop
Python Project on Computer Shopvikram mahendra
 
PYTHON PROJECT ON CARSHOP SYSTEM
PYTHON PROJECT ON CARSHOP SYSTEMPYTHON PROJECT ON CARSHOP SYSTEM
PYTHON PROJECT ON CARSHOP SYSTEMvikram mahendra
 
BOOK SHOP SYSTEM Project in Python
BOOK SHOP SYSTEM Project in PythonBOOK SHOP SYSTEM Project in Python
BOOK SHOP SYSTEM Project in Pythonvikram 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 PYTHONvikram mahendra
 
FLOWOFCONTROL-IF..ELSE PYTHON
FLOWOFCONTROL-IF..ELSE PYTHONFLOWOFCONTROL-IF..ELSE PYTHON
FLOWOFCONTROL-IF..ELSE PYTHONvikram mahendra
 
FLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONFLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONvikram mahendra
 
OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1vikram mahendra
 
OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2vikram 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 2vikram mahendra
 
INTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHONINTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHONvikram mahendra
 

Más de vikram mahendra (20)

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

Último

MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Developmentchesterberbo7
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptxmary850239
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptxmary850239
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesVijayaLaxmi84
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxkarenfajardo43
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 

Último (20)

MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Development
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their uses
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 

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