Python Notes

 

(work in progress)

Division in Python

 

4/3 is interpreted by the Python compiler as integer division

4.0/3.0 is interpreted as real division

float (4/3) is interpreted as real division  (“float” forces the variable type)

 

4/3 will give an incorrect answer to the sphere area/volume calculations

 

The bottom line is that Python make up its own mind for variable type based on what it sees. A “4” is clearly an integer, a 4.0 is clearly a decimal or real value. You, the programmer, can force a variable type, as in the case of float (4/3).

 

 

Loop

Can a loop decrement? Yes, as long as the starting value is bigger than the ending value and the “increment” is set to a negative value.

 

You should test this with your While-Loop program.

 

 

 

Homework program: Assignment 2

 

 

 

Homework Assignment

Design a Python program that does the following:

 

 

Capture two runs of the program, one using integral data values, one using real numbers. Hand in the run and suitably commented code.

 

 

Homework Project (handout)

 

Design a Python program that uses functions (see example) to calculate the

of a number supplied by the program user

 

Hints:

cube: either use number*number*number  or the power function:  math.pow(number,3)

cube root: consider the power function: math.pow(number, 1.00/3.00)