abs(x) - returns a number that is the absolute value of x→
len(s) - returns the length of a sequence type (such as a string) as an int→
round(x [,digits]) - round a number to a given precision in decimal digits (default 0 digits)→
dir([object]) - returns a list of names in a module or in the current namespace→
Let's try these out! →
More Functions, More Functions, More Functions
Python boasts that it comes with batteries included:
it has a large and comprehensive standard library
most likely, if you need it, Python has it built in!
everything from audio file processing to parsing html documents
Modules
that's because you can access this functionality through modules!
these modulesprovide standardized solutions for many problems that occur in everyday programming
they also help in organizing code / the standard library (related functionality is grouped together in the same module)
What's a Module?
A module is just a file that contains Python code!
a file containing Python definitions and statements intended for use in other Python programs
the contents of a module are made available to the other program by using the import statement.
No, Really, What's a Module?
So… what does that actually mean?
there are functions (as well as other definitions) that are not automatically loaded when you run python
these functions (and other definitions, such as variables and constants like pi, classes, etc) are grouped together in files called modules
you can bring them in to your program by using the import statement
simply use the keyword import followed by the module name (with no quotes and no extension)
So… What Can These Modules Do?
math
pi - a constant that contains the value of pi→
floor(x) - returns the smallest integer less than or equal to x→
ceil(x) - returns the smallest integer greater than or equal to x→
sqrt(x) - returns the square root of x→
cos(x) - returns the cosine of x radians →
So… What Can These Modules Do (Continued)?
random
random() - return a random float that's between 0 and 1→
randint(a, b) - returns a random int that's a <= n <= b→
sys
exit([arg]) - exits from python→
version - a constant that contains the version of Python→
We Know How to Call Functions, Right?
So, what's the exact syntax for calling a function? Let's start with built-in ones, like print or str. →
the function name
open parentheses
optional arguments
close parentheses
Calling a Function is Easy
Call or use functions and other definitions in a module by:
making sure you import the module first!
no, really: IMPORT FIRST
using the module name as the prefix
a dot (.)
the function (or variable, class, etc) name
Let's Try That Again
How do I bring in the random module to call the randint function? Write a short program that prints out a random number. →
The math Module In Use
Write a quick program to print out the cosine of 2pi and the squareroot of 225. →
The random Module In Use
Write a quick program to print out two random numbers, one a floating point between 0 and 1, the other an int between 5 and 10. →
Lastly, Let's Check Out the Sys Module
BTW, what do you think gets printed?→
To Use a Module, IMPORT IT FIRST!
What do you think happens if your forget to import the file? →
To Call a Function in a Module, Use The Module's Name as a Prefix
What if you don't use the module name as the prefix before calling your function? →
So… With All of That Out of the Way Let's Simulate Rolling a Six Sided Die Twice
Use random to "roll" dice; print out the two die rolls→
Behind the Scenes
What happens when we import?
the name of the module actually corresponds to a python file
Python looks in the current working folder (as well as several other locations) for a file named after the module your importing
imagine placing the contents of that file directly into the file you're working on (at the point of the import statement)
An Example Module
In fact, we can make our own modules! We'll take a look at this later, but to illustrate how modules work, we can create two files in the same directory:
foo.py (the module that we'll import)
hello.py
An Example Module Continued
What do you think will be output when we run hello.py? →
Um… So Where Are All of Those Other Modules?
Where do sys, math, random, and other modules come from? If modules are just files, we should just be able to find them!
the current directory
or a list of specified directories called the PYTHON_PATH
these usually vary by system, but
for example, on OSX (Mountain Lion) and Python 3.3, you may be able to find modules in:
Why Do Modules Exist?
That's great, but why bother with using and/or creating modules (aside, of course, from bringing in additional built-in functionality)?
they encourage code reuse (DRY - don't repeat yourself)
they provide "namespacing" to avoid name collisions
they provide a way of organizing code
Three Modules Out of ??? / HALP!
There are many more modules to explore. Check out the official documentation for a more comprehensive list. You'll find modules like: