U2 SB1 84python Teacher
U2 SB1 84python Teacher
In this lesson, you will write a program that requires a mathematical Objectives:
function that is not part of Python’s “built-in” tools. • Use import for additional functions
• Write a program using the menus
• Examine a mathematical relationship
1. The arithmetic mean (average, pronounced “arithMETIC” of two
numbers is:
Example 1: The altitude to the hypotenuse of a right triangle is the geometric mean of the
two segments on the hypotenuse.
Example 2: In an arithmetic sequence, each term after the first one is the arithmetic mean
of its two neighbors. In a geometric sequence each term after the first one is the geometric
mean of its two neighbors.
gm <= am
3. This program requires the square root function which is not part of
Python’s built-in operations. The square root (sqrt) and other
mathematics functions are found in the standard Python module called
math. To use this function, you must import the math module to your
code. Select <Fns…> Modul > math and select the statement at the
top:
from math import *
Modules are used to keep Python small and fast: We only import stuff
that our program will actually use.
The Math menu contains lots of mathematics functions and there are
separate Const and Trig sub-menus, too. To use any of these functions
the math module must be imported into your program.
Teacher Tip: You can also write from math import sqrt if you know you are only going to
need the sqrt function. Or import math which requires you to write math.sqrt().
The math module, along with random and time, are standard Python modules that are
included in the TI-84 Plus CE Python system.
4. Use the input( ) function to enter the first number. First type the variable
a and the = sign ([sto] key).
Recall that input( ) returns a string and we must convert it to a number.
Combine those two steps into one by writing:
a = float(input( ))
First, get float() from <Fns…> Type,
Then, with the cursor inside the parentheses, look on <Fns…> I/O for
the input( ) function.
For the prompt inside the input( ) parentheses, write “First number?”
The question mark is on the <a A # > screen.
Write a second statement to enter the second number (not shown). This
is a good opportunity for you use the <Tools> Copy Line and Paste
Line Below features and then edit the second line.
Teacher Tip: Be careful about the two right parentheses at the end of the line: one for
input() and one for float(). This is a perfect example of “composition of functions.” The inner
function input is processed first, and the result (a string) is passed to the outer function
float.
5. After your two input statements, write two assignment statements, one
for the arithmetic mean and one for the geometric mean:
am = (a + b) / 2
gm = sqrt(a * b)
6. The last task is to write the print( ) statements to display the two
calculated values. Use your imagination! A sample is shown here:
7. Run the program and enter two numbers for which you know the
answers… TEST, TEST, TEST.
Check your guesses with the teacher! Can you prove it?
Teacher Tip:
am >= gm (When is there equality?)
am and gm are always between a and b.