Case Study #1¶

Question 1¶

Given the equation for calculating Body Mass Index (BMI) is:

$$BMI = \frac{(\text{weight in pounds}) \times 703}{(\text{height in inches})^2}$$

Say an individual weighs 150 lbs and is 68 inches tall. What is their BMI?

weight = ____
height = ____
bmi = ____
In [1]:
weight = 150
height = 68
bmi = (weight * 703) / (height ** 2)

print(bmi)
22.80493079584775

Question 2¶

Given the area of a circle is:

$$A = \pi(\text{diameter}/2)^2$$

Say you have a 12" diameter pizza. Compute the area of the pizza, and assign it to the variable area.

area = ____

Hint: run from math import pi to use the variable pi for $\pi$.

Now, say that the cost of the pizza was $8. Compute the cost per square inch of this pizza.

cost_per_sqin = ____
In [2]:
from math import pi

area = pi * ((12 / 2) ** 2)

print(area)
113.09733552923255
In [3]:
cost_per_sqin = 8 / area

print(cost_per_sqin)
0.0707355302630646

Question 3¶

Given the variable language, which contains a string, use if/elif/else to write a function that:

  • returns "I love snakes!" if language is "Python" (with any kind of capitalization)
  • returns "Are you a pirate?" if language is "R" (with any kind of capitalization)
  • returns "What is language?" if language is any other value
In [4]:
def which_language(language):
    if language.lower() == "python":
        return "I love snakes!"
    elif language.lower() == "r":
        return "Are you a pirate?"
    else:
        return "What is language?"
In [5]:
print(which_language("Python"))
print(which_language("python"))
print(which_language("R"))
print(which_language("rust"))
I love snakes!
I love snakes!
Are you a pirate?
What is language?

Question 4¶

Say we have the following heights (in inches) and weights (in kg) for 10 children:

heights = [62, 58, 61, 61, 59, 64, 63, 61, 60, 62]
weights = [69, 62, 57, 59, 59, 64, 56, 66, 67, 66]

Using the BMI formula from earlier, and the conversion of 1 kg = 2.205 lbs, compute the BMI for each child. What is the average BMI?

average_bmi = ____
In [13]:
heights = [62, 58, 61, 61, 59, 64, 63, 61, 60, 62]
weights = [69, 62, 57, 59, 59, 64, 56, 66, 67, 66]

weights_lbs = [w * 2.205 for w in weights]

def bmi(height, weight):
    return (weight * 703) / (height ** 2)
In [14]:
all_bmis = []
for i, h in enumerate(heights):
    this_bmi = bmi(h, weights_lbs[i])
    all_bmis.append(this_bmi)
In [15]:
average_bmi = sum(all_bmis) / len(all_bmis)

print(average_bmi)
26.004155847803467

Question 5¶

Given this nested dictionary, extract the element containing "BANA":

d = {
    "a_list": [1, 2, 3],
    "a_dict": {
        "first": ["this", "is", "inception"],
        "second": [1, 2, 3, "BANA"]
    }
}
In [16]:
d = {
    "a_list": [1, 2, 3],
    "a_dict": {
        "first": ["this", "is", "inception"],
        "second": [1, 2, 3, "BANA"]
    }
}

d["a_dict"]["second"][3]
Out[16]:
'BANA'
In [17]:
# access the value for "a_dict", which is another dictionary
d["a_dict"]
Out[17]:
{'first': ['this', 'is', 'inception'], 'second': [1, 2, 3, 'BANA']}
In [18]:
# access the value for "second", which is a list
d["a_dict"]["second"]
Out[18]:
[1, 2, 3, 'BANA']
In [19]:
# access the 4th element of the list
d["a_dict"]["second"][3]
Out[19]:
'BANA'

Question 6¶

Create a function divisible() that accepts two integers (a and b), and returns True if a is divisible by b without a remainder. For example, divisible(10, 3) should return False, while divisible(6, 3) should return True.

Once you have created this function, run the following code. Make sure to use the same seed values. What is your result?

In [ ]:
import random

random.seed(123)
a = random.randint(10, 100)
b = random.randint(1, 10)

divisible(a, b)
In [20]:
def divisible(a, b):
    if a % b == 0:
        return True
    else:
        return False
In [21]:
import random

random.seed(123)
a = random.randint(10, 100)
b = random.randint(1, 10)

divisible(a, b)
Out[21]:
False

Question 7¶

Create a function lucky_sum that takes a list of integers, and returns their sum. However, if one of the values is 13, then it does not count towards the sum, nor do any values to its right.

For example, your function should behave as follows:

lucky_sum([1, 2, 3, 4])
10

lucky_sum([1, 13, 3, 4])
1

lucky_sum([1, 3, 13, 4])
4

lucky_sum([13])
0

Once you've created this function, run the following code. Be sure to run the code exactly as you see it. What is your result?

In [ ]:
import random

random.seed(18)
my_values = random.choices(range(1, 14), k=26)
lucky_sum(my_values)
In [22]:
def lucky_sum(numbers):
    running_sum = 0
    for n in numbers:
        if n == 13:
            break
        running_sum = running_sum + n
    return running_sum
In [23]:
import random

random.seed(18)
my_values = random.choices(range(1, 14), k=26)
lucky_sum(my_values)
Out[23]:
101

Question 8¶

Run the following code. Read and embrace this mantra throughout this workshop, and your Python code-writing lives!

In [ ]:
import this
In [24]:
import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!