Functions: Python For Informatics: Exploring Information
Functions: Python For Informatics: Exploring Information
Chapter 4
Result
>>> big = max('Hello world')
>>> print big
w
>>> tiny = min('Hello world')
>>> print tiny
>>>
Max Function
A function is some
>>> big = max('Hello world') stored code that we
>>> print big
use. A function takes
w
some input and
produces an output.
'w'
'Hello world' max() (a
(a string) function string)
• 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.'
print "I'm a lumberjack, and I'm okay."
print_lyrics(): 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."
Hello
print 'I sleep all night and I work all day.' Yo
print 'Yo'
7
x = x + 2
print x
Definitions and Uses
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() Hello
x = x + 2 Yo
print x 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
def greet():
return "Hello"
Enter Hours: 45
Enter Rate: 10
Pay: 475.0
475 = 40 * 10 + 5 *
15
Summary
• Functions • Arguments
• Built-In Functions • Results (fruitful functions)
> Type conversion (int, • Void (non-fruitful) functions
float)
• Why use functions?
> String conversions
• Parameters
Acknowledgements / Contributions
These slides are Copyright 2010- Charles R. Severance
...
(www.dr-chuck.com) of the University of Michigan School of
Information and open.umich.edu and made available under a
Creative Commons Attribution 4.0 License. Please maintain this
last slide in all copies of the document to comply with the
attribution requirements of the license. If you make a change,
feel free to add your name and organization to the list of
contributors on this page as you republish the materials.