Review of lecture 1, part 1

In [ ]:
# Do errors from lecture 1, part 1 because I forgot to do them
In [6]:
# Comments do not run
In [8]:
x = 1 # Once the # appears, everything after is a comment
x
Out[8]:
1
In [ ]:
y = 10 # Cells will not run on their own
In [1]:
# When to use (), [], {}
In [2]:
# () are used for changing the order of operations
(1 + 2) * 3
Out[2]:
9
In [3]:
1 + 2 * 3
Out[3]:
7
In [5]:
# [] are used for lists
x = [1, 2, 3]
x
Out[5]:
[1, 2, 3]
In [9]:
# Do not use {}
In [11]:
# Cells must be run IN ORDER
z = 10
In [10]:
z
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-10-3a710d2a84f8> in <module>()
----> 1 z

NameError: name 'z' is not defined
In [12]:
# Strings and variables are not the same thing
x = 'x'
x
Out[12]:
'x'
In [13]:
# The type keyword is not the same as the verb type
In [ ]:
# What is the result of "True and True"
# How do I know?
True and True
In [14]:
# Variables can be REASSIGNED
x = 10
x = 6.9
x
Out[14]:
6.9
In [15]:
x = 3
x
Out[15]:
3

Printing

In [1]:
# Print is another keyword that will print what is inside the ()
print(1)
1
In [2]:
# Printing works for most types
print(1.)
1.0
In [4]:
# Printing strings removes the quotes
x = 'Hello world!'
print(x)
Hello world!
In [2]:
# You can print multiple things by separating them with commas
print(1, 2)
1 2
In [3]:
# You can print mixed types
print(1, 2.0)
1 2.0
In [7]:
# You can print lists
y = [1, 2, 3]
print(y)
[1, 2, 3]
In [10]:
# Putting a variable on its own line only prints at the very end
x = 1
x
x = 2
x
Out[10]:
2
In [9]:
# But you can print intermediate results using print
x = 1
print(x)
x = 2
print(x)
1
2

Lists

In [13]:
# Lists are indexed STARTING FROM 0
# You can retreive elements from a list using LIST[INDEX] notation
x = [0, 1, 2, 3, 4, 5]
print(x[0])
print(x[1])
0
1
In [16]:
x = ['hello', 'world', 'how', 'do', 'you', 'do']
print(x[2])
how
In [15]:
# Indexing from negative numbers gives elements from the back
x = ['hello', 'world', 'how', 'do', 'you', 'do']
print(x[-1])
do
In [17]:
# len(LIST) gives you the length of the list
x = ['hello', 'world', 'how', 'do', 'you', 'do']
len(x)
Out[17]:
6
In [18]:
# Which can be helpful with long lists
x = [32, 43, 45, 532, 3, 4, 14, 32, 1, 31, 3, 13, 3, 51, 1, 1]
len(x)
Out[18]:
16
In [19]:
# List slicing can give you parts of lists
x = [32, 43, 45, 532, 3, 4, 14, 32, 1, 31, 3, 13, 3, 51, 1, 1]
x[0:1]
Out[19]:
[32]
In [20]:
# It does not include the last element past the colon
x = [32, 43, 45, 532, 3, 4, 14, 32, 1, 31, 3, 13, 3, 51, 1, 1]
x[1:5]
Out[20]:
[43, 45, 532, 3]
In [1]:
# If you don't have something past the colon, it takes until the end
x = [32, 43, 45, 532, 3, 4, 14, 32, 1, 31, 3, 13, 3, 51, 1, 1]
x[5:]
Out[1]:
[4, 14, 32, 1, 31, 3, 13, 3, 51, 1, 1]
In [2]:
# If you don't have something before the colon, it takes from the start
x = [32, 43, 45, 532, 3, 4, 14, 32, 1, 31, 3, 13, 3, 51, 1, 1]
x[:5]
Out[2]:
[32, 43, 45, 532, 3]
In [3]:
# The above syntax works with negative indexes
x = [32, 43, 45, 532, 3, 4, 14, 32, 1, 31, 3, 13, 3, 51, 1, 1]
x[-3:]
Out[3]:
[51, 1, 1]
In [4]:
x = [32, 43, 45, 532, 3, 4, 14, 32, 1, 31, 3, 13, 3, 51, 1, 1]
x[:-4]
Out[4]:
[32, 43, 45, 532, 3, 4, 14, 32, 1, 31, 3, 13]
In [23]:
# You can modify list elements by the usual assignment
x = [0, 1, 2, 3, 5]
print(x)
x[4] = 4
print(x)
[0, 1, 2, 3, 5]
[0, 1, 2, 3, 4]
In [25]:
# Lists can have mixed types
x = [1, 2, 'hi', 4.0, True, None]
x
Out[25]:
[1, 2, 'hi', 4.0, True, None]

String indexing

In [27]:
# String indexing follows the same rules as list indexing
x = 'Hello world!'
x[0]
Out[27]:
'H'
In [28]:
x = 'Hello world!'
x[7]
Out[28]:
'o'
In [1]:
# You can also use len with strings
x = 'Hello world!'
len(x)
Out[1]:
12
In [29]:
x = 'Hello world!'
x[0] = 'B'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-29-5c1075b60537> in <module>()
      1 x = 'Hello world!'
----> 2 x[0] = 'B'

TypeError: 'str' object does not support item assignment

Basic function syntax

In [ ]:
# This is function syntax
def FUNCTION_NAME(ARGUMENT1, ARGUMENT2, ...):
    FUNCTION_BODY
    return RETURN_VALUE
In [30]:
# This is an example of a function, with no function body
def thisIsAFunction(x):
    return x
In [32]:
# This is an example of calling a function
y = thisIsAFunction(100)
print(y)
100
In [34]:
# This is another example of a function that does something. What does it do?
def multByTwo(x):
    return x * 2
In [35]:
# You can pass any type into a function
multByTwo(1)
Out[35]:
2
In [36]:
multByTwo(2.0)
Out[36]:
4.0
In [37]:
multByTwo('hi')
Out[37]:
'hihi'
In [38]:
multByTwo([0, 1])
Out[38]:
[0, 1, 0, 1]
In [43]:
# Function names DO NOT represent what the function does!
def multThree(x):
    return x * 2
In [44]:
multThree(2)
Out[44]:
4
In [3]:
# You can call functions in functions
# You can also call functions on functions
def identityFunction(x):
    return x

def multByTwo(x):
    return 2 * x

def addOne(x):
    return x + 1

def mystery(x):
    return multByTwo(x) + addOne(identityFunciton(x))

print(identityFunction(3))
print(multByTwo(3))
print(addOne(3))
3
6
4
In [ ]:
mystery(3) # what is the output?
In [5]:
# Functions can have multiple arguments
def printBoth(x, y):
    print(x, y)
    
printBoth(1, 2)
1 2
In [7]:
def printThree(x, y, z):
    print(x, y, z)

printThree(1, 2, 3)
1 2 3