Python Refresher

In this notebook, I would like to show you the top 5 things you will need to know in order for you to gain maximum benefit from this workshop.

0. Python is Zero-Indexed


In [20]:
# If you have a list of numbers:
my_list = [1, 5, 2, 3, 9]

# What is the index of the number "3"?
x = 1  # change this line
my_list[x]


Out[20]:
5

1. Namespaces are a cool thing.


In [21]:
# e.g. How do I use the numpy trigonometric functions?

import numpy as np  # you can abbreviate a package import name in order to not use 

np.tan(3)  # change this function call to get the cosine of 3. Also, does "numpy.tan(3)" work


Out[21]:
-0.1425465430742778

2. Python "classes" are objects that have methods.


In [22]:
from datetime import datetime

class Watch(object):
    def __init__(self):
        self.stopwatch_timer = 0

    def current_time(self):
        return str(datetime.now())

w = Watch()
# Write the funciton call that tells the current time on the watch.

3. Numbers can be represented numerically or as strings


In [23]:
x = (3 == '3')  # change this line to make x evaluate to True
x


Out[23]:
False

4. Dictionaries are really useful


In [24]:
from datetime import datetime  # usually, I would opt to place all the import statements at the top.

useful_info = dict()

# Add the key-value pairs to the useful_info dictionary:
# - 'instructor_name':'Eric Ma'
# - 'date_today':datetime.today()

5. Write functions to encapsulate code for reuse.


In [25]:
# Write a function that gives the sum of all numbers up to a passed in number.
# Hint: a for-loop is the easiest implementation.
# Hint: if you're feeling fancy, go ahead and use numpy.

def sum_up_to(x):
    """
    Sums up all of the numbers from 1 to x.
    """
    ###
    pass    
    ###

# Then, write another function that uses that function to compute the sums for every even digit up to a number y.

def sums_for_even_digits(y):
    """
    Computes the sum_up_to(x) for every even number up to y.
    - If y is even, y is included.
    - If y is odd, y is not included.
    
    An example function call is as such:
    
    >>> sums_for_even_digits(10)
    [0, 3, 10, 21, 36, 55]
    """
    ### Write your code below.
    pass
    ###

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]: