Review from yesterday

In [ ]:
# Fill in based on questions
In [3]:
# Every function has a name
# Once you define a function, it can be "called"
def this_is_the_function_name(x):
    return x

this_is_the_function_name(100)
Out[3]:
100
In [5]:
# Every function takes 0 or more arguments
def func(argument):
    return argument

func(10)
Out[5]:
10
In [6]:
# Functions can have code inside the function
def function_with_body(x):
    y = x + 10
    return y

function_with_body(10)
Out[6]:
20
In [8]:
# The function body must be indented
def function_with_error(x):
return x

function_with_error(x)
  File "<ipython-input-8-d9f090779bcf>", line 3
    return x
         ^
IndentationError: expected an indented block
In [10]:
# Write a function that returns the argument x plus two
def plusTwo(x):
    return x + 2

plusTwo(5)
Out[10]:
7
In [11]:
def complicatedFunction(x):
    x += 5
    x *= 3
    x -= 2
    x %= 100
    return x

complicatedFunction(10)
Out[11]:
43
In [ ]:
# indentation is critical
In [13]:
# You can call functions within functions
def addTwo(x):
    return x + 2

def mystery(x):
    y = addTwo(x)
    z = addTwo(y)
    return z

mystery(10)
Out[13]:
14
In [14]:
# Comparisions: since I forgot
1 > 0
Out[14]:
True
In [15]:
1 < 0
Out[15]:
False
In [16]:
1 >= 1
Out[16]:
True
In [17]:
1 <= 1
Out[17]:
True

Control flow

In [ ]:
# This is condtional syntax
if CONDITIONAL:
    IF_BODY
elif CONDITIONAL: # Optional
    ELIF_BODY
else: # Optional
    ELSE_BODY
In [4]:
# Here is an example of an if statement
if True:
    print('Hello world!')
Hello world!
In [5]:
if False:
    print('Hello world!')
In [8]:
# And with a conditional
if 1 == 1:
    print('Hello world!')
Hello world!
In [6]:
# Else is a catch-all
if 1 == 1:
    print('Hello world!')
else:
    print('Goodbye world!')
Hello world!
In [7]:
if 1 == 2:
    print('Hello world!')
else:
    print('Goodbye world!')
Goodbye world!
In [10]:
# elif only triggers on the condition
x = 1
if x == 0:
    print('x = 0')
elif x == 2:
    print('x = 2')
In [11]:
# They can be combined
x = 1
if x == 0:
    print('x = 0')
elif x == 2:
    print('x = 2')
else:
    print('x is neither 0 nor 2')
x is neither 0 nor 2
In [2]:
# Here's an example of a conditional in a function
def passingGrade(grade):
    if grade > 70.0:
        return True
    else:
        return False
    
daniel_grade = 60.0
if passingGrade(daniel_grade):
    print('Daniel passed!')
else:
    print('Daniel failed! :(')
Daniel failed! :(

Lists review

In [12]:
# List indexing review
x = [0, 1, 2, 4, 5]
print(x[1])
print(x[-1])
print(x[-3])
print(x[2])
1
5
2
2
In [13]:
# List slicing review
x = [1, 'hello', 0, 'world']
print(x[0:2])
[1, 'hello']
In [1]:
x = [1, 'hello', 0, 'world']
print(x[:4])
[1, 'hello', 0, 'world']
In [20]:
x = [1, 'hello', 0, 'world']
print(x[-2:])
print(x[:-1])
[0, 'world']
[1, 'hello', 0]
In [23]:
# List assignment review
x = [1, 'hello', 0, 'world']
x[2] = 2
print(x)
[1, 'hello', 2, 'world']
In [24]:
# List len review
x = [1, 'hello', 0, 'world']
print(len(x))
4

Lists vs strings

In [26]:
# Review of strings vs lists
x = [0, 1, 2, 3, 4, 5]
y = '012345'
print(type(x))
print(type(y))
<class 'list'>
<class 'str'>
In [33]:
# Review of printing strings
y = '012345'
print(y)
012345
In [28]:
# Differences in indexing
x = [0, 1, 2, 3, 4, 5]
y = '012345'
x[0]
Out[28]:
0
In [29]:
x = [0, 1, 2, 3, 4, 5]
y = '012345'
y[0]
Out[29]:
'0'
In [32]:
x = [0, 1, 2, 3, 4, 5]
y = '012345'
type(y[0])
Out[32]:
str
In [30]:
x = [0, 1, 2, 3, 4, 5]
y = '012345'
print(x[0])
print(y[0])
0
0
In [31]:
# Differences in slicing
x = [0, 1, 2, 3, 4, 5]
y = '012345'
print(x[0:2])
print(y[0:2])
[0, 1]
01
In [34]:
# Assignment for lists
x = [0, 1, 2, 3, 4, 5]
y = '012345'
x[3] = -1
x
Out[34]:
[0, 1, 2, -1, 4, 5]
In [35]:
# Assignmenet for strings fails
x = [0, 1, 2, 3, 4, 5]
y = '012345'
y[3] = -1
y
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-35-147701be05ec> in <module>()
      1 x = [0, 1, 2, 3, 4, 5]
      2 y = '012345'
----> 3 y[3] = -1
      4 y

TypeError: 'str' object does not support item assignment