Quick Reference Pages
- Operators
- Built-in Functions
- Common Methods
- Built-in Modules
- Turtle graphics
- Music synthesis
- Testing
Built-in Modules Reference
Besides the turtle
/turtleBeads
and wavesynth
modules that we've provided for working with graphics and audio, there
are a huge array of built-in modules in Python, and there are even more
available online. In this reference page we only cover a few functions
from a couple of built-in modules that we'll use in this class. You can
think of these built-in modules as ways to extend the built-in
functions that are available.
To use functions or values from a built-in module you will need to import that module (although because it's a built-in module, you won't have to worry about where the code file for that module is). The simplest form is like this:
import math
After that import, you can use functions or values from the math
module
by writing math.
before their name. If you'd like to use those
functions or variables directly, you can instead import what you need
using from
, like this:
from math import ceil, pi
You can list whatever functions or values you want to import, separated
by commas, and each of those will become available in the current file,
using just its name (e.g, pi
instead of math.pi
). There's one more way to import things:
from math import *
The *
here stands for "everything" and it makes all of the functions
and variables from that module available in the current file without the
need to write math.
every time. Why don't we just always use this
version? Sometimes it's nice to have your code show where each value is
coming from, so that someone reading it doesn't get confused (e.g., did
the 'ceil
' function come from a module, or was it defined here?). This
gets especially important if you end up importing multiple modules, since
there would be no way for someone to look up where a function came from
without looking through each imported module to find it. So we generally
stick to the first form of import except in cases where we'll be using a
lot of different names from a module very frequently, like with turtle
,
turtleBeads
, or wavesynth
.
The table below includes very brief reference explanations for a few
functions and/or values from several built-in modules that we'll use in
this class, along with tiny examples of how they can be used correctly
(feel free to paste those examples into your Python shell to see what the
results are and double-check your understanding). This is meant as a
quick reference guide, not a thorough explanation of all of the details.
Remember you can always use the help
function to get more
information about a function or module, and you can also use
dir
to list the contents of a module.
Reference
math | Function | Effect | Example(s) |
---|---|---|
ceil
|
Returns the smallest integer that's greater than or equal to the number you give it. Whereas round rounds to the nearest integer and int chops off the decimal part, ceil always rounds up.
|
four = math.ceil(3.01)
|
floor
|
Works like ceil but rounds down (towards negative infinity) instead of up. Note that for positive numbers, int does the same thing, but for negative numbers, they're different.
|
four = math.floor(4.99)
|
pi
|
The transcendental number describing the ratio between the circumference of a circle and its diameter. This isn't a function; it's just a number. Useful for specifying angles in radians, since 2π radians is 360 degrees. |
radians = 45 * math.pi / 180
|
random | Function | Effect | Example(s) |
seed
|
Sets the 'seed value' for the random number generator. The sequence of random numbers produced will always be the same when starting from the same seed. If you never call this function, a default seed will be in place based on a variety of factors including when your program is run, so you should get different values each time you run the program. But if you do call seed , you should get the same sequence of random results every time. Very useful for testing random programs. Note that each call to a random-number or random-choice function in the random module advances the random sequence, so for two programs to stay in-sync after setting up the same seed, they must make exactly identical calls to random functions. This function doesn't return anything.
|
seed(128912)
|
randint
|
Accepts a lower and an upper limit (both integers) and returns a pseudo-random integer between them (inclusive on both ends). The exact number returned each time depends on the seed value and on how many other random functions have been used since then, so it's effectively random.
|
reps = random.randint(4, 7)
|
random
|
Returns a pseudo-random floating-point number between 0 (inclusive) and 1 (exclusive). If you want a different range, you can always multiply the result and/or add to it. Like randint , the exact result depends on the seed value and how many other random calls have happened since the seed was established.
|
angle = 2 * math.pi * random.random()
|
choice
|
Given a sequence of items, returns one of them at random (depending on the seed value and how many other random functions have been used). If multiple copies of a value are included, that value will be more likely to be selected, since the function doesn't pay attention to the values when picking one.
|
color = random.choice(['blue', 'green', 'pink', 'pink'])
|