Using Modules
Today's Goal Introduce important concepts: Functions Classes Objects Methods Modules
Python Standard Library Python consists of two parts: Language Standard Library The Python Standard Library contains code you can use in your programs to do things like: Compute mathematical functions Perform network communication Access files Draw with turtles
Standard Library Organization Code in the Standard Library is organized into Modules A module is a collection of Variables Functions Classes Python Standard Library math pi e sin() cos() sqrt() random randrange() turtle class Turtle class Screen
Functions A function is a named list of statements that performs a task. You've used functions like: print() randrange() [in the random module] The library contains many functions. Today, you'll start learning to write your own. def add(x, y): sum = x + y return sum
Classes and Data Types Python has a few built-in data types. Can you recall some? These data types are defined by classes in the Python Standard Library. A class is Python's mechanism for defining data types. The classes in the Standard Library define data types that you can use in your programs.
Standard Library Documentation docs.python.org describes contents of Standard Library Access the correct version Navigate to Library Reference We'll explore it today.
Using Modules Use the import statement to make the contents of a module available to your program. After importing a module, use its functions and variables by prefixing them with the name of the module. import math radius = 3.5 area = math.pi * (radius ** 2) sqrtOf4 = math.sqrt(4)
Functions
Using Functions To use a function, you write a statement like this: print("Hello") or this: x = math.sqrt(3) We call this a function invocation or function call To execute this statement, the Python interpreter Locates the definition of the function Executes the statements in the function
Using Functions A function is like a miniature program: It needs input to operate on It performs some calculation using the input It produces a result
Function Input and Output Functions usually get their information using parameters supplied by your program A parameter is an expression used in a function call Parameters are also called arguments Functions often return a result that you store in a variable use as a parameter to another function Example: round() is a built-in function that rounds a float to the nearest int y = 3.4 x = round(y + .3) output stored in x input parameter
Function Evaluation To execute x = round(y + .3), Python Evaluates parameter expression y + .3 Invokes round() function, passing in value 3.7 round() executes, producing result (4) Stores the result in x y + .3 3.7 round() 4 x
Aside: print() is an atypical function It sends its output to the screen (most functions don't) It does not return a value to be used in an expression
Built-in Functions The Python Standard Library contains several "built-in" functions You don't have to import a module to use these:
Let's Practice Meet the len() function: Suppose we have a variable: name = "Fred" Write a statement to invoke the len() function to determine the length of name
Let's Survey Some Modules math module os module Variables pi e Functions fabs() sqrt() sin() Activity: Survey Library Documentation Variables name Functions getlogin() getcwd()
Function Documentation Function documentation specifies Required and optional parameters Return value
Caution About Googling Python has been around a long time. Many Internet pages present techniques for Python 2. We're using Python 3. Some things that work for Python 2 don't work in Python 3.
Classes and Objects
Classes, continued Data values in Python are called objects. The makeup and behavior of objects are defined by classes. We say that an object is an instance of a class. Each object can perform operations and methods defined by its class. A class defines attributes - one or more data values that each object contains methods - functions that the objects perform
Classes in Turtle Module The turtle module contains two classes: Turtle and Screen. Turtle attributes: Location Heading Shape Whether pen is up or down Pen color Turtle methods: You know these... Screen attributes: State of each pixel on the screen Screen methods: clear() screensize()
Using the Turtle Module import turtle # makes contents of turtle module available wn = turtle.Screen() # creates instance of Screen class in turtle module wn.title("Hello") # sets window title of Screen wn alex = turtle.Turtle() # creates instance of Turtle class in turtle module alex.forward(150) # invokes forward() method on alex instance
Creating Objects Create some objects, like numbers and strings, by writing literal values x = 10 s = "Hello" Create other objects by writing module-name.class-name() wn = turtle.Screen() alex = turtle.Turtle()
Using Objects Use objects by passing them to functions: y = math.fabs(x) size = len(s) And invoking their methods: wn.title("Hello") alex.forward(10) Activity: Explore using the Turtle methods in the Standard Library x = 10 s = "Hello" wn = turtle.Screen() alex = turtle.Turtle()
Instances and State The state of an object is the combined values of its attributes. Each object of a given class has its own distinct state. alex = turtle.Turtle() alex.forward(150) gerta = turtle.Turtle() gerta.forward(10)