An Introduction To Python Modules
Module 6: Lab Activity – An Introduction to Python Modules
Deliverables:
Python programs for the following problems. Use the names listed below:
· Problem 1 – randomrange.py
· Problem 2 – randomint.py
· Problem 3 – randomday.py
· Problem 4 – pi.py
· Problem 5 – degrees.py
· Problem 6 – factorial.py
All submitted code must include comments:
# Your Name
# The Date
# The Problem Number and Description
# Any other information throughout your code that is helpful
Python modules facilitate modular programming. Modular programming refers to the process of breaking a complex program into smaller subtasks, modules. You can use and reuse modules to build a larger application.
Advantages include:
Simplicity – focus on small portions of a problem instead of the entire problem.
Maintainability – designed to use logical boundaries between different problems
Reusability – can reuse by other parts of an applications and removes duplicate code
Scoping – define a separate namespace
All modules use an import statement: import . We used this one last week.
import turtle
All modules use dot notation to call objects in that module. We used this one last week.
alex.forward()
The Random Module
This module provides access to functions that support the generation of random numbers. You can find all of the random functions here: https://docs.python.org/3/library/random.html#module-random
Use it to:
· pick a random number in a given range
· pick a random element from a list
· make a password
Problem 1: Use a for statement and random.randrange to print 10 random integers between 25 and 35.
Problem 2: Use random.randrange to print an odd integer between 0 and 100.
Problem 3: Use random.choice to select a day of the week from a list and print that day.
The Math Module
This module provides access to the mathematical functions. You can find all of the math functions here: https://docs.python.org/3/library/math.html#module-math
Use it to:
· calculate trigonometric functions
· calculate logarithmic functions
· access mathematical constants
Problem 4: Search on the internet for a way to calculate an approximation for pi. There are many that use simple arithmetic. Write a program to compute the approximation and then print that value as well as the value of math.pi from the math module.
Problem 5: Search the internet for how to convert radians to degrees. Write a program to compute the conversion given a user input value. Print this value as well as the calculated value using the degrees function in the math module.
Problem 6: Use a for statement to calculate the factorial of a user input value. Print this value as well as the calculated value using the factorial function in the math module.