Cover page images (keyboard)

Computing with Numbers

Sue Evans & Travis Mayberry

Adapted from the CS1 Course at Swarthmore College by Lisa Meeden

Hit the space bar for next slide

Learning Outcomes

Number Basics

Bases

Bases

Python Operators

Operation Python Operator
Addition
+
Subtraction
-
Multiplication
*
Division
/
Exponentiation
**
Modulus
%

Integers vs. Floats

Integers Floats
Truncates floating point results Allows floating point results
Always has exact values Sometimes has approximations of values
Used for things that are countable Used for continuous data
Faster arithmetic operations Slower arithmetic operations
Stored in 32 bits (4 bytes)
one bit holds the sign and
31 bits store the number
Stored in 64 bits (8 bytes)
one bit holds the sign, 11 bits hold the exponent
and 52 bits hold the mantissa
Largest possible int is 2147483647 N/A, since numbers are stored in exponential form

Many languages are difficult to use when a program requires working with integers that are larger than 2147483647. Since a different data type has to be used to hold the values, the code has to be rewritten. Python handles this problem for you by seamlessly continuing to run the program by switching the data type to long ints automatically.

The third numeric type in Python is the long int. A long int doesn't have a fixed size. It expands to the length the number needs, limited only by the amount of memory your computer has.

Working with Integers:

>>> 7 / 3
2

Both the dividend and the divisor are integers, causing the integer arithmetic unit to do the calculation. The integer arithmetic unit can take only integers as input and can produce only integer results. This is called integer division and it can be quite useful, as in the following example.

>>> length = input("Enter a length in inches : ")
Enter a length in inches : 27
>>> feet  = length / 12
>>> inches = length % 12
>>> print length, "inches is ", feet, "feet and ", inches, "inches"
27 inches is  2 feet and  3 inches

Working with Floating-point Numbers

>>> 7.0 / 3.0
2.3333333333333335

Here the floating-point arithmetic unit had to be used for the calculation. Floats are not always exact answers. We all know the last digit here should be a 3, not a 5. If the number is a repeating decimal, or if it has more decimal places than there is space, the value may be slightly off or truncated.

Types

How can you tell what type of number you are using?

>>> type(1)
<type 'int'>
>>> type(1.0)
<type 'float'>
>>> type(1.0 + 2)
<type 'float'>
>>> myNum = 4
>>> type(myNum)
<type 'int'>
>>> myNum = myNum + 2.0
>>> type(myNum)
<type 'float'>

Notice that numbers are automatically converted to floats if an operation contains both integers and floats. Explicit conversion can be done with float()

>>> type(float(1))
<type 'float'>
>>> float(7)/3
2.3333333333333335

Math Library

The math library contains many useful math functions. You must have 'import math' in your program to use the functions in the math library.

Function Purpose
cos(x),sin(x),tan(x) Trigonometric functions
log(x,base) Logarithm of x with given base
floor(x) Floor function (closest integer less than or equal to x)
ceil(x) Ceiling function (closest integer greater than or equal to x)
sqrt(x) Square root of x
pi Constant that represents pi
e Constant that represents e

More information can be found on the official Python documentation page.

Exercise

How would you write an algorithm to find the average of some numbers entered by the user?