Learner objectives for this lesson
math
module and utilize math-related variables and functionsturtle
moduleOn a blank sheet of paper, write the following:
In pairs, work on the following problems. Each student needs to turn in their own paper to get credit for MA4.
+ # binary addition
- # unary minus (e.g. negation)
% # modulus
= # assignment
() # parentheses
4 / 12 =
4 // 12 =
4 % 12 =
7 // 4 =
9.0 / 4.0 =
3 / 0 =
3.0 % 1 =
16 % 0 =
3 % 5 =
9 % 5 =
2 * 4 ** 2 =
2 ** 4 ** (2 / 4) =
y = m % n
, what are the possible values of y
?m
and 0 for n
:m = int(input("Enter an integer> "))
n = int(input("Enter an integer> "))
m = m + 5
n = 3 * n
print("m = %d\nn = %d\n" %(m, n))
A module is a file that contains a collection of related variables and functions. Python provides several modules for us programmers to use in our programs. In order to use the variables and functions within a module, we have to let Python know we want to use the module with an import <name of module>
statement.
import math
To access one of the variables or functions in a module, you type the module name (somewhere in the code after you import the module, remember Python executes code from top to bottom), followed by a dot, and then the name of the variable or function. For example, we can access an approximation of the mathematical constant pi
($\pi$) in the math
module:
print(math.pi)
print("%.4f" %(math.pi))
As another example, to access the square root function of the math
module, use math.sqrt()
:
print(help(math.sqrt))
print(math.sqrt(9))
The Python math
module defines numerous useful mathematical functions. This library is an excellent example of the power of functions: commonly-used mathematical operations are packaged up in functions that can be re-used over and over. We don't have to define these functions or know how they work, we can simply call the functions and use the return value(s). Examples of math functions available for our use include:
fabs()
for absolute valuesceil()
for computing the ceiling of a numberfloor()
for computing the floor of a numbercos()
for cosine functionsin()
for sine functiontan()
for tangent functionpow()
for raising a number to its powerlog()
for logarithms (see also log2()
and log10()
sqrt()
for computing square rootsNote: trig functions expect arguments in radians, not degrees. To convert degrees to radians, multiply by (math.pi
/ 180) or use the radians()
function in the math
module.
You can find out all the functions available within a module by importing the module, typing the module name and a dot, then pressing tab. This "auto-complete" feature is super helpful when learning a new library or when you can't remember the name of a function.
x = -5
print("x: %d absolute value of x: %d" %(x, math.fabs(x)))
degrees = 90
radians = degrees * (math.pi / 180)
print("sin(%d): %.2f" %(degrees, math.sin(radians)))
There are several Python modules available for doing graphical user interface (GUI) programming. For example, the turtle
graphics library makes it really easy to draw pictures programmatically. We will return to turtle
graphics later, but for now, check out how easy it is to draw:
import turtle
turtle.forward(100)
turtle.done()
Equivalent to the "Scientific Method" in the sciences and the "Systems Approach" in business.
Six basic steps:
Developing software is an iterative process, your first solution is generally not your best. Your understanding of software your required to build evolves as you understand the problem more. At this point don't be afraid to make mistakes!
Apply the software development method to the practice problems below. Have fun!
Note: Some problem descriptions have been adapted from Chapter 2 of Hanly & Koffman's Problem Solving and Program Design in C (7th Edition)
Write a program to compute the total price for a purchase after sales tax. Prompt the user to enter the purchase amount and the sales tax percent. Display the total price (to the nearest 2 decimal places) after adding the sales tax to the purchase amount.
Example output:
Please enter the purchase price: 9.00
Please enter the sales tax as a percent (%): 7.8
Total purchase price after tax: $9.70
purchase = float(input("Please enter the purchase price: "))
tax_percent = float(input("Please enter the sales tax as a percent (%): "))
tax = purchase * (tax_percent / 100.0)
purchase += tax
print("Total purchase price after tax: $%.2f" %(purchase))
Please enter the purchase price: 9.00 Please enter the sales tax as a percent (%): 7.8 Total purchase price after tax: $9.70
Write a program that calculates mileage reimbursement for a salesperson at the rate of $.35 per mile.
Example output:
MILEAGE REIMBURSEMENT CALCULATOR
Please enter the beginning odometer reading: 13505.2
Please enter the ending odometer reading: 13810.6
You traveled 305.4 miles. At $0.35 per mile, your reimbursement is $106.89
print("MILEAGE REIMBURSEMENT CALCULATOR")
odo_begin = float(input("Please enter the beginning odometer reading: "))
odo_end = float(input("Please enter the ending odometer reading: "))
dist_traveled = odo_end - odo_begin
reimbursement = dist_traveled * .35
print("You traveled %.1f miles. At $0.35 per mile, your reimbursement is $%.2f" %(dist_traveled, reimbursement))
MILEAGE REIMBURSEMENT CALCULATOR Please enter the beginning odometer reading: 13505.2 Please enter the ending odometer reading: 13810.6 You traveled 305.4 miles. At $0.35 per mile, your reimbursement is $106.89
The Pythagorean theorem states that the sum of the squares of the sides of a right triangle is equal to the square of the hypotenuse.
$$side1^{2} + side2^{2} = hypotenuse^{2}$$For example, if two sides of a right triangle have lengths 3 and 4, then the hypotenuse must have a length of 5. Together the integers 3, 4, and 5 form a Pythagorean triple. There are an infinite number of such triples. Given two positive integers m
and n
, where m
> n
, a Pythagorean triple can be generated by the following formulas:
Write a program that takes the values for m
and n
as input and displays the values of the Pythagorean triple generated by the formulas above.
Example output:
Please enter a m value: 4
Please enter a n value: 2
Pythagorean triple: 12^2 + 16^2 = 20^2
m = int(input("Please enter an m value: "))
n = int(input("Please enter an n value: "))
side1 = m ** 2 - n ** 2
side2 = 2 * m * n
hypotenuse = m ** 2 + n ** 2
print("Pythagorean triple: %d^2 + %d^2 = %d^2" %(side1, side2, hypotenuse))
Please enter an m value: 4 Please enter an n value: 2 Pythagorean triple: 12^2 + 16^2 = 20^2
# check the output
print(12 * 12 + 16 * 16)
print(20 * 20)
400 400
We will define our own functions!!