Download presentation

Presentation is loading. Please wait.

Published byGretchen Spratling Modified over 3 years ago

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

2
Lesson objectives 1. State the purpose of functions and modules in Python 2. Use built-in functions 3. Import modules and use imported functions 4. Create custom void functions 5. Discuss the concept of variable scope 4/5/09 Python Mini-Course: Day 1 - Lesson 4 2

3
Functions 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) 4/5/09 Python Mini-Course: Day 1 - Lesson 4 3

4
Functions We’ve already seen some functions: type() Type casting functions int(), float(), str() 4/5/09 Python Mini-Course: Day 1 - Lesson 4 4

5
Modules 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/)http://docs.python.org/library/ You can also create and use add-in modules 4/5/09 Python Mini-Course: Day 1 - Lesson 4 5

6
Using import To use a module, you first have to import it into your namespace To import the entire module import module_name To import specific functions from module_name import function_name 4/5/09 Python Mini-Course: Day 1 - Lesson 4 6

7
The math module The standard math module includes: Number-theoretic and representation functions Power and logarithmic functions Trigonometric functions Hyperbolic functions Angular conversion Constants 4/5/09 Python Mini-Course: Day 1 - Lesson 4 7

8
Using the math module import math degrees = 45 radians = degrees / 360.0 \ * 2 * math.pi print math.sin(radians) x = math.sin(degrees / 360.0 \ * 2 * math.pi) 4/5/09 Python Mini-Course: Day 1 - Lesson 4 8

9
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/09 Python Mini-Course: Day 1 - Lesson 4 9

10
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/09 Python Mini-Course: Day 1 - Lesson 4 10

11
Creating your own functions 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." 4/5/09 Python Mini-Course: Day 1 - Lesson 4 11

12
Composing functions def repeat_lyrics(): print_lyrics() repeat_lyrics() 4/5/09 Python Mini-Course: Day 1 - Lesson 4 12

13
Functions with arguments def print_twice(in_text): print in_text print_twice(‘Spam’) print_twice(‘Spam’*4) 4/5/09 Python Mini-Course: Day 1 - Lesson 4 13

14
Variable scope Scope The enclosing context where values and expressions are associated (partition in namespace) Variables inside functions are local 4/5/09 Python Mini-Course: Day 1 - Lesson 4 14

15
Variable scope def cat_string(part1, part2): cat = part1 + part2 print cat cat_string(‘This ‘, ‘works’) print cat 4/5/09 Python Mini-Course: Day 1 - Lesson 4 15

16
Documentation You can document functions in the code immediately after the function header Example: func_doc.py 4/5/09 Python Mini-Course: Day 1 - Lesson 4 16

17
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)http://docs.python.org/library/math.html 4/5/09 Python Mini-Course: Day 1 - Lesson 4 17

Similar presentations

Presentation is loading. Please wait....

OK

11 = This is the fact family. You say: 8+3=11 and 3+8=11

11 = This is the fact family. You say: 8+3=11 and 3+8=11

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Technical seminar ppt on recent interactive voice response system Ppt on csa of cylinder Ppt on metallic and nonmetallic minerals Ppt on porter's five forces airline industry Ppt on combination of resistors Ppt on autonomous car computer Free ppt on smart note taker documentation Word to ppt online converter free 100% Ppt on healthy diet india Electroluminescent display ppt on tv