CS 101 - Python Quick Reference

Basic Python

Data types

Operators

Arithmetic operators

Operator Meaning
+ addition
- subtraction
* multiplication
/ division [note that if both operands are integers, the result will be an integer (e.g., 7/2 is 3, not 3.5)]
% mod or modulo, gives the remainder of integer division (e.g., 7%4 == 3, 9%2 == 1)
** exponent (e.g., 2**3 == 8)

Comparison operators

Operator Meaning
< less than
> greater than
<= less than or equal to
>= greater than or equal to
== equals [note that this is ==, not =]
!= not equal to

Boolean operators

Operator Meaning
and given expression1 and expression2, evaluates to True if both expression1 and expression2 are True
or given expression1 or expression2, evaluates to True if either expression1 or expression2 is True
not given not expression, evaluates to True if expression if False

Operator precedence (from highest to lowest)

Operator Meaning
() grouping
** exponent
-[negation sign] negation(not subtraction)
*,/,% multiplicative operators
+,- additive operators
<,>,<=,>=,==,!= comparison operators
not logical NOT
and logical AND
or logical OR

Variables

Variables are just named storage locations for values. Each variable can hold a single value. We load values into variables using assignment statements.

variable name = expression

When Python encounters an assignment statement, it first evaluates the expression, performing any required operations until there is a single value on the right of the '='. This value is then stored in the location matching the variable name.

Comments

Comments are annotations that we put in source code that the computer ignores. They are more of that "human communication thing". In this instance, we are using comments as a way to embed directions to you. There are two kinds of comments in Python: multi-line comments and inline comments.

Multi-line comments are multiple lines of text surrounded by triple quotation marks. We use these for longer explanations about what our code does.

Inline comments start with a # and include everything from the # until the end of the line. We use these for shorted, more target comments.

Another key use of comments is for debugging. We can comment out lines of code that we don't want to run.

Data structures

Strings

Strings are the way that we store text in Python. They are represented as a sequence of characters surrounded by quotes (single or double). There are many things we can do with strings (Python documentation for strings). Here are the ones we discussed in class. For all of the examples, assume we have two string variables str1 = 'tardis' and str2 = 'dalek'.

Operation Meaning Example Result
+ concatenate two strings str1 + str2 'tardisdalek'
* make multiple copies and concatenate them together str1 * 3 'tardistardistardis'
len() return the length of the string len(str1) 6
str.index(*substr*) get the index of the substr in the string str1.index('a') 1

We also saw that we can use [] to extract characters out of the string. Here are some examples:

Expression Result
str1[2] 'r'
str1[0] 't
str1[-1] 's
str1[1:3] 'ar'
str1[0:3] 'tar'
str1[:3] 'tar'
str1[3:-1] 'di'
str1[3:] 'dis'

Lists

Lists allow us to store a collection of things in one place. lists could be a list of numbers (e.g., [1,2,3,4]), a list of strings (e.g., ['Tom', 'Jon', 'Patrick']), or a mixture (e.g., [5, 'black', 16]). We can even put lists in there (e.g., [1,2,[3,4],5]).

Most of the functions we looked at for strings also work on lists (including indexing and slicing for extracting items out of the list). Look at the Python documentation for more functions.

The real big difference between lists and strings is that you can actually change individual values within the list, so we can use indexed lists on the left side of assignment statements. For example, if we have a list l = [3,2,4], we can set the second item using l[1] = 5, so our list will become [3,5,4].