def C2F(degrees_celsius):
""" Convert Celsius to Fahrenheit"""
return degrees_celsius * (9/5) + 32
print("Accessing docstrings using __doc__:\n")
print(C2F.__doc__)
def C2F(degrees_celsius):
""" Convert Celsius to Fahrenheit"""
return degrees_celsius * (9/5) + 32
print("Accessing docstrings using help:\n")
help(C2F)
def kg2lb(kilograms):
"""
Convert kilograms to pounds
args:
kilograms: weight in kg
returns:
pounds: weight in lb
"""
pounds = kilograms * 2.20462262185
return pounds
print("Accessing docstrings using __doc__:\n")
print(kg2lb.__doc__)
def kg2lb(kilograms):
"""
Convert kilograms to pounds
args:
kilograms: weight in kg
returns:
pounds: weight in lb
"""
pounds = kilograms * 2.20462262185
return pounds
print("Accessing docstrings using help:\n")
help(kg2lb)
# [ ] The following function generates a single die roll
# Document the function using a one-line docstring
from random import randint
def die_roller():
"""Returning a random number between 1 & 6"""
return (randint(1, 6))
print("Accessing docstring \n")
help(die_roller)
# [ ] The following function computes the area of a circle
# Document the function using a one-line docstring
from math import pi
def circle_area(r):
"""Returns the area of a circle given radius"""
return pi * (r ** 2)
help(circle_area)
# [ ] The following program counts the number of times the value in `a` appears in `lst`
# Document the function using a multi-line docstring
def count_occurrences(a, lst):
"""
Counts the number of times a value: a appears in a list
ARGS:
a: any value
lst: list
RETURNS:
count: a in lst
"""
count = 0
for element in lst:
if a == element:
count = count + 1
return count
help(count_occurrences)
# [ ] The following program prints out the date `d` number of days after today
# Document the function using a multi-line docstring
from datetime import date, timedelta
def future_date(d):
"""
prints the date, d days after today
ARGS:
d: int, days after today
"""
today = date.today()
td = timedelta(days = d)
future = today + td
print("Date {:d} from today is: {:s}".format(d, future.strftime("%a %h %d, %Y")))
# Date 10 days from today
future_date(10)
help(future_date)