Python documentation reference.


Numeric and logical types

day = 7
temp = 20
(day > 7 and temp != 20) or (day<1)

Strings

Arrays of characters, can be used to hold single characters, words, sentences, user input/output, etc. Strings are declared using single or double quotes.

c   = "a"
wd  = "moxie"
sen = "Your midterm is on Friday!"
print(c)
print(wd)
print(sen)

They can be easily concatenated or repeated.

print("hi" + "there")
print(3*"blah")

Shortcut operators

The operators +=, *=, /=, -= can be used to do something and then assign the value back to a variable: for instance x+=1 is exactly equivalent to x = x+1

built-in functions

Building Strings

You can ‘build’ strings by creating an empty string and adding to it

s = ""
s += "What's"
s += " "
s += "up?"
print(s)

Lists

Lists are used to store arrays of any type. They can all be the same type (for example int), or differing types. They can also be added together like strings. Create a list with square brackets ([1, 2, 3]); an empty list is [].

numlist  = [1, 2, 3, 4, 5, 525600]
wordlist = ['this', 'is', 'a', 'list', 'of', 'words']
print(numlist)
print(wordlist)
print(numlist + wordlist)

range()

Nested Lists

Lists can be nested too (lists in lists).

nestlist = [[1,2,3],[4,5,6],[]]
print(nestlist)

Sometimes we represent matrices as lists of equal-length lists:

m = [[1,2], [3,4], [5,6]] ## a 3 x 2 (3-row, 2-column) matrix

A matrix stored this way has len(m) rows and len(m[0]) columns.

Useful methods for working with lists include append() for adding a single element to the end of a list, no matter what type it is.

numlist = [1, 2, 3]
print(numlist)
numlist.append(4)
print(numlist)
numlist.append([5])  ## last element is a list!
print(numlist)

An alternative method, extend(), can be used to concatenate lists similar to using the plus (+) operator. Note that extend() expects its argument to be a list.

numlist = [1, 2, 3]
print(numlist)
numlist.extend([4])
print(numlist)
numlist.extend([5, 6, 7])
print(numlist)

Note on Mutability

If you assign a list variable to another variable, both variables will point at the same list.

L1 = [1, 2, 3, 4, 5]
L2 = L1
L2.append(55)
print(L1)
print(L2)

built-in functions for strings/lists/tuples

Useful methods for Lists

Some other helpful methods for working with lists (from class notes):

Suppose x = [17, 35, 29]

.index() and .count() also work on strings and tuples.


Tuples

Similar to lists, but are immutable (more on that later).

tup = (1, 2, 3, (4, 5))
print(tup)

An empty tuple is (); a tuple with one element is ("hello",).


Indexing and Slicing

Strings, lists, and tuples all support indexing and slicing. This means we can access individual elements or ranges of elements in any of these types. Recall that element indices start at 0.

nums = [1,2,3,4,5]
s = "Pythons are snakes"
print(nums[0])      # indexing
print(nums[1:4])    # slicing
print(s[-1])        # indexing from end
print(s[-3:])       # slicing to end

With nested lists/tuples, double indexing is used.

tup = ((1,2,3),(4,5))
print(tup[0])
print(tup[0][1])
print(tup[-1][-1])

With lists you can assign to elements using indexing/slicing, this does not work with strings/tuples as they are immutable.

numlist = [[1, 2, 3], [], [4, 5]]
print(numlist)
numlist[0][2] = 42
print(numlist)

If statements

if statements can be used to conditionally execute code. We usually check conditions using logical operators such as

For example:

x = 5
if x < 17:
    print("x is smaller")

Alternative conditions can be checked at the same time using elif statements, and a default action can be set using else.

x = 42
if x < 17:
    print("x is smaller")
elif x > 17:
    print("x is bigger")
else:
    print("x is the same")

Loops

We can repeatedly carry out sequences of instructions using for or while loops. Traditionally, for loops are used when we know how long the loop needs to run for, and while loops are used when we do not know or would rather have the computer figure it out.

for i in range(10):
    print(i)
i = 0
while i**2 < 87:
    print(i)
    i += 2

You can also use break to get out of a loop:

i = 0
while True:
  print(i)
  i += 2
  if i**2 >= 87:
    break

Functions

We can define functions to store sections of code we want to run repeatedly. The input/output is specified using arguments and return statements

def add1(x):
    x += 1
    return(x)

x = 3
print(add1(x))
print(x)

Note that x is not modified in the above example because it is immutable. Immutable types are numbers (int/float), bool, str, and tuples. Important: lists are mutable.

def append4(L):
    L.append(4)
    return(L)

numL = [1,2,3]
print(append4(numL))
print(numL)

Common errors