際際滷

際際滷Share a Scribd company logo
Functions in
PYTHON
Rahul Singh Sikarwar
Functions
 A function is a device that groups a set of statements so they
can be run more than once in a programa packaged
procedure invoked by name
 Functions also can compute a result value and let us specify
parameters that serve as function inputs and may differ each
time the code is run.
 More fundamentally, functions are the alternative to
programming by cutting and pastingrather than having
multiple redundant copies of an operations code, we can
factor it into a single function.
Function-related statements and
expressions
Introduction
 Functions are a nearly universal program-structuring device.
You may have come across them before in other languages,
where they may have been called subroutines or procedures.
 As a brief introduction, functions serve two primary
development roles:
1. Maximizing code reuse and minimizing redundancy
2. Procedural decomposition
 def is executable code. Python functions are written with a new
statement, the def.
 def is an executable statementyour function does not exist until
Python reaches and runs the def. In fact, its legal (and even
occasionally useful) to nest def statements inside if statements,
while loops, and even other defs.
 def creates an object and assigns it to a name.: When Python
reaches and runs a def statement, it generates a new function
object and assigns it to the functions name. As with all
assignments, the function name becomes a reference to the
function object
 lambda creates an object but returns it as a result: Functions
may also be created with the lambda expression, a feature that
allows us to in-line function definitions in places where a def
statement wont work syntactically.
 return sends a result object back to the caller: When a
function is called, the caller stops until the function finishes its
work and returns control to the caller. Functions that compute
a value send it back to the caller with a return statement; the
returned value becomes the result of the function call. A return
without a value simply returns to the caller
 yield sends a result object back to the caller, but
remembers where it left off. Functions known as generators
may also use the yield statement to send back a value and
suspend their state such that they may be resumed later, to
produce a series of results over time
def Statements
 The def statement creates a function object and assigns it to a name. Its general format is as follows:
def name(arg1, arg2,... argN):
Statements
Function bodies often contain a return statement:
def name(arg1, arg2,... argN):
...
return value
The Python return statement can show up anywhere in a function body; when reached, it ends the
function call and sends a result back to the caller. The return statement consists of an optional object
value expression that gives the functions result. If the value is omitted, return sends back a None.
nest a function def inside an if statement to
select between alternative definitions:
if test:
def func(): # Define func this way
...
else:
def func(): # Or else this way
...
...
func() # Call the version selected and built
A First Example: Definitions and Calls
Definition:
Heres a definition typed interactively that defines a function called
times, which returns the product of its two arguments:
>>> def times(x, y): # Create and assign function
... return x * y # Body executed when called
...
>>> times(2, 4) # Arguments in parentheses
8
Calls
Python program for solving a quadratic equation of the form ax
2
+bx+c=0
1. Use function def() to find the solution
2. a ,b and c is user input.
Input 1: a= 1, b=-3, c=2
Input 2: a= 1, b=-8, c=5
Input 3: a=5, b= 20, c= 32
use of Functions to write python program.pptx

More Related Content

Similar to use of Functions to write python program.pptx (20)

Powerpoint presentation for Python Functions
Powerpoint presentation for Python FunctionsPowerpoint presentation for Python Functions
Powerpoint presentation for Python Functions
BalaSubramanian376976
Functions in C
Functions in CFunctions in C
Functions in C
Kamal Acharya
function of C.pptx
function of C.pptxfunction of C.pptx
function of C.pptx
shivas379526
functions new.pptx
functions new.pptxfunctions new.pptx
functions new.pptx
bhuvanalakshmik2
4. function
4. function4. function
4. function
Shankar Gangaju
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
TeshaleSiyum
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdf
TeshaleSiyum
Lecture 11 - Functions
Lecture 11 - FunctionsLecture 11 - Functions
Lecture 11 - Functions
Md. Imran Hossain Showrov
Python functions
Python functionsPython functions
Python functions
Prof. Dr. K. Adisesha
Functions and modular programming.pptx
Functions and modular programming.pptxFunctions and modular programming.pptx
Functions and modular programming.pptx
zueZ3
Functions
FunctionsFunctions
Functions
Septi Ratnasari
User defined function in C.pptx
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptx
Rhishav Poudyal
Function
FunctionFunction
Function
GauravGautam224100
Ch4 functions
Ch4 functionsCh4 functions
Ch4 functions
Hattori Sidek
Py-際際滷s-3 difficultpythoncoursefforbeginners.ppt
Py-際際滷s-3 difficultpythoncoursefforbeginners.pptPy-際際滷s-3 difficultpythoncoursefforbeginners.ppt
Py-際際滷s-3 difficultpythoncoursefforbeginners.ppt
mohamedsamydeveloper
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1
YOGESH SINGH
functions.pptx
functions.pptxfunctions.pptx
functions.pptx
KavithaChekuri3
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
miki304759
C functions
C functionsC functions
C functions
University of Potsdam
Java script function
Java script functionJava script function
Java script function
suresh raj sharma
Powerpoint presentation for Python Functions
Powerpoint presentation for Python FunctionsPowerpoint presentation for Python Functions
Powerpoint presentation for Python Functions
BalaSubramanian376976
function of C.pptx
function of C.pptxfunction of C.pptx
function of C.pptx
shivas379526
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
TeshaleSiyum
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdf
TeshaleSiyum
Functions and modular programming.pptx
Functions and modular programming.pptxFunctions and modular programming.pptx
Functions and modular programming.pptx
zueZ3
User defined function in C.pptx
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptx
Rhishav Poudyal
Py-際際滷s-3 difficultpythoncoursefforbeginners.ppt
Py-際際滷s-3 difficultpythoncoursefforbeginners.pptPy-際際滷s-3 difficultpythoncoursefforbeginners.ppt
Py-際際滷s-3 difficultpythoncoursefforbeginners.ppt
mohamedsamydeveloper
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1
YOGESH SINGH
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
miki304759

Recently uploaded (20)

Jotform AI Agents: Overview and Benefits
Jotform AI Agents: Overview and BenefitsJotform AI Agents: Overview and Benefits
Jotform AI Agents: Overview and Benefits
Jotform
Autodesk MotionBuilder 2026 Free Download
Autodesk MotionBuilder 2026 Free DownloadAutodesk MotionBuilder 2026 Free Download
Autodesk MotionBuilder 2026 Free Download
blouch52kp
Threat Modeling & Risk Assessment Webinar: A Step-by-Step Example
Threat Modeling & Risk Assessment Webinar: A Step-by-Step ExampleThreat Modeling & Risk Assessment Webinar: A Step-by-Step Example
Threat Modeling & Risk Assessment Webinar: A Step-by-Step Example
ICS
Software Architecture and Design-Ch-1.v6
Software Architecture and Design-Ch-1.v6Software Architecture and Design-Ch-1.v6
Software Architecture and Design-Ch-1.v6
Salahaddin University-Erbil, University of Kurdistan Hewler
REVIEW AI Apps Empire The Future of No-Code Ai Apps is Here!
REVIEW AI Apps Empire The Future of No-Code Ai Apps is Here!REVIEW AI Apps Empire The Future of No-Code Ai Apps is Here!
REVIEW AI Apps Empire The Future of No-Code Ai Apps is Here!
stanislausGabriel
The Open-Closed Principle - Part 2 - The Contemporary Version - An Introduction
The Open-Closed Principle - Part 2 - The Contemporary Version - An IntroductionThe Open-Closed Principle - Part 2 - The Contemporary Version - An Introduction
The Open-Closed Principle - Part 2 - The Contemporary Version - An Introduction
Philip Schwarz
Top Performance Testing Tools of 2025: Ensure Speed, Stability, and Scale
Top Performance Testing Tools of 2025: Ensure Speed, Stability, and ScaleTop Performance Testing Tools of 2025: Ensure Speed, Stability, and Scale
Top Performance Testing Tools of 2025: Ensure Speed, Stability, and Scale
Shubham Joshi
RocketVideos AI The Ultimate AI Video Creation Tool
RocketVideos AI   The Ultimate AI Video Creation ToolRocketVideos AI   The Ultimate AI Video Creation Tool
RocketVideos AI The Ultimate AI Video Creation Tool
Richmaven
Image-Line FL Studio 20.8.3.2304 crack free
Image-Line FL Studio 20.8.3.2304 crack freeImage-Line FL Studio 20.8.3.2304 crack free
Image-Line FL Studio 20.8.3.2304 crack free
alihamzakpa084
Wondershare Recoverit 13.5.11.3 Free crack
Wondershare Recoverit 13.5.11.3 Free crackWondershare Recoverit 13.5.11.3 Free crack
Wondershare Recoverit 13.5.11.3 Free crack
blouch52kp
Software Architecture and Design in the Age of Code Assist tools.pdf
Software Architecture and Design in the Age of Code Assist tools.pdfSoftware Architecture and Design in the Age of Code Assist tools.pdf
Software Architecture and Design in the Age of Code Assist tools.pdf
Manu Pk
Oracle Database administration Security PPT
Oracle Database administration Security PPTOracle Database administration Security PPT
Oracle Database administration Security PPT
pshankarnarayan
Marketo User Group - Singapore - April 2025
Marketo User Group - Singapore - April 2025Marketo User Group - Singapore - April 2025
Marketo User Group - Singapore - April 2025
BradBedford3
microsoft office 2019 crack free download
microsoft office 2019 crack free downloadmicrosoft office 2019 crack free download
microsoft office 2019 crack free download
mohsinrazakpa39
Building-Your-Professional-Website-No-Coding-Required
Building-Your-Professional-Website-No-Coding-RequiredBuilding-Your-Professional-Website-No-Coding-Required
Building-Your-Professional-Website-No-Coding-Required
Ozias Rondon
Adobe Photoshop 2025 Free crack Download
Adobe Photoshop 2025 Free crack DownloadAdobe Photoshop 2025 Free crack Download
Adobe Photoshop 2025 Free crack Download
juttjolie9
AI has already changed software development.pdf
AI has already changed software development.pdfAI has already changed software development.pdf
AI has already changed software development.pdf
Radam辿s Roriz
Jotform AI Agents: Real User Success Stories
Jotform AI Agents: Real User Success StoriesJotform AI Agents: Real User Success Stories
Jotform AI Agents: Real User Success Stories
Jotform
IObit Driver Booster Pro 12.3.0.557 Free
IObit Driver Booster Pro 12.3.0.557 FreeIObit Driver Booster Pro 12.3.0.557 Free
IObit Driver Booster Pro 12.3.0.557 Free
blouch51kp
Lecture2_REQUIREMENT_Process__Modelss.pptx
Lecture2_REQUIREMENT_Process__Modelss.pptxLecture2_REQUIREMENT_Process__Modelss.pptx
Lecture2_REQUIREMENT_Process__Modelss.pptx
Aqsa162589
Jotform AI Agents: Overview and Benefits
Jotform AI Agents: Overview and BenefitsJotform AI Agents: Overview and Benefits
Jotform AI Agents: Overview and Benefits
Jotform
Autodesk MotionBuilder 2026 Free Download
Autodesk MotionBuilder 2026 Free DownloadAutodesk MotionBuilder 2026 Free Download
Autodesk MotionBuilder 2026 Free Download
blouch52kp
Threat Modeling & Risk Assessment Webinar: A Step-by-Step Example
Threat Modeling & Risk Assessment Webinar: A Step-by-Step ExampleThreat Modeling & Risk Assessment Webinar: A Step-by-Step Example
Threat Modeling & Risk Assessment Webinar: A Step-by-Step Example
ICS
REVIEW AI Apps Empire The Future of No-Code Ai Apps is Here!
REVIEW AI Apps Empire The Future of No-Code Ai Apps is Here!REVIEW AI Apps Empire The Future of No-Code Ai Apps is Here!
REVIEW AI Apps Empire The Future of No-Code Ai Apps is Here!
stanislausGabriel
The Open-Closed Principle - Part 2 - The Contemporary Version - An Introduction
The Open-Closed Principle - Part 2 - The Contemporary Version - An IntroductionThe Open-Closed Principle - Part 2 - The Contemporary Version - An Introduction
The Open-Closed Principle - Part 2 - The Contemporary Version - An Introduction
Philip Schwarz
Top Performance Testing Tools of 2025: Ensure Speed, Stability, and Scale
Top Performance Testing Tools of 2025: Ensure Speed, Stability, and ScaleTop Performance Testing Tools of 2025: Ensure Speed, Stability, and Scale
Top Performance Testing Tools of 2025: Ensure Speed, Stability, and Scale
Shubham Joshi
RocketVideos AI The Ultimate AI Video Creation Tool
RocketVideos AI   The Ultimate AI Video Creation ToolRocketVideos AI   The Ultimate AI Video Creation Tool
RocketVideos AI The Ultimate AI Video Creation Tool
Richmaven
Image-Line FL Studio 20.8.3.2304 crack free
Image-Line FL Studio 20.8.3.2304 crack freeImage-Line FL Studio 20.8.3.2304 crack free
Image-Line FL Studio 20.8.3.2304 crack free
alihamzakpa084
Wondershare Recoverit 13.5.11.3 Free crack
Wondershare Recoverit 13.5.11.3 Free crackWondershare Recoverit 13.5.11.3 Free crack
Wondershare Recoverit 13.5.11.3 Free crack
blouch52kp
Software Architecture and Design in the Age of Code Assist tools.pdf
Software Architecture and Design in the Age of Code Assist tools.pdfSoftware Architecture and Design in the Age of Code Assist tools.pdf
Software Architecture and Design in the Age of Code Assist tools.pdf
Manu Pk
Oracle Database administration Security PPT
Oracle Database administration Security PPTOracle Database administration Security PPT
Oracle Database administration Security PPT
pshankarnarayan
Marketo User Group - Singapore - April 2025
Marketo User Group - Singapore - April 2025Marketo User Group - Singapore - April 2025
Marketo User Group - Singapore - April 2025
BradBedford3
microsoft office 2019 crack free download
microsoft office 2019 crack free downloadmicrosoft office 2019 crack free download
microsoft office 2019 crack free download
mohsinrazakpa39
Building-Your-Professional-Website-No-Coding-Required
Building-Your-Professional-Website-No-Coding-RequiredBuilding-Your-Professional-Website-No-Coding-Required
Building-Your-Professional-Website-No-Coding-Required
Ozias Rondon
Adobe Photoshop 2025 Free crack Download
Adobe Photoshop 2025 Free crack DownloadAdobe Photoshop 2025 Free crack Download
Adobe Photoshop 2025 Free crack Download
juttjolie9
AI has already changed software development.pdf
AI has already changed software development.pdfAI has already changed software development.pdf
AI has already changed software development.pdf
Radam辿s Roriz
Jotform AI Agents: Real User Success Stories
Jotform AI Agents: Real User Success StoriesJotform AI Agents: Real User Success Stories
Jotform AI Agents: Real User Success Stories
Jotform
IObit Driver Booster Pro 12.3.0.557 Free
IObit Driver Booster Pro 12.3.0.557 FreeIObit Driver Booster Pro 12.3.0.557 Free
IObit Driver Booster Pro 12.3.0.557 Free
blouch51kp
Lecture2_REQUIREMENT_Process__Modelss.pptx
Lecture2_REQUIREMENT_Process__Modelss.pptxLecture2_REQUIREMENT_Process__Modelss.pptx
Lecture2_REQUIREMENT_Process__Modelss.pptx
Aqsa162589

use of Functions to write python program.pptx

  • 2. Functions A function is a device that groups a set of statements so they can be run more than once in a programa packaged procedure invoked by name Functions also can compute a result value and let us specify parameters that serve as function inputs and may differ each time the code is run. More fundamentally, functions are the alternative to programming by cutting and pastingrather than having multiple redundant copies of an operations code, we can factor it into a single function.
  • 4. Introduction Functions are a nearly universal program-structuring device. You may have come across them before in other languages, where they may have been called subroutines or procedures. As a brief introduction, functions serve two primary development roles: 1. Maximizing code reuse and minimizing redundancy 2. Procedural decomposition
  • 5. def is executable code. Python functions are written with a new statement, the def. def is an executable statementyour function does not exist until Python reaches and runs the def. In fact, its legal (and even occasionally useful) to nest def statements inside if statements, while loops, and even other defs. def creates an object and assigns it to a name.: When Python reaches and runs a def statement, it generates a new function object and assigns it to the functions name. As with all assignments, the function name becomes a reference to the function object lambda creates an object but returns it as a result: Functions may also be created with the lambda expression, a feature that allows us to in-line function definitions in places where a def statement wont work syntactically.
  • 6. return sends a result object back to the caller: When a function is called, the caller stops until the function finishes its work and returns control to the caller. Functions that compute a value send it back to the caller with a return statement; the returned value becomes the result of the function call. A return without a value simply returns to the caller yield sends a result object back to the caller, but remembers where it left off. Functions known as generators may also use the yield statement to send back a value and suspend their state such that they may be resumed later, to produce a series of results over time
  • 7. def Statements The def statement creates a function object and assigns it to a name. Its general format is as follows: def name(arg1, arg2,... argN): Statements Function bodies often contain a return statement: def name(arg1, arg2,... argN): ... return value The Python return statement can show up anywhere in a function body; when reached, it ends the function call and sends a result back to the caller. The return statement consists of an optional object value expression that gives the functions result. If the value is omitted, return sends back a None.
  • 8. nest a function def inside an if statement to select between alternative definitions: if test: def func(): # Define func this way ... else: def func(): # Or else this way ... ... func() # Call the version selected and built
  • 9. A First Example: Definitions and Calls Definition: Heres a definition typed interactively that defines a function called times, which returns the product of its two arguments: >>> def times(x, y): # Create and assign function ... return x * y # Body executed when called ... >>> times(2, 4) # Arguments in parentheses 8
  • 10. Calls
  • 11. Python program for solving a quadratic equation of the form ax 2 +bx+c=0 1. Use function def() to find the solution 2. a ,b and c is user input. Input 1: a= 1, b=-3, c=2 Input 2: a= 1, b=-8, c=5 Input 3: a=5, b= 20, c= 32

Editor's Notes

  • #4: As in most programming languages, Python functions are the simplest way to package logic you may wish to use in more than one place and more than one time. Functions allow us to group and generalize code to be used arbitrarily many times later. Because they allow us to code an operation in a single place and use it in many places, Python functions are the most basic factoring tool in the language: they allow us to reduce code redundancy in our programs, and thereby reduce maintenance effort. Functions also provide a tool for splitting systems into pieces that have well-defined roles. For instance, to make a pizza from scratch, you would start by mixing the dough, rolling it out, adding toppings, baking it, and so on. If you were programming a pizza-making robot, functions would help you divide the overall make pizza task into chunksone function for each subtask in the process. Its easier to implement the smaller tasks in isolation than it is to implement the entire process at once
  • #7: The statement block becomes the functions bodythat is, the code Python executes each time the function is later called.