Last active
September 11, 2020 01:37
-
-
Save lucpet/6314588 to your computer and use it in GitHub Desktop.
Practical Python Chapter 3 exercises
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 3. Following the function design recipe, define a function that has one | |
# parameter, a number, and returns that number tripled. | |
def recipie(x): | |
""" (number) -> number | |
Following the function design recipe, define a function that has one | |
parameter, a number, and returns that number tripled. | |
""" | |
return x * 3 | |
#----------------------------------------------------------------------- | |
# 4. Following the function design recipe, define a function that has two | |
# parameters, both numbers, and returns the absolute value of the difference | |
# of the two. Hint: Call the built-in abs function. | |
def absolute_difference(num1, num2): | |
""" (number, number) -> number | |
Return the absolute value of the difference between number1 | |
and number2. | |
>>> absolute_difference(3, 7) | |
4 | |
""" | |
return abs(num1 - num2) | |
#----------------------------------------------------------------------- | |
# 5. Following the function design recipe, define a function that has one | |
# parameter, a distance in kilometers, and returns the distance in miles. | |
# (There are 1.6 kilometers per mile.) | |
def convert_to_miles(k): | |
""" (number) -> number | |
define a function that has one | |
parameter, a distance in kilometers, and returns the distance in miles. | |
(There are 1.6 kilometers per mile.) | |
""" | |
return k * 1.6 | |
#----------------------------------------------------------------------- | |
# 6. Following the function design recipe, define a function that has three | |
# parameters, grades between 0 and 100 inclusive, and returns the average | |
# of those grades. | |
def grade_average(num1, num2, num3): | |
""" (number, number, number) -> number | |
Return the average of the grade1, grade2, and grade3, where | |
each grade ranges from 0 to 100, inclusive. | |
>>> grade_average(80, 95, 90) | |
88.33333333333333 | |
""" | |
return (num1 + num2 + num3) /3 | |
#----------------------------------------------------------------------- | |
# 7. Following the function design recipe, define a function that has four | |
# parameters, all of them grades between 0 and 100 inclusive, and returns | |
# the average of the best 3 of those grades. Hint: Call the function that you | |
# defined in the previous question. | |
def av_grade(grade1, grade2, grade3, grade4): | |
""" (number, number, number, number) -> number | |
define a function that has four parameters, | |
all of them grades between 0 and 100 inclusive, and returns | |
the average of the best 3 of those grades. Hint: Call the function that you | |
defined in the previous question. | |
""" | |
sub_lowest_score = min(grade1, grade2, grade3, grade4) | |
add_all_grades = (grade1 + grade2 + grade3 + grade4) | |
best_three = (add_all_grades - sub_lowest_score) /3 | |
return best_three | |
#----------------------------------------------------------------------- | |
# 8. Complete the examples in the docstring and then write the body of the | |
# following function: | |
def weeks_elapsed(day1, day2): | |
""" (int, int) -> int | |
day1 and day2 are days in the same year. Return the number of full weeks | |
that have elapsed between the two days. | |
>>> weeks_elapsed(3, 20) | |
2 | |
>>> weeks_elapsed(20, 3) | |
2 | |
>>> weeks_elapsed(8, 5) | |
>>> weeks_elapsed(40, 61) | |
""" | |
return int(abs((day1 - day2) / 7)) | |
#----------------------------------------------------------------------- | |
# 9. Consider this code: | |
def square(num): | |
""" (number) -> number | |
Return the square of num. | |
>>> square(3) | |
9 | |
""" | |
return num ** 2 | |
# In the table below, fill in the Example column by writing square, num, | |
# square(3), and 3 next to the appropriate description. | |
# Description Example | |
# Parameter num | |
# Argument 3 | |
# Function name square | |
# Function call square(3) | |
#--------------------------------------------------------------------- | |
# 10. Write the body of the square function from the previous question. | |
# Done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment