This is the P2PU Archive. If you want the current site, go to www.p2pu.org!

Python Programming 101

Functions

Brylie Oxley's picture
Wed, 2011-05-18 04:23

Functions allow us to reuse bits of code. For example, say that we are calculating tax on transactions. We can wrap up our code in a functional unit and give it a name:

def calculate_tax(price, tax):
    sales_tax = price * tax
    total = price + sales_ tax
    return total

From then on we can just call that name and pass it some information (called parameters):

calculate_tax(53.79, 0.075)

This will save us time as well as make our code easier to review. Lets take a look at some more in-depth learning resources.

Learning Resources

Python for Informatics

Dive Into Python3: Declaring Functions

Practice

Post chapter excercises and questions below.

Q:  What is an example of a machine or process that takes external input and returns a modified output?

Q: How many tasks do you think that an individual function should generally perform?

Comments

Q: What is an example of a

Alfonso Gilberto Urroz-Aguirre's picture
Alfonso Gilberto ...
Wed, 2011-05-18 06:00

Q: What is an example of a machine or process that takes external input and returns a modified output?
A function, takes external inputs and returns a modified output.

Q: How many tasks do you think that an individual function should generally perform?
An individual function should perform a single task.

Alfonso

Q: What is an example of a

Pein Junior's picture
Pein Junior
Thu, 2011-05-19 03:20

Q: What is an example of a machine or process that takes external input and returns a modified output?

printer
fax machine

Q: How many tasks do you think that an individual function should generally perform?

function only perform single tasks but a function can be used multiple times.

ex 4.4

#working with functions()

def computepay(time, rate):

if time > 40:
over = (int(hours) - 40) * 1.5
time = 40 + float(over)

pay = float(time) * float(rate)

print pay

hours = raw_input("Enter hours worked:")
try:
hours = int(hours)

except:
print 'error, enter a number\n'

rate = raw_input("Enter rate at which worked:")
try:
rate = int(rate)

except:
print 'error, enter a number\n'

computepay(hours, rate)

ex 4.5

#Student Grade

score = raw_input('Enter student score(0.0-1.0): ')

try:
score = float(score)

if score < 0.6:
print 'F'
elif score <= 0.69:
print 'D'
elif score <= 0.79:
print 'C'
elif score <= 0.89:
print 'B'
elif score <= 1.0:
print 'A'

else:
print 'Bad Score'

except:
print 'error Bad Score'

Q: What is an example of a

Mark Hutchison's picture
Mark Hutchison
Sun, 2011-05-29 05:21

Q: What is an example of a machine or process that takes external input and returns a modified output?

A human being: eats input and, erm, creates modified output

Q: How many tasks do you think that an individual function should generally perform?
Any number internal to the function, but one result.

chapter 4 exercises:
https://github.com/brotherhutch/python101/tree/master/chapter4

Q: What is an example of a

Luc Chase's picture
Luc Chase
Sun, 2011-05-29 09:31

Q: What is an example of a machine or process that takes external input and returns a modified output?

Function(input): Go-to-Gym(keen but un-fit Gym Member)
Process: Excercise Gym-Member almost to failure
Output: Slightly Fitter Gym-Member

Q: How many tasks do you think that an individual function should generally perform?
In general one per individual function. But a Function may have any number of related Functions within it, each performing a single related task. Very important that the function only deals with closely related tasks.

Chapter 4 Exercises:

Jimmy Moore's picture
Jimmy Moore
Thu, 2011-06-02 04:09

Chapter 4 Exercises: https://github.com/jwmnatl/jwmnatl.github.com/blob/master/python101/chap...

Q: What is an example of a machine or process that takes external input and returns a modified output?

A: I'll go 18th century and say the cotton gin. Input cotton, returns fibers

Q: How many tasks do you think that an individual function should generally perform?

A: Can perform several tasks within the function but just one task can result in output. So generally speaking, one.

Q: What is an example of a

Johan Mares's picture
Johan Mares
Sun, 2011-06-05 11:30

Q: What is an example of a machine or process that takes external input and returns a modified output?
TV remote control

Q: How many tasks do you think that an individual function should generally perform?
One.

Exercises chapter 4: http://www.pastie.org/2021666