Built-In Modules

Built-In Functions

So… again, we learned a few built-in functions. What are some of the functions that we know? → These are all available by default:

Even More Built-In Functions!

We've only scratched the surface. There are even more built-in functions! There's a list in the official python documentation: http://docs.python.org/py3k/library/functions.html

For example:

Let's try these out! →

More Functions, More Functions, More Functions

Python boasts that it comes with batteries included:

Modules

What's a Module?

A module is just a file that contains Python code!

No, Really, What's a Module?

So… what does that actually mean?

import math
import random
import sys

So… What Can These Modules Do?

So… What Can These Modules Do (Continued)?

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.

print("foo")
str(5)

Calling a Function is Easy

Call or use functions and other definitions in a module by:

import math # import first!
#  remember - the syntax is: module_name.function_name()
math.pi
math.sqrt(25)

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. →

import random 
num = random.randint(0, 10)
print(num)

The math Module In Use

Write a quick program to print out the cosine of 2pi and the squareroot of 225. →

import math
a = math.cos(2 * math.pi)
print(a)
b = math.sqrt(225)
print(b)

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. →

import random
a = random.random()
print(a)
b = random.randint(5,10)
print(b)

Lastly, Let's Check Out the Sys Module

import sys
print(sys.version)
#  note that the following line causes what looks like error text to appear...
sys.exit() 
print("Is this really the end????")
print("Even before this!")

BTW, what do you think gets printed?→

3.2.3 (v3.2.3:3d0686d90f55, Apr 10 2012, 11:25:50) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]

To Use a Module, IMPORT IT FIRST!

What do you think happens if your forget to import the file? →

>>> math.sqrt(4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'math' is not defined

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? →

>>> import math
>>> sqrt(4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'sqrt' is not defined
>>> 

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

d1 = random.randint(1,6)
print(d1)
d2 = random.randint(1,6)
print(d2)

Behind the Scenes

What happens when we import?

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
print("called from inside the foo module")
def greet():
	print("hi")

#  hello.py
import foo
print("hello")
foo.greet()

An Example Module Continued

What do you think will be output when we run hello.py? →

inside my_awesome_module
hello
42

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!

#  for me
/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/

#  for you...
/System/Library/Frameworks/Python.framework/Versions/ ...

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

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:

HALP!!!

To find help on these modules:

>>> import math
>>> help(math)
>>> dir(math)

How About a Practical Application, PLZ?

Rewrite our guess number game so that it uses a random number instead of a hardcoded one. It should display the correct answer after you guess.→

#  Run 1:
Guess a number between 1 and 10!
> 5
Nope, I was thinking of 10.

#  Run 2:
Guess a number between 1 and 10!
> 8
You guessed right; I was thinking of 8.

Let's Review