Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 2

Python has two different divisions: / will return a float and //

will return an int. Often // is used in conjunction with %, the


modulus (or remainder) operator. Ex. 5//3 = 1, 10//4 = 2.

Strings can be enclosed in either single-quotes or double-


quotes. Or, a string can be enclosed in three quotes to be
extended over multiple lines -- this is called a docstring.

We can specify the values that it loops over using the range
function, or we can loop over a string, a list, a tuple, or a
dict. (When we loop over a dict, note that we loop over the
keys of the dict.) Notice that range(a,b) starts at a and stops
just before, but does not include, b. range(10) is the same as
range(0,10), and loops through 0,1,2,3,...,9.

Other list methods that are important are sort() to sort the
list, and count(element) to count the number of occurrences
of element in the list.

We can break out of either type of loop early using the break
keyword, or we can go immediately to the next iteration of
the loop using the continue keyword.

We should always follow our function declaration with a


docstring describing the functions. That's because IDLE's
help system can access your docstrings!
def prime_factorization(num):
    '''prime_factorization(num) ­> str
    returns a string with the prime factorization of 
num'''
Related to this: every function should have a docstring. I
don't want you to do things because of grades alone, but you
should know that your style score will suffer if there are
missing docstrings.
Use descriptive variable names. A single letter is almost
always a bad variable name except for the simplest of loops.
Use names_with_underscores() all lowercase for functions
and namesWithMixedCase (starting with a lowercase letter)
for variables -- that makes it easy to tell them apart. We'll
use names starting with uppercase for classes when we
cover object-oriented programming later in this course,
starting in Week 3. If you use a consistent naming
convention, it makes your code much easier to read.

Use blank lines to separate functions, or even to separate


significant parts of the same function.

Broadly speaking, there are three main steps in computer


programming:
* Plan out the algorithm.
* Turn the algorithm into actual computer code.
* Test the program to see if it works, and fix it if it's not.

You might also like