17
Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 4 Beginning Functions 4/5/09 Python Mini-Course: Day 1 - Lesson 4 1

Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 4 Beginning Functions 4/5/09 Python Mini-Course: Day 1 - Lesson 4 1

Embed Size (px)

Citation preview

Page 1: Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 4 Beginning Functions 4/5/09 Python Mini-Course: Day 1 - Lesson 4 1

Python Mini-CourseUniversity of Oklahoma

Department of Psychology

Day 1 – Lesson 4Beginning Functions

4/5/09Python Mini-Course: Day 1 - Lesson 41

Page 2: Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 4 Beginning Functions 4/5/09 Python Mini-Course: Day 1 - Lesson 4 1

Lesson objectives

1. State the purpose of functions and modules in Python

2. Use built-in functions3. Import modules and use imported

functions4. Create custom void functions5. Discuss the concept of variable

scope4/5/09Python Mini-Course: Day 1 - Lesson 42

Page 3: Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 4 Beginning Functions 4/5/09 Python Mini-Course: Day 1 - Lesson 4 1

Functions

FunctionA named sequence of statements that

performs a computation or actionFunctions are called by name

Most functions accept inputs (arguments)

Some functions return results (return value)

4/5/09Python Mini-Course: Day 1 - Lesson 43

Page 4: Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 4 Beginning Functions 4/5/09 Python Mini-Course: Day 1 - Lesson 4 1

Functions

We’ve already seen some functions:type()Type casting functions

int(), float(), str()

4/5/09Python Mini-Course: Day 1 - Lesson 44

Page 5: Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 4 Beginning Functions 4/5/09 Python Mini-Course: Day 1 - Lesson 4 1

Modules

ModuleA file that contains a collection of

related functionsPython has hundreds of standard

modulesThese are known as the Python Standard

Library (http://docs.python.org/library/)

You can also create and use add-in modules

4/5/09Python Mini-Course: Day 1 - Lesson 45

Page 6: Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 4 Beginning Functions 4/5/09 Python Mini-Course: Day 1 - Lesson 4 1

Using import

To use a module, you first have to import it into your namespace

To import the entire moduleimport module_name

To import specific functionsfrom module_name import function_name

4/5/09Python Mini-Course: Day 1 - Lesson 46

Page 7: Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 4 Beginning Functions 4/5/09 Python Mini-Course: Day 1 - Lesson 4 1

The math module

The standard math module includes:Number-theoretic and representation

functionsPower and logarithmic functionsTrigonometric functionsHyperbolic functionsAngular conversionConstants

4/5/09Python Mini-Course: Day 1 - Lesson 47

Page 8: Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 4 Beginning Functions 4/5/09 Python Mini-Course: Day 1 - Lesson 4 1

Using the math module

import mathdegrees = 45radians = degrees / 360.0 \ * 2 * math.piprint math.sin(radians)

x = math.sin(degrees / 360.0 \ * 2 * math.pi)

4/5/09Python Mini-Course: Day 1 - Lesson 48

Page 9: Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 4 Beginning Functions 4/5/09 Python Mini-Course: Day 1 - Lesson 4 1

Dot notation

Why did we use math.sin() instead of just sin()?

Try this: print sin(radians)Dot notation allows the Python interpreter to organize and divide the namespace

4/5/09Python Mini-Course: Day 1 - Lesson 49

Page 10: Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 4 Beginning Functions 4/5/09 Python Mini-Course: Day 1 - Lesson 4 1

More on Importing

from math import *print sin(2)

Be careful when using the import * command. It can easily lead to namespace conflicts.

4/5/09Python Mini-Course: Day 1 - Lesson 410

Page 11: Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 4 Beginning Functions 4/5/09 Python Mini-Course: Day 1 - Lesson 4 1

Creating your own functions

You have to define the functionExample:def print_lyrics():

print "I'm a lumberjack, and I'm okay."

print "I sleep all night and I work all day."

4/5/09Python Mini-Course: Day 1 - Lesson 411

Page 12: Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 4 Beginning Functions 4/5/09 Python Mini-Course: Day 1 - Lesson 4 1

Composing functions

def repeat_lyrics():print_lyrics()print_lyrics()

repeat_lyrics()

4/5/09Python Mini-Course: Day 1 - Lesson 412

Page 13: Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 4 Beginning Functions 4/5/09 Python Mini-Course: Day 1 - Lesson 4 1

Functions with arguments

def print_twice(in_text):print in_textprint in_text

print_twice(‘Spam’)print_twice(‘Spam’*4)

4/5/09Python Mini-Course: Day 1 - Lesson 413

Page 14: Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 4 Beginning Functions 4/5/09 Python Mini-Course: Day 1 - Lesson 4 1

Variable scope

ScopeThe enclosing context where values and expressions are associated (partition in namespace)

Variables inside functions are local

4/5/09Python Mini-Course: Day 1 - Lesson 414

Page 15: Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 4 Beginning Functions 4/5/09 Python Mini-Course: Day 1 - Lesson 4 1

Variable scope

def cat_string(part1, part2):cat = part1 + part2print cat

cat_string(‘This ‘, ‘works’)print cat

4/5/09Python Mini-Course: Day 1 - Lesson 415

Page 16: Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 4 Beginning Functions 4/5/09 Python Mini-Course: Day 1 - Lesson 4 1

Documentation

You can document functions in the code immediately after the function header

Example: func_doc.py

4/5/09Python Mini-Course: Day 1 - Lesson 416

Page 17: Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 4 Beginning Functions 4/5/09 Python Mini-Course: Day 1 - Lesson 4 1

Before next time

Practice creating and using your own functions (try the exercises on pp 26-28)

Practice using the math module (see http://docs.python.org/library/math.html for documentation)

4/5/09Python Mini-Course: Day 1 - Lesson 417