3
Most read
5
Most read
10
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

DOCX
List of programs to practice for ICSE
PPTX
Functions in python
PPTX
wired and wireless networks
PPTX
PPT Backbone And Networks
PPTX
Wireless LAN technologies
DOCX
evt lab manual.docx
PPTX
FLOW OF CONTROL-NESTED IFS IN PYTHON
PPTX
Higher Education: challenges and opportunities
List of programs to practice for ICSE
Functions in python
wired and wireless networks
PPT Backbone And Networks
Wireless LAN technologies
evt lab manual.docx
FLOW OF CONTROL-NESTED IFS IN PYTHON
Higher Education: challenges and opportunities

What's hot (20)

PPTX
Functions in python slide share
PDF
Operators in python
PDF
How to use Map() Filter() and Reduce() functions in Python | Edureka
PDF
Functions and modules in python
PPTX
Python functions
PPTX
Preprocessor directives in c language
PPTX
Chapter 02 functions -class xii
PDF
Datatypes in python
PDF
Python exception handling
PPTX
Conditional and control statement
PPTX
python conditional statement.pptx
PPTX
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
PPTX
Operators in Python
PPTX
11 Unit 1 Chapter 02 Python Fundamentals
PPTX
String Manipulation in Python
PPT
Python Control structures
PPTX
Python dictionary
PDF
List,tuple,dictionary
PDF
Control Structures in Python
Functions in python slide share
Operators in python
How to use Map() Filter() and Reduce() functions in Python | Edureka
Functions and modules in python
Python functions
Preprocessor directives in c language
Chapter 02 functions -class xii
Datatypes in python
Python exception handling
Conditional and control statement
python conditional statement.pptx
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
Operators in Python
11 Unit 1 Chapter 02 Python Fundamentals
String Manipulation in Python
Python Control structures
Python dictionary
List,tuple,dictionary
Control Structures in Python
Ad

Similar to USER DEFINE FUNCTIONS IN PYTHON (20)

PPTX
Working with functions.pptx. Hb.
PPTX
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
PDF
Python lambda functions with filter, map & reduce function
PPTX
Python_Unit-1_PPT_Data Types.pptx
PPT
Functions and pointers_unit_4
PPTX
Python crush course
PPTX
Dti2143 chapter 5
PPTX
Python Functions.pptx
PPTX
Python Functions.pptx
PPT
Functions and pointers_unit_4
PDF
Functions_19_20.pdf
PPT
Functions in c
PPTX
CS 1106 - UNIT -3 notes for students to learn
PPTX
Python Lecture 4
PDF
Python basic
PPTX
JNTUK python programming python unit 3.pptx
PDF
III MCS python lab (1).pdf
PPTX
Functions2.pptx
PPTX
2 Functions2.pptx
PDF
Functions
Working with functions.pptx. Hb.
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
Python lambda functions with filter, map & reduce function
Python_Unit-1_PPT_Data Types.pptx
Functions and pointers_unit_4
Python crush course
Dti2143 chapter 5
Python Functions.pptx
Python Functions.pptx
Functions and pointers_unit_4
Functions_19_20.pdf
Functions in c
CS 1106 - UNIT -3 notes for students to learn
Python Lecture 4
Python basic
JNTUK python programming python unit 3.pptx
III MCS python lab (1).pdf
Functions2.pptx
2 Functions2.pptx
Functions
Ad

More from vikram mahendra (20)

PPTX
Communication skill
PDF
Python Project On Cosmetic Shop system
PDF
Python Project on Computer Shop
PDF
PYTHON PROJECT ON CARSHOP SYSTEM
PDF
BOOK SHOP SYSTEM Project in Python
PPTX
FLOWOFCONTROL-IF..ELSE PYTHON
PPTX
FLOW OF CONTROL-INTRO PYTHON
PPTX
OPERATOR IN PYTHON-PART1
PPTX
OPERATOR IN PYTHON-PART2
PPTX
USE OF PRINT IN PYTHON PART 2
PPTX
DATA TYPE IN PYTHON
PPTX
INTRODUCTION TO FUNCTIONS IN PYTHON
PPTX
Python Introduction
PPTX
GREEN SKILL[PART-2]
PPTX
GREEN SKILLS[PART-1]
PPTX
Dictionary in python
PPTX
Entrepreneurial skills
PPTX
Boolean logic
PPTX
Tuple in python
PPTX
LIST IN PYTHON[SELECTION SORT]
Communication skill
Python Project On Cosmetic Shop system
Python Project on Computer Shop
PYTHON PROJECT ON CARSHOP SYSTEM
BOOK SHOP SYSTEM Project in Python
FLOWOFCONTROL-IF..ELSE PYTHON
FLOW OF CONTROL-INTRO PYTHON
OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART2
USE OF PRINT IN PYTHON PART 2
DATA TYPE IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHON
Python Introduction
GREEN SKILL[PART-2]
GREEN SKILLS[PART-1]
Dictionary in python
Entrepreneurial skills
Boolean logic
Tuple in python
LIST IN PYTHON[SELECTION SORT]

Recently uploaded (20)

PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
PDF
advance database management system book.pdf
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
PPTX
Virtual and Augmented Reality in Current Scenario
PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PDF
Environmental Education MCQ BD2EE - Share Source.pdf
PDF
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
PDF
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PDF
Mucosal Drug Delivery system_NDDS_BPHARMACY__SEM VII_PCI.pdf
PDF
What if we spent less time fighting change, and more time building what’s rig...
PPTX
Introduction to pro and eukaryotes and differences.pptx
PDF
AI-driven educational solutions for real-life interventions in the Philippine...
PDF
MICROENCAPSULATION_NDDS_BPHARMACY__SEM VII_PCI .pdf
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PPTX
B.Sc. DS Unit 2 Software Engineering.pptx
PDF
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 2).pdf
PPTX
What’s under the hood: Parsing standardized learning content for AI
PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
advance database management system book.pdf
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
Virtual and Augmented Reality in Current Scenario
Share_Module_2_Power_conflict_and_negotiation.pptx
Environmental Education MCQ BD2EE - Share Source.pdf
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
Mucosal Drug Delivery system_NDDS_BPHARMACY__SEM VII_PCI.pdf
What if we spent less time fighting change, and more time building what’s rig...
Introduction to pro and eukaryotes and differences.pptx
AI-driven educational solutions for real-life interventions in the Philippine...
MICROENCAPSULATION_NDDS_BPHARMACY__SEM VII_PCI .pdf
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
B.Sc. DS Unit 2 Software Engineering.pptx
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 2).pdf
What’s under the hood: Parsing standardized learning content for AI
A powerpoint presentation on the Revised K-10 Science Shaping Paper

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