0% found this document useful (0 votes)
9 views18 pages

Python Functions

Uploaded by

kpakoup
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views18 pages

Python Functions

Uploaded by

kpakoup
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

SKILLPILLS

Skill Pill: Programming


with Python
Functions

Peter Bratby
Functions SKILLPILLS

1. What is a function?
2. Defining and calling functions
3. Importing modules
4. Local variables
5. Function composition
6. Stack Traces
7. Exercises

2
What is a function? SKILLPILLS

• A self-contained reusable piece of code,


callable by name
• May have parameter(s) and return value(s)

3
Example SKILLPILLS

A simple function

def sayhello(name):
print("Hello, " + name)

sayhello("Pete")

4
Defining and calling a function SKILLPILLS

function name comma-separated


list of parameters colon

def dostuff(x, y):


function body z = x + y optional return
(indented) print("Hello") statement
return z

answer = dostuff(1, 2)

comma-separated
function list of arguments
name
5
Execution order: be careful! SKILLPILLS

!!
answer = dostuff(1, 2)

def dostuff(x, y):


z = x + y
print("Hello")
return z

Define functions at top of file

6
Example: cylinder SKILLPILLS

The volume V of a cylinder of height h and


radius r is given by:

Write a function which calculates the volume of


a cylinder of height h and radius r.

def volume(r, h):


vol = 3.14 * r * r * h
return vol

7
Exercise 1 SKILLPILLS

In free fall, the distance d an object travels in t


seconds is given by:

where g = 9.8 m/s2 is acceleration due to gravity.


a. Write a function which takes a single
parameter t and returns the distance travelled
by an object in free fall. How far does an object
travel in 5 seconds?
b. Change the function so that is takes two
arguments, t and g. How far would that same
object fall on Mars, where g = 3.8 m/s2?
8
Importing modules SKILLPILLS

cylinder.py

def volume(r, h):


vol = 3.14 * r * r * h
return vol

python console
>>> import cylinder
>>> cylinder.volume(5,10)
785.0
>>> import cylinder as c
>>> c.volume(5,10)
785.0

9
Multiple return statements SKILLPILLS

Write a function oddoreven(x) which returns


"odd" if x is odd or "even" if it is even.

def oddoreven(x):
if x % 2 == 0:
return "even"
else:
return "odd"

10
Exercise 2 SKILLPILLS

a. Write a function maximum(x, y) which


returns the larger of two numbers.
b. Import and run it from the python console.

11
Local variables SKILLPILLS

def add3(x): z is a local variable


z = x + 3 which exists only
return z inside the function

x = 2
print(add3(x))
print(z) error! z does not exist here

local variables: exist only inside the function


where they are defined

12
Local variables SKILLPILLS

def add3(x):
x = x + 3
return x

x = 2 output:
print(add3(x)) 5
?
print(x) ?
2

Functions usually do not change the value of


their arguments

13
Function composition SKILLPILLS

def printrow(n): prints a row of


print('*' * n) n stars

def printblock(m, n):


for i in range(m): prints an m by n
printrow(n) block of stars

printblock(2, 3)
output:
***
***

14
Stack trace SKILLPILLS

printblock(3, 0.5)

Traceback (most recent call last):


File "/Users/peter/PycharmProjects/untitled/test2.py", line 8, in <module>
printblock(3,.5)
File "/Users/peter/PycharmProjects/untitled/test2.py", line 6, in printblock
printrow(n) file name line number function name
File "/Users/peter/PycharmProjects/untitled/test2.py", line 2, in printrow
print('*' * n)
TypeError: can't multiply sequence by non-int of type 'float'
error message

15
Exercise 3 SKILLPILLS

a. Write function 'hist' which takes a list of


integers, and prints a histogram:
output:
l = [1,4,5]
*
hist(l)
****
*****

b. Add a second parameter to your function so


that the character can be chosen:
output:
l = [1,4,5]
hist(l, 'Q') Q
QQQQ
QQQQQ

16
Exercise 4 SKILLPILLS

Write a function which returns the first even


number in a list.

l = [3, 4, 6, 2, 7]
x = firsteven(l) output:
print(x) 4

17
Exercise 5 SKILLPILLS

a. Write a function which returns the maximum of two numbers:


output:
x = max2(23, 14)
print(x) 23

b. Write a function which returns the maximum of three numbers:


x = max3(23, 14, 50) output:
print(x) 50
c. Write a function which returns the maximum of a list of numbers:
x = maxlist([1,5,6,2,3]) output:
print(x) 6
d. Write a function which takes three lists of integers, and returns
a new list containing the maximum number at each position:
x = max3list([23, 14, 3], [1, 5, 7], [5,
100, 5]) output:
print(x) [23, 100, 7]

18

You might also like