cs101 »


Arithmetic Expressions

addition: <Number> + <Number> = <Number>

   outputs the sum of the two input numbers

multiplication: <Number> * <Number> = <Number>

   outputs the product of the two input numbers

subtraction: <Number> - <Number> = <Number>

   outputs the difference between the first and second input numbers

division: <Number> / <Number> = <Number>

   outputs the result of dividing the first number by the second
      Note: if both numbers are integers, the result is truncated to just the whole number part (e.g., 5/3 evaluates to 1).

Variables and Assignment


A name in Python can be any sequence of letters, numbers, and underscores (_) that does not start with a number. We usually use all lowercase letters for variable names, but capitalization must match exactly. Here are some valid examples of names in Python (but most of these would not be good choices to actually use in your programs):


Assignment Statement

An assignment statement assigns a value to a variable:    <Name> = <Expression>

After the assignment statement, the variable <Name> refers to the value of the <Expression> on the right side of the assignment. An <Expression> is any Python construct that has a value.

Python allows the left hand side of an assignment to have more than one name separated by commas.  For example,

a, b = c, d

assigns the value of c to a and the value of d to b.  (What's really going on here is that list of values are packaged into a tuple, and a tuple is separated when multiple names are on the left side of an assignment.  We'll introduce tuples in later units.)


A string is a sequence of characters surrounded by quotes. The quotes can be either single or double quotes, but the quotes at both ends of the string must be the same type. Here are some examples of strings in Python:

"I'm a valid string, even with a single quote in the middle!"

Indexing Strings

The indexing operator provides a way to extract subsequences of characters from a string.

string indexing: <String>[<Number>] = <String>

Outputs a single-character string containing the character at position <Number> of the input <String>. Positions in the string are counted starting from 0, so s[1] outputs the second character in s. If the <Number> is negative, positions are counted from the end of the string: s[-1] produces the last character in s.

string extraction: <String>[<Start Number>:<Stop Number>] = <String>

Outputs a string that is the subsequence of the input string starting from position <Start Number> and ending just before position <Stop Number>. If <Start Number> is missing, starts from the beginning of the input string; if <Stop Number> is missing, the output extends to the end of the input string.


The find method provides a way to find sub-sequences of characters in strings.

find: <Search String>.find(<Target String>) = <Number>

Outputs a number giving the position in <Search String> where <Target String> first appears. If there is no occurrence of <Target String> in <Search String>, the output is -1.

find: <Search String>.find(<Target String>,<Number>) = <Number>

The find method can accept a second input that is a number.  The <Number> indicates the position in the string where the search for the <Target String> begins.  The output is the position of the first occurrence of <Target String> in <Search String> at or after position <Number>.  If there is no occurrence of <Target String> in <Search String> at or after position <Number>, the output is -1.