This page includes reference material with sections that cover all of the operators, functions, and methods you'll need in this class, as well as sections specific to the turtle module, and a section on other built-in modules we'll use. You can think of it like a pocket dictionary for the parts of the Python programming language that we'll cover in this class. The search function in your browser can be very helpful finding the term you're looking for. Terms also link to their location in the official Python documentation.
Right from the start of the class, we will be dealing with operators in Python, which work somewhat like the same operators you're familiar with from math, like '+' for addition, although while their use in mathematical formulae indicates some kind of relationship between values, in Python, they correspond to simplification steps for the computer to carry out. Python also deals with more than just numbers, and the same operator can have different behavior depending on the types of its operands.
The table below includes very brief reference explanations for every operator 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).
Besides operators, some of the most important building-blocks of Python programs are built-in functions. These represent important ways to convert or deduce information and a few of them, like print are going to appear in almost every program you write.
To use a function, we write the function name followed by parentheses. Any information that the function needs to work with is placed in the parentheses, separated by commas, these expressions are called the arguments of the function. This notation overall is called a "function call" and it looks like this:
len("hello")
In this example we've used the len function with "hello" as the argument, and the result will be the integer 5. When used as part of a larger program, if the function has a result (which most do) then we'd usually store that result in a variable, like this:
myString = "What a lovely day!"
howLong = len(myString)
Here we also used a variable as the input, rather than a fixed value. You could also use a function as part of a larger expression: the place where you write the function call will eventually be simplified to the result value from that function call. Here's an example of a program that uses several built-in functions:
x = 5
y = 4
z = 10
print(max(x, y, z) + min(x, y, z))
The last expression simplifies as follows:
print(max(x, y, z) + min(x, y, z))
print(max(5, y, z) + min(x, y, z)) # variable substitution for x
print(max(5, 4, z) + min(x, y, z)) # variable substitution for y
print(max(5, 4, 10) + min(x, y, z)) # variable substitution for z
print(10 + min(x, y, z)) # max function result
print(10 + min(5, y, z)) # variable substitution for x
print(10 + min(5, 4, z)) # variable substitution for y
print(10 + min(5, 4, 10)) # variable substitution for z
print(10 + 4) # min function result
print(14) # + operator
None # print function result; this result won't show up
Note that as a side effect of this simplification, the text "14" will be printed.
The table below includes brief explanations for the built-in functions 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). Remember you can always use the help function to get more information about another function.
In addition to Python's built-in functions, Python's basic types like strings, lists, and dictionaries have more functions attached to them as "methods" that you'll need to learn about. A "method" is just a function that's attached to a type; to call it we write a specific value of that type (maybe as a variable) and then a period, followed by the method name and any additional arguments. The value before the period always serves as an implicit first argument to the method call. This is what that looks like:
response = input("Do you like ice cream? ")
lowercase = response.lower()
affirmative = lowercase.startswith('y')
Here we used two methods of strings: lower and startswith. The lower method doesn't need any additional arguments beyond the value that it's attached to (but the parentheses are still required to actually call the method), while the startswith method needs one additional argument (which we put inside the parentheses after the method name, just like when we call a regular function). In both cases, the thing before the period (response or lowercase) is functioning like an extra argument to the method (e.g., what are we getting a lowercase version of? The response value).
Why are some functions just built-in and others methods of a particular type? That's because when a function can only work with a specific type of data, making it a method helps prevent errors, because you can't accidentally try to use it with another kind of value. It also helps organize things conceptually. For example, len is a built-in function because it can work with any type of sequence, including strings, lists, etc. On the other hand, lower is a method, because it only makes sense in the context of strings.
The table below includes brief reference explanations for the common methods 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). Remember you can always use the help function to get more information about another function; in the case of methods, you could write something like help(str.lower) or help(list.append) because you still need to reference a particular type to get access to a method.
Besides the turtle/turtleBeads for graphics, 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 and turtleBeads.
The table below includes 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). 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.
The turtle built-in module is used for drawing graphics in Python. turtleBeads.py is a custom module that adds a few commands to easily draw certain shapes centered on the current turtle position, like circles, ellipses, and polygons. This page covers the core commands from both modules and briefly describes how to use them. If you need more details, remember that you can get help for any Python function by calling the built-in help function, for example, you could get help for the turtle penup command by running:
help(penup)
You can download the latest version of turtleBeads.py at this link:
The colors page has pictures showing all of the color names you can use with turtle.
To draw with turtle graphics, you will need to import the built-in turtle module. To do this, add a line of code at the top of your file that looks like this:
from turtle import *
The '*' here means "everything," and this style of import makes the functions in the turtle module directly available.
If you also want to use turtleBeads functions, you should add:
from turtleBeads import *
to the top of your file as well. Make sure the turtleBeads.py file is in the same directory as the file that you are writing your code in, otherwise Python won't be able to import it and you will get an error.