Python Exercise¶

Philip Vishnevsky¶

Python can be downloaded here and documentation can be viewed here.

Python is an "interpreted high-level general-purpose programming language" (Wikipedia. The fact that it is an interpreted language means that the source code we write is converted into machine code line by line, rather than all at once. More on this can be read here. Python is object-oriented, dynamically typed (a variable's data type can change), and white space dependent (can't mess up the tab spaces or things will break!).

When submitting this file to Canvas, submit both the .ipynb (notebook file) and html file versions. To download the html version, click File > Download as > HTML (.html). We will be checking for completion as a part of your participation. The solution to this file will then be posted for you to check.

drawing


1. Print hello world :)¶

In [7]:
print("Hello, World!")
Hello, World!

2. Read in two values (user input), convert to integers, and then print the sum¶

In [10]:
a = int(input())
In [11]:
b = int(input())
In [12]:
print(a + b)
42

Note: type(variable) lets you check the data type of variable.

In [14]:
type(a)
Out[14]:
int

Note: In jupyter notebooks, you can also display values without the print (shown below).

In [16]:
a + b
Out[16]:
42

Question: What prints if you don't convert to integers first?

A string


3. Create a function called "search"¶

This function should take in two string values (let's call them a and b). Both a and b should be set to all lowercase. Then search for the substring b in a. It should return the index of the found substring, or -1 if not found.

In [22]:
def search(a, b):
    a = a.lower()
    b = b.lower()
    return a.find(b)

Here are some examples to test:

In [24]:
search('Hello world', 'hello')
Out[24]:
0
In [25]:
search('Hello world', 'WoRlD')
Out[25]:
6
In [26]:
search('Hello world', ' ')
Out[26]:
5

4. Working with lists¶

In [29]:
food = ['ramen', 'potato', 'chicken nuggets', 'pineapple']

4a. Print the 2nd item.

In [31]:
print(food[1])
potato

4b. Change 'potato' to another food (and print the whole list to check).

In [33]:
food[1] = 'pizza'
print(food)
['ramen', 'pizza', 'chicken nuggets', 'pineapple']

4c. Add a new food item to the end of the list.

In [35]:
food.append('cake')
print(food)
['ramen', 'pizza', 'chicken nuggets', 'pineapple', 'cake']

4d. Use slicing to view the 3rd to last items of the list.

In [37]:
print(food[2:])
['chicken nuggets', 'pineapple', 'cake']

4e. Reverse the list!

In [39]:
food.reverse()
print(food)
['cake', 'pineapple', 'chicken nuggets', 'pizza', 'ramen']

4f. List comprehension

List comprehension allows you to make a new list by performing some operation on each item of an existing list. You can read more about it here.

Use list comprehension to create a new list of all the foods in the food list that contain the letter e.

In [43]:
e_foods = [f for f in food if 'e' in f]
In [44]:
print(e_foods)
['cake', 'pineapple', 'chicken nuggets', 'ramen']

5. Working with dictionaries¶

Here is a reference on dictionaries.

In [48]:
pokemon = {
    "name": "bulbasaur",
    "nickname": "potato",
    "shiny": False
}

5a. Print the name.

In [50]:
print(pokemon["name"])
bulbasaur

5b. Change the value of nickname from potato to something else.

In [52]:
pokemon["name"] = "tomato"
print(pokemon["name"])
tomato

5c. Add the key, value pair "type" : "grass" to the dictionary.

In [54]:
pokemon["type"] = "grass"
print(pokemon)
{'name': 'tomato', 'nickname': 'potato', 'shiny': False, 'type': 'grass'}

6. Loops and If Statements¶

6a. Use the range function to loop through numbers and print each one.

Note: Should iterate between a range of 0 and the length of numbers

In [59]:
numbers = [1, 2, 3, 4, 5]
In [60]:
for i in range(0, len(numbers)):
    print(numbers[i])
1
2
3
4
5

6b. Loop through potato_weights and print yum if it is larger than 0.7

In [62]:
potato_weights = [0.32, 0.53, 1.2, 0.21, 0.36, 0.72, 0.12, 0.23, 0.83, 0.91, 0.56]
In [63]:
for i in range(0, len(potato_weights)):
    if potato_weights[i] > 0.7:
        print("yum")
yum
yum
yum
yum

6c. Loop through and print each value of potato_weights until it is less than 0.2.

There are multiple ways to do this (this is not the only possible answer).

In [66]:
for i in range(0, len(potato_weights)):
    if potato_weights[i] < 0.2: 
        break 
    else: 
        print(potato_weights[i])
0.32
0.53
1.2
0.21
0.36
0.72

7. Poisonous or edible?¶

Make a function that returns whether or not a given mushroom is edible or poisonous. Brainstorm some rules (e.g., if a mushroom is purple, then it is poisonous) with your group based on the mushroom information below. The poisonous attribute is given to help you come up with rules, but don't use this in the function itself.

Please note that this is all completely fake data. :)

drawing

In [71]:
mushrooms = {
    0: {"cap_diameter": 15, "cap_color": "purple", "stem_width": 2, "has_skull":True, 'poisonous':True},
    1: {"cap_diameter": 25, "cap_color": "orange", "stem_width": 5, "has_skull":True, 'poisonous':True},
    2: {"cap_diameter": 3, "cap_color": "green", "stem_width": 6, "has_skull":False, 'poisonous':False},
    3: {"cap_diameter": 8, "cap_color": "green", "stem_width": 3, "has_skull":False, 'poisonous':False},
    4: {"cap_diameter": 55, "cap_color": "green", "stem_width": 35, "has_skull":False, 'poisonous':True},
    5: {"cap_diameter": 7, "cap_color": "purple", "stem_width": 6, "has_skull":False, 'poisonous':True},
    6: {"cap_diameter": 3, "cap_color": "purple", "stem_width": 8, "has_skull":True, 'poisonous':True},
    7: {"cap_diameter": 35, "cap_color": "green", "stem_width": 4, "has_skull":False, 'poisonous':True},
}

Example: If we enter mushrooms[0] into our function, it should return that it is poisonous.

In [73]:
'''
Facts:
All purple mushrooms are poisonous
All skull mushrooms are poisonous
Mushrooms with a cap > 10 are poisonous
'''

def is_poisonous(mushroom):
    if mushroom["cap_diameter"] > 10: return True
    if mushroom["has_skull"]: return True
    if mushroom["cap_color"] == "purple": return True
    return False
    
In [74]:
try:
    for i in range(0, len(mushrooms)):
        assert mushrooms[i]["poisonous"] == is_poisonous(mushrooms[i])
except:
    print("mushrooms were INCORRECTLY classified")
else:
    print("mushrooms were correctly classified")
mushrooms were correctly classified