Loading in 5 sec....

Day 1 – Lesson 4 Beginning Functions PowerPoint Presentation

Day 1 – Lesson 4 Beginning Functions

- 120 Views
- Uploaded on
- Presentation posted in: General

Day 1 – Lesson 4 Beginning Functions

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Day 1 – Lesson 4Beginning Functions

Python Mini-Course

University of Oklahoma

Department of Psychology

Python Mini-Course: Day 1 - Lesson 4

- State the purpose of functions and modules in Python
- Use built-in functions
- Import modules and use imported functions
- Create custom void functions
- Discuss the concept of variable scope

Python Mini-Course: Day 1 - Lesson 4

- Function
- A named sequence of statements that performs a computation or action

- Functions are called by name
- Most functions accept inputs (arguments)
- Some functions return results (return value)

Python Mini-Course: Day 1 - Lesson 4

- We’ve already seen some functions:
- type()
- Type casting functions
- int(), float(), str()

Python Mini-Course: Day 1 - Lesson 4

- Module
- A file that contains a collection of related functions

- Python has hundreds of standard modules
- These are known as the Python Standard Library (http://docs.python.org/library/)

- You can also create and use add-in modules

Python Mini-Course: Day 1 - Lesson 4

- 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

Python Mini-Course: Day 1 - Lesson 4

- The standard math module includes:
- Number-theoretic and representation functions
- Power and logarithmic functions
- Trigonometric functions
- Hyperbolic functions
- Angular conversion
- Constants

Python Mini-Course: Day 1 - Lesson 4

import math

degrees = 45

radians = degrees / 360.0 \

* 2 * math.pi

print math.sin(radians)

x = math.sin(degrees / 360.0 \

* 2 * math.pi)

Python Mini-Course: Day 1 - Lesson 4

- 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

Python Mini-Course: Day 1 - Lesson 4

from math import *

print sin(2)

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

Python Mini-Course: Day 1 - Lesson 4

- You have to define the function
- Example:
def print_lyrics():

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

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

- Example:

Python Mini-Course: Day 1 - Lesson 4

def repeat_lyrics():

print_lyrics()

print_lyrics()

repeat_lyrics()

Python Mini-Course: Day 1 - Lesson 4

def print_twice(in_text):

print in_text

print in_text

print_twice(‘Spam’)

print_twice(‘Spam’*4)

Python Mini-Course: Day 1 - Lesson 4

- Scope
- The enclosing context where values and expressions are associated (partition in namespace)

- Variables inside functions are local

Python Mini-Course: Day 1 - Lesson 4

def cat_string(part1, part2):

cat = part1 + part2

print cat

cat_string(‘This ‘, ‘works’)

print cat

Python Mini-Course: Day 1 - Lesson 4

- You can document functions in the code immediately after the function header
- Example: func_doc.py

Python Mini-Course: Day 1 - Lesson 4

- 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)

Python Mini-Course: Day 1 - Lesson 4