SlideShare a Scribd company logo
Functions
Python Functions
• There are two kinds of functions in Python.
- Built-in functions that are provided as part of Python - print(),
input(), type(), float(), int() ...
- Functions that we define ourselves and then use
• We treat function names as “new” reserved words
(i.e., we avoid them as variable names)
Function Definition
• In Python a function is some reusable code that takes
arguments(s) as input, does some computation, and then returns
a result or results
• We define a function using the def reserved word
• We call/invoke the function by using the function name,
parentheses, and arguments in an expression
Functions of Our Own…
Building our Own Functions
• We create a new function using the def keyword followed by
optional parameters in parentheses
• We indent the body of the function
• This defines the function but does not execute the body of the
function
def print_lyrics():
print("I'm a lumberjack, and I'm okay.")
print('I sleep all night and I work all day.')
x = 5
print('Hello')
def print_lyrics():
print("I'm a lumberjack, and I'm okay.")
print('I sleep all night and I work all day.')
print('Yo')
x = x + 2
print(x)
Hello
Yo
7
print("I'm a lumberjack, and I'm okay.")
print('I sleep all night and I work all day.')
print_lyrics():
Definitions and Uses
• Once we have defined a function, we can call (or invoke) it
as many times as we like
• This is the store and reuse pattern
x = 5
print('Hello')
def print_lyrics():
print("I'm a lumberjack, and I'm okay.")
print('I sleep all night and I work all day.')
print('Yo')
print_lyrics()
x = x + 2
print(x)
Hello
Yo
I'm a lumberjack, and I'm okay.
I sleep all night and I work all day.
7
Arguments
• An argument is a value we pass into the function as its input
when we call the function
• We use arguments so we can direct the function to do different
kinds of work when we call it at different times
• We put the arguments in parentheses after the name of the
function
big = max('Hello world')
Argument
Parameters
A parameter is a variable which
we use in the function definition.
It is a “handle” that allows the
code in the function to access
the arguments for a particular
function invocation.
>>> def greet(lang):
... if lang == 'es':
... print('Hola')
... elif lang == 'fr':
... print('Bonjour')
... else:
... print('Hello')
...
>>> greet('en')
Hello
>>> greet('es')
Hola
>>> greet('fr')
Bonjour
>>>
Parameter
Argument
Return Values
Often a function will take its arguments, do some computation, and
return a value to be used as the value of the function call in the
calling expression. The return keyword is used for this.
def greet():
return "Hello"
print(greet(), "Glenn")
print(greet(), "Sally")
Hello Glenn
Hello Sally
Return Value
• A “fruitful” function is one
that produces a result (or
return value)
• The return statement ends
the function execution and
“sends back” the result of
the function
>>> def greet(lang):
... if lang == 'es':
... return 'Hola'
... elif lang == 'fr':
... return 'Bonjour'
... else:
... return 'Hello'
...
>>> print(greet('en'),'Glenn')
Hello Glenn
>>> print(greet('es'),'Sally')
Hola Sally
>>> print(greet('fr'),'Michael')
Bonjour Michael
>>>
Arguments, Parameters, and
Results
>>> big = max('Hello world')
>>> print(big)
w
def max(inp):
blah
blah
for x in inp:
blah
blah
return 'w'
'Hello world' 'w'
Argument
Parameter
Result
Multiple Parameters / Arguments
• We can define more than one
parameter in the function
definition
• We simply add more arguments
when we call the function
• We match the number and order
of arguments and parameters
def addtwo(a, b):
added = a + b
return added
x = addtwo(3, 5)
print(x)
8
To function or not to function...
• Organize your code into “paragraphs” - capture a complete
thought and “name it”
• Don’t repeat yourself - make it work once and then reuse it
• If something gets too long or complex, break it up into logical
chunks and put those chunks in functions
• Make a library of common stuff that you do over and over -
perhaps share this with your friends...
Exercise
Rewrite your pay computation with time-and-a-
half for overtime and create a function called
computepay which takes two parameters ( hours
and rate).
Enter Hours: 45
Enter Rate: 10
Pay: 475.0
475 = 40 * 10 + 5 * 15

More Related Content

Similar to Py-slides-3 easyforbeginnerspythoncourse.pptx (20)

PPTX
Decided to go to the 65 and the value of the number
harshoberoi2050
 
PDF
functions- best.pdf
MikialeTesfamariam
 
PDF
Notes5
hccit
 
PPTX
Functions_new.pptx
Yagna15
 
PPTX
Python Functions.pptx
AnuragBharti27
 
PPTX
Functions in python3
Lakshmi Sarvani Videla
 
PPTX
Python functions PYTHON FUNCTIONS1234567
AnjaneyuluKunchala1
 
PDF
3-Python Functions.pdf in simple.........
mxdsnaps
 
PPTX
Working with functions.pptx. Hb.
sabarivelan111007
 
PDF
Python Function.pdf
NehaSpillai1
 
PPT
functions modules and exceptions handlings.ppt
Rajasekhar364622
 
PDF
Function
GauravGautam224100
 
PPTX
2_3 Functions 5d.pptx2_3 Functions 5d.pptx
usha raj
 
PPTX
Python for Data Science function third module ppt.pptx
bmit1
 
PPTX
04. WORKING WITH FUNCTIONS-2 (1).pptx
Manas40552
 
PDF
Python functions
Prof. Dr. K. Adisesha
 
PPTX
UNIT-02-pythonfunctions python function using detehdjsjehhdjejdhdjdjdjddjdhdhhd
tony8553004135
 
PPTX
Functions and Modules.pptx
Ashwini Raut
 
PPTX
Functions in python, types of functions in python
SherinRappai
 
PPTX
Lecture 08.pptx
Mohammad Hassan
 
Decided to go to the 65 and the value of the number
harshoberoi2050
 
functions- best.pdf
MikialeTesfamariam
 
Notes5
hccit
 
Functions_new.pptx
Yagna15
 
Python Functions.pptx
AnuragBharti27
 
Functions in python3
Lakshmi Sarvani Videla
 
Python functions PYTHON FUNCTIONS1234567
AnjaneyuluKunchala1
 
3-Python Functions.pdf in simple.........
mxdsnaps
 
Working with functions.pptx. Hb.
sabarivelan111007
 
Python Function.pdf
NehaSpillai1
 
functions modules and exceptions handlings.ppt
Rajasekhar364622
 
2_3 Functions 5d.pptx2_3 Functions 5d.pptx
usha raj
 
Python for Data Science function third module ppt.pptx
bmit1
 
04. WORKING WITH FUNCTIONS-2 (1).pptx
Manas40552
 
Python functions
Prof. Dr. K. Adisesha
 
UNIT-02-pythonfunctions python function using detehdjsjehhdjejdhdjdjdjddjdhdhhd
tony8553004135
 
Functions and Modules.pptx
Ashwini Raut
 
Functions in python, types of functions in python
SherinRappai
 
Lecture 08.pptx
Mohammad Hassan
 

Recently uploaded (20)

PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PDF
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PDF
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
Ad

Py-slides-3 easyforbeginnerspythoncourse.pptx

  • 2. Python Functions • There are two kinds of functions in Python. - Built-in functions that are provided as part of Python - print(), input(), type(), float(), int() ... - Functions that we define ourselves and then use • We treat function names as “new” reserved words (i.e., we avoid them as variable names)
  • 3. Function Definition • In Python a function is some reusable code that takes arguments(s) as input, does some computation, and then returns a result or results • We define a function using the def reserved word • We call/invoke the function by using the function name, parentheses, and arguments in an expression
  • 5. Building our Own Functions • We create a new function using the def keyword followed by optional parameters in parentheses • We indent the body of the function • This defines the function but does not execute the body of the function def print_lyrics(): print("I'm a lumberjack, and I'm okay.") print('I sleep all night and I work all day.')
  • 6. x = 5 print('Hello') def print_lyrics(): print("I'm a lumberjack, and I'm okay.") print('I sleep all night and I work all day.') print('Yo') x = x + 2 print(x) Hello Yo 7 print("I'm a lumberjack, and I'm okay.") print('I sleep all night and I work all day.') print_lyrics():
  • 7. Definitions and Uses • Once we have defined a function, we can call (or invoke) it as many times as we like • This is the store and reuse pattern
  • 8. x = 5 print('Hello') def print_lyrics(): print("I'm a lumberjack, and I'm okay.") print('I sleep all night and I work all day.') print('Yo') print_lyrics() x = x + 2 print(x) Hello Yo I'm a lumberjack, and I'm okay. I sleep all night and I work all day. 7
  • 9. Arguments • An argument is a value we pass into the function as its input when we call the function • We use arguments so we can direct the function to do different kinds of work when we call it at different times • We put the arguments in parentheses after the name of the function big = max('Hello world') Argument
  • 10. Parameters A parameter is a variable which we use in the function definition. It is a “handle” that allows the code in the function to access the arguments for a particular function invocation. >>> def greet(lang): ... if lang == 'es': ... print('Hola') ... elif lang == 'fr': ... print('Bonjour') ... else: ... print('Hello') ... >>> greet('en') Hello >>> greet('es') Hola >>> greet('fr') Bonjour >>> Parameter Argument
  • 11. Return Values Often a function will take its arguments, do some computation, and return a value to be used as the value of the function call in the calling expression. The return keyword is used for this. def greet(): return "Hello" print(greet(), "Glenn") print(greet(), "Sally") Hello Glenn Hello Sally
  • 12. Return Value • A “fruitful” function is one that produces a result (or return value) • The return statement ends the function execution and “sends back” the result of the function >>> def greet(lang): ... if lang == 'es': ... return 'Hola' ... elif lang == 'fr': ... return 'Bonjour' ... else: ... return 'Hello' ... >>> print(greet('en'),'Glenn') Hello Glenn >>> print(greet('es'),'Sally') Hola Sally >>> print(greet('fr'),'Michael') Bonjour Michael >>>
  • 13. Arguments, Parameters, and Results >>> big = max('Hello world') >>> print(big) w def max(inp): blah blah for x in inp: blah blah return 'w' 'Hello world' 'w' Argument Parameter Result
  • 14. Multiple Parameters / Arguments • We can define more than one parameter in the function definition • We simply add more arguments when we call the function • We match the number and order of arguments and parameters def addtwo(a, b): added = a + b return added x = addtwo(3, 5) print(x) 8
  • 15. To function or not to function... • Organize your code into “paragraphs” - capture a complete thought and “name it” • Don’t repeat yourself - make it work once and then reuse it • If something gets too long or complex, break it up into logical chunks and put those chunks in functions • Make a library of common stuff that you do over and over - perhaps share this with your friends...
  • 16. Exercise Rewrite your pay computation with time-and-a- half for overtime and create a function called computepay which takes two parameters ( hours and rate). Enter Hours: 45 Enter Rate: 10 Pay: 475.0 475 = 40 * 10 + 5 * 15