In [1]:
# Exercise 1-1.
# Write a well-structured English sentence with invalid tokens in it. 
# Then write another sentence with all valid tokens but with invalid 
# structure.

# When you read a sentence in English or a statement in a formal 
# language, you have to figure out what the structure of the sentence 
# is (although in a natural language you do this subconsciously). This 
# process is called parsing.

In [2]:
# Exercise 1-2.

# import requests
# from IPython.display import HTML
# python_page = requests.get("http://python.org")
# HTML(python_page.text)

In [3]:
# Exercise 1-3.

# help() # This actually works in IPython!
help('print')


Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.


In [4]:
# Exercise 1-4.

def get_mph(distance, time, distance_units="mi"):
    """ Given a distance, its units and time it took will return the 
    average speed in miles per hour or MPH.
    
    Time expects HH:MM:SS format as a string, this could be converted 
    to time object later.
    
    """
    if distance_units in ["mi", "mile", "miles"]:
        distance = distance
    elif distance_units in ["km", "kilometers"]:
        distance = distance * 0.621371
    elif distance_unit in ["m", "meters"]:
        distance = (distance / 1000) * 0.621371
    elif distance_unit in ["f", "ft", "feet"]:
        distance = distance / 5280
    else:
        print("Please enter a valid unit for distance.")
        
    time = time.split(":")
    hours = int(time[0])
    mins = int(time[1])
    secs = int(time[2])
    
    total_secs = hours * 60 * 60 + mins * 60 + secs
    total_hrs = total_secs / (60 * 60)
    
    return round(distance / total_hrs, 2)

d = 10
du = "km"
t = "00:43:30"
print(get_mph(d, t, du))
print(get_mph(26.2, "04:37:00"))


8.57
5.68