# Pavan Kumpatla
# December 28, 2021.
# I learned about docstrings and how to access and create them.
# I had no difficulties
def C2F(degrees_celsius):
# creates new function with parameter
""" Convert Celsius to Fahrenheit"""
# docstring
return degrees_celsius * (9/5) + 32
# returns
print("Accessing docstrings using __doc__:\n")
# prints
print(C2F.__doc__)
# prints docstring
def C2F(degrees_celsius):
# creates new function with parameter
""" Convert Celsius to Fahrenheit"""
# docstring
return degrees_celsius * (9/5) + 32
# returns
print("Accessing docstrings using help:\n")
# prints
help(C2F)
# asks for help with C2F
def kg2lb(kilograms):
# creates new function with parameter
"""
Convert kilograms to pounds
args:
kilograms: weight in kg
returns:
pounds: weight in lb
"""
# docstring
pounds = kilograms * 2.20462262185
# multiplies, stores
return pounds
# returns
print("Accessing docstrings using __doc__:\n")
# prints
print(kg2lb.__doc__)
# prints docstring
def kg2lb(kilograms):
# creates new function with parameter
"""
Convert kilograms to pounds
args:
kilograms: weight in kg
returns:
pounds: weight in lb
"""
# docstring
pounds = kilograms * 2.20462262185
# multiplies
return pounds
# returns
print("Accessing docstrings using help:\n")
# prints
help(kg2lb)
# asks for help for kg2lb
# [ ] The following function generates a single die roll
# Document the function using a one-line docstring
from random import randint
# import random integer from random
def die_roller ():
""" Returns random integer between 1-6 """
return (randint(1, 6))
print(die_roller.__doc__)
# prints
# [ ] The following function computes the area of a circle
# Document the function using a one-line docstring
from math import pi
# import pi from math
def circle_area(r):
""" Returns the area of circle """
return pi * (r ** 2)
# returns
print(circle_area.__doc__)
# prints docstring
# [ ] 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):
# creates new function with 2 parameters
"""
Counts the number of times the value in `a` appears in `lst`
args:
lst = list
a = value
returns:
count (the number of times a is in lst)
"""
count = 0
# stores
for element in lst:
# for element in lst
if a == element:
# if a is equal to element
count = count + 1
# add 1 to count
return count
print(count_occurrences.__doc__)
# prints docstring
# [ ] 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
# imports
def future_date(d):
"""
Prints out the date `d` number of days after today
args:
d = stores int value to calculate days after today
returns:
count (the number of times a is in lst)
"""
today = date.today()
# stores today's date
td = timedelta(days = d)
# converts to timedelta object, stores
future = today + td
# adds, stores
print("Date {:d} from today is: {:s}".format(d, future.strftime("%a %h %d, %Y")))
# prints, formats
# Date 10 days from today
future_date(10)
# calls function
print(future_date.__doc__)
# prints docstring