For example the abs function takes a single number as its argument and returns

# For example the abs function takes a single number as

This preview shows page 7 - 10 out of 13 pages.

# # For example, the abs function takes a single number as its argument and returns the absolute value of that number. The absolute value of a number is its distance from 0 on the number line, so abs(5) is 5 and abs(-5) is also 5.# In[29]:abs(5)# In[30]:abs(-5)# ### 3.1. Application: Computing walking distances# Chunhua is on the corner of 7th Avenue and 42nd Street in Midtown Manhattan, and she wants to know far she'd have to walk to get to Gramercy School on the corner of 10th Avenue and 34th Street.# # She can't cut across blocks diagonally, since there are buildings in the way. She has to walk along the sidewalks. Using the map below, she sees she'd have to walk 3 avenues (long blocks) and 8 streets (short blocks). In terms of the given numbers, she computed 3 as the difference between 7 and 10, *in absolute value*, and 8 similarly. # # Chunhua also knows that blocks in Manhattan are all about 80m by 274m (avenuesare farther apart than streets). So in total, she'd have to walk $(80 \times |42 - 34| + 274 \times |7 - 10|)$ meters to get to the park.# # <img src="map.jpg" alt="visual map about distance calculation"/># # **Question 3.1.1.** <br /> Finish the line num_avenues_away = ... in the next cell so that the cell calculates the distance Chunhua must walk and gives it the name manhattan_distance. Everything else has been filled in for you. **Use the abs function.**
# In[31]:# Here's the number of streets away:num_streets_away = abs(42-34)# Compute the number of avenues away in a similar way:num_avenues_away = abs(7-10)street_length_m = 80avenue_length_m = 274# Now we compute the total distance Chunhua must walk.manhattan_distance = street_length_m*num_streets_away + avenue_length_m*num_avenues_away# We've included this line so that you see the distance# you've computed when you run this cell. You don't need# to change it, but you can if you want.manhattan_distance# Be sure to run the next cell to test your code.# In[32]:check('tests/q311.py')# ##### Multiple arguments# Some functions take multiple arguments, separated by commas. For example, the built-in max function returns the maximum argument passed to it.# In[33]:max(2, -3, 4, -5)# ## 4. Understanding nested expressions# Function calls and arithmetic expressions can themselves contain expressions. You saw an example in the last question:# # abs(42-34)# # has 2 number expressions in a subtraction expression in a function call expression. And you probably wrote something like abs(7-10) to compute num_avenues_away.# # Nested expressions can turn into complicated-looking code. However, the way inwhich complicated expressions break down is very regular.# # Suppose we are interested in heights that are very unusual. We'll say that a height is unusual to the extent that it's far away on the number line from the average human height. [An estimate]() of the average adult human height (averaging, we hope, over all humans on Earth today) is 1.688 meters.# # So if Aditya is 1.21 meters tall, then his height is $|1.21 - 1.688|$, or $.478$, meters away from the average. Here's a picture of that:#
# <img src="numberline_0.png" alt="number line showing height difference and teaching abs value"># # And here's how we'd write that in one line of Python code:# In[34]:abs(1.21 - 1.688)# What's going on here? abs takes just one argument, so the stuff inside the parentheses is all part of that *single argument*.

#### You've reached the end of your free preview.

Want to read all 13 pages?

• Fall '17
• Human height