# If you've filled in the above code, this should just work.difference = predicted_distance_m - estimated_distance_mdifferenceOut:0.040223694659304865In :_ = ok.grade('q232')/home/jovyan/materials-x18/materials/x18/lab/1/lab01/tests/q232.py: All tests passed!3. Calling functions¶The most common way to combine or manipulate values in Python is by calling functions. Python comes with many built-in functions that perform common operations.For example, the absfunction 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 :abs(5)Out:5In :abs(-5)Out:53.1. Application: Computing walking distances¶Chunhua is on the corner of 7th Avenue and 42nd Street in Midtown Manhattan, and she wants to knowfar 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 (avenues are 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.Question 3.1.1.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 absfunction.In :# 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_distanceOut:1462Be sure to run the next cell to test your code.In :_ = ok.grade('q311')/home/jovyan/materials-x18/materials/x18/lab/1/lab01/tests/q311.py: All tests passed!Multiple arguments¶Some functions take multiple arguments, separated by commas. For example, the built-in maxfunction returns the maximum argument passed to it.
In :max(2, -3, 4, -5)Out:44. 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.
You've reached the end of your free preview.
Want to read all 19 pages?
- Fall '17
- Human height