This presentation is a part of the COP2271C college level course taught at the Florida Polytechnic University located in Lakeland Florida. The purpose of this course is to introduce Freshmen students to both the process of software development and to the Python language.
The course is one semester in length and meets for 2 hours twice a week. The Instructor is Dr. Jim Anderson.
A video of Dr. Anderson using these slides is available on YouTube at:
https://youtu.be/lse2J_aIGuo
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
An Introduction To Python - Functions, Part 1
1. An Introduction To Software
Development Using Python
Spring Semester, 2014
Class #17:
Functions, Part 1
2. The Worst Homework Assignment
EVER!
• Using the 15 sets of patient data that you’ve
been given in Homework #2, sort each one of
them in ascending order based on each of the
11 fields in a record.
Image Credit: luluscottage.blogspot.com
3. 2nd Worst Homework Assignment
EVER!
• Now, using the 15 sets of patient data that
you’ve been given in Homework #2, sort each
one of them in DECENDING order based on
each of the 11 fields in a record.
Image Credit: www.clipartpanda.com343 × 350
4. We Have A Problem
• We are running a VERY successful ice cream store.
• Creating an ice cream cone is a 3-step process:
1. Select flavor of ice cream
2. Construct cone
3. Add toppings
• As the store owner we have two problems:
consistent quality, minimizing training time.
Image Credit: www.etsy.com
5. I Got 99 Problems But
Quality Ain't One
• Select flavor of ice cream
– Check inventory for available flavors
– Separate ice cream from yogurt
– Get user selection
• Construct cone
– Select cone type: waffle, sugar, regular
– Determine # of scoops: 1, 2, 3, crazy
• Add toppings
– Check inventory for available flavors
– Separate into regular and premium toppings
– Get user selection
Image Credit: modresdes.com
6. Training Is Going To Be A Pain
1. Check inventory for available flavors
2. Separate ice cream from yogurt
3. Get user selection
4. Select cone type: waffle, sugar, regular
5. Determine # of scoops: 1, 2, 3, crazy
6. Check inventory for available flavors
7. Separate into regular and premium toppings
8. Get user selection
Image Credit: www.gograph.com
7. What Is Our Python Code Going To
Look Like?
# drive through order
Check inventory for available flavors
Separate ice cream from yogurt
Get user selection
Select cone type: waffle, sugar, regular
Determine # of scoops: 1, 2, 3, crazy
Check inventory for available flavors
Separate into regular and premium toppings
Get user selection
# walk-up order
Check inventory for available flavors
Separate ice cream from yogurt
Get user selection
Select cone type: waffle, sugar, regular
Determine # of scoops: 1, 2, 3, crazy
Check inventory for available flavors
Separate into regular and premium toppings
Get user selection
# online order
Check inventory for available flavors
Separate ice cream from yogurt
Get user selection
Select cone type: waffle, sugar, regular
Determine # of scoops: 1, 2, 3, crazy
Check inventory for available flavors
Separate into regular and premium toppings
Get user selection
# phone-in order
Check inventory for available flavors
Separate ice cream from yogurt
Get user selection
Select cone type: waffle, sugar, regular
Determine # of scoops: 1, 2, 3, crazy
Check inventory for available flavors
Separate into regular and premium toppings
Get user selection
Image Credit: pixshark.com
8. There Has To Be A Better Way To
Make Ice Cream Cones!
• In Python, a function packages a computation consisting of multiple steps
into a form that can be easily understood and reused.
– e.g. Select flavor of ice cream, Construct cone, Add toppings
• A function is a sequence of instructions with a name.
• You call a function in order to execute its instructions. For example,
consider the following program statement:
price = round(6.8275, 2) # Sets result to 6.83
Image Credit: www.clipartpanda.com
9. What Happens When You Call A
Function?
price = round(6.8275, 2)
“Arguments”
Note: Multiple arguments
can be passed to a function.
Only one value can be returned.
10. Benefits Of Using Functions
• The first reason is reusability. Once a function is
defined, it can be used over and over and over again.
You can invoke the same function many times in your
program, which saves you work.
• A single function can be used in several different
programs. When you need to write a new program,
you can go back to your old programs, find the
functions you need, and reuse those functions in
your new program.
• The second reason is abstraction. If you just want to
use the function in your program, you don't have to
know how it works inside! You don't have to
understand anything about what goes on inside the
function.
11. How To Create A Function
• When writing this function, you need to
– Pick a name for the function (selectFlavor).
– Define a variable for each argument (yogurt).
• These variables are called the parameter variables.
• Put all this information together along with the def reserved
word to form the first line of the function’s definition:
def selectFlavor(yogurt) :
• This line is called the header of the function.
Image Credit: imgarcade.com
12. Create The Body Of The Function
• The body contains the statements that are
executed when the function is called.
listOfFlavors = checkInventory()
while flavor in listOfFlavors
print(listOfFlavors[flavor])
if (yogurt) :
selection = input(“Please enter the flavor of yogurt you would like:”)
else :
selection = input(“Please enter the flavor of ice cream you would like:”)
Image Credit: www.pinterest.com
13. Send The Result Back
• In order to return the result of the function,
use the return statement:
return selection
Image Credit: www.clipartpanda.com
14. Final Form Of Our Function
def selectFlavor(yogurt) :
listOfFlavors = checkInventory()
while flavor in listOfFlavors
print(listOfFlavors[flavor])
if (yogurt == 1) :
selection = input(“Please enter the flavor of yogurt you would like:”)
else :
selection = input(“Please enter the flavor of ice cream you would like:”)
return selection
Note: A function is a compound statement, which requires the statements
in the body to be indented to the same level.
Image Credit: www.clipartpanda.com
16. Order Matters!
• Python is an interpreted language. This means
that it needs to “see” a function before you
call it…
myChoice = selectFlavor(0)
myChoice = selectFlavor(0)
Image Credit: www.dreamstime.com
17. Order Definition Weirdness
• It turns out that you CAN call a function
before it is defined – from another function.
def a() :
c = b(4)
return (c)
def b(d) :
d =10
return(d)
Image Credit: www.acclaimclipart.com
18. Functions: Going All In
• It is possible to create a Python program that
is all functions…
def main():
temp = a
def a() :
c = b(4)
return (c)
def b(d) :
d =10
return(d)
# start program
main()
19. Revisiting The Worst Homework
Assignment EVER!
• Using the 15 sets of patient data that you’ve
been given in Homework #2, sort each one of
them in ascending order based on each of the
11 fields in a record…
• … using functions.
Image Credit: www.spiritofthesouth.net
20. What’s In Your Python Toolbox?
print() math strings I/O IF/Else elif While For
Lists And / Or Functions
21. What We Covered Today
1. Functions, Part 1
Image Credit: http://www.tswdj.com/blog/2011/05/17/the-grooms-checklist/
22. What We’ll Be Covering Next Time
1. Functions, Part 2
Image Credit: http://merchantblog.thefind.com/2011/01/merchant-newsletter/resolve-to-take-advantage-of-these-5-e-commerce-trends/attachment/crystal-ball-fullsize/
Editor's Notes
New name for the class
I know what this means
Technical professionals are who get hired
This means much more than just having a narrow vertical knowledge of some subject area.
It means that you know how to produce an outcome that I value.
I’m willing to pay you to do that.