# Do errors from lecture 1, part 1 because I forgot to do them
# Comments do not run
x = 1 # Once the # appears, everything after is a comment
x
1
y = 10 # Cells will not run on their own
# When to use (), [], {}
# () are used for changing the order of operations
(1 + 2) * 3
9
1 + 2 * 3
7
# [] are used for lists
x = [1, 2, 3]
x
[1, 2, 3]
# Do not use {}
# Cells must be run IN ORDER
z = 10
z
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-10-3a710d2a84f8> in <module>() ----> 1 z NameError: name 'z' is not defined
# Strings and variables are not the same thing
x = 'x'
x
'x'
# The type keyword is not the same as the verb type
# What is the result of "True and True"
# How do I know?
True and True
# Variables can be REASSIGNED
x = 10
x = 6.9
x
6.9
x = 3
x
3
# Print is another keyword that will print what is inside the ()
print(1)
1
# Printing works for most types
print(1.)
1.0
# Printing strings removes the quotes
x = 'Hello world!'
print(x)
Hello world!
# You can print multiple things by separating them with commas
print(1, 2)
1 2
# You can print mixed types
print(1, 2.0)
1 2.0
# You can print lists
y = [1, 2, 3]
print(y)
[1, 2, 3]
# Putting a variable on its own line only prints at the very end
x = 1
x
x = 2
x
2
# But you can print intermediate results using print
x = 1
print(x)
x = 2
print(x)
1 2
# 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
x = ['hello', 'world', 'how', 'do', 'you', 'do']
print(x[2])
how
# Indexing from negative numbers gives elements from the back
x = ['hello', 'world', 'how', 'do', 'you', 'do']
print(x[-1])
do
# len(LIST) gives you the length of the list
x = ['hello', 'world', 'how', 'do', 'you', 'do']
len(x)
6
# 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)
16
# 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]
[32]
# 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]
[43, 45, 532, 3]
# 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:]
[4, 14, 32, 1, 31, 3, 13, 3, 51, 1, 1]
# 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]
[32, 43, 45, 532, 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:]
[51, 1, 1]
x = [32, 43, 45, 532, 3, 4, 14, 32, 1, 31, 3, 13, 3, 51, 1, 1]
x[:-4]
[32, 43, 45, 532, 3, 4, 14, 32, 1, 31, 3, 13]
# 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]
# Lists can have mixed types
x = [1, 2, 'hi', 4.0, True, None]
x
[1, 2, 'hi', 4.0, True, None]
# String indexing follows the same rules as list indexing
x = 'Hello world!'
x[0]
'H'
x = 'Hello world!'
x[7]
'o'
# You can also use len with strings
x = 'Hello world!'
len(x)
12
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
# This is function syntax
def FUNCTION_NAME(ARGUMENT1, ARGUMENT2, ...):
FUNCTION_BODY
return RETURN_VALUE
# This is an example of a function, with no function body
def thisIsAFunction(x):
return x
# This is an example of calling a function
y = thisIsAFunction(100)
print(y)
100
# This is another example of a function that does something. What does it do?
def multByTwo(x):
return x * 2
# You can pass any type into a function
multByTwo(1)
2
multByTwo(2.0)
4.0
multByTwo('hi')
'hihi'
multByTwo([0, 1])
[0, 1, 0, 1]
# Function names DO NOT represent what the function does!
def multThree(x):
return x * 2
multThree(2)
4
# 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
mystery(3) # what is the output?
# Functions can have multiple arguments
def printBoth(x, y):
print(x, y)
printBoth(1, 2)
1 2
def printThree(x, y, z):
print(x, y, z)
printThree(1, 2, 3)
1 2 3