Chapter 3 - Number Types

The last chapter mentioned two different kinds of numbers: ints and floats. This chapter is going to discuss the differences between the two types and what you as a programmer can do using number types.

Number Operations

Computers carry out commands by writing data to memory, reading data from memory, and performing calculations on that data. We have gone over how to write data to memory by defining variables, but we have yet to learn how to perform calculations on those numbers stored in memory. The type of the variable determines what kinds of calculations can be performed on it, and this section discusses the types of calculations that can be performed on number types. The table below shows a list of common operations on two numbers x and y.

Common Number Operations
OperationResult
x + ySum (addition)
x - yDifference (subtraction)
x * yProduct (multiplication)
x / yQuotient (division)
x // yQuotient (division) rounded down to whole number
x % yRemainder of x / y (e.g., 19 % 5 is 4)
abs(x)Absolute value of x
pow(x, y)Exponent (x to the power y)
x ** yExponent (x to the power y)

Use the exercise below to play with these operations. Change the values of x and y to different integer and float values to see what difference it makes. (Note: If you set y as 0, you will get a division by 0 error.)

# Change the values of x and y x = 19 y = 5 # This code performs the operations num_sum = x + y diff = x - y prod = x * y quot = x / y quot_int = x // y rem = x % y abs_val = abs(x) exp = x ** y # This prints all the results print("") print("x = " + str(x) + ", y = " + str(y)) print(str(x) + " + " + str(y) + " = " + str(num_sum)) print(str(x) + " - " + str(y) + " = " + str(diff)) print(str(x) + " * " + str(y) + " = " + str(prod)) print(str(x) + " / " + str(y) + " = " + str(quot)) print(str(x) + " // " + str(y) + " = " + str(quot_int)) print(str(x) + " % " + str(y) + " = " + str(rem)) print("abs(" + str(x) + ") = " + str(abs_val)) print(str(x) + " ** " + str(y) + " = " + str(exp))

Being Careful with Types

For some versions of Python and for most programming languages, the types of the operands (the numbers operated on in math operations) will determine the type of the resulting value. For example, many programing languages will evaluate 12 / 5 as 2 instead of 2.4. Why? Because 12 is an integer and 5 is an integer, so the computer thinks that an operation with only integers (such as 12 / 5) should result in an integer, in this case 2. If you know you want the output of a math operation to be a float, then it is a good idea to make at least one of the operands a float. For example, 12.0 / 5.0 will evaluate to 2.4, as will 12.0 / 5 or 12 / 5.0. When at least one operand is a float, the computer knows that the result may have a decimal value, so the computer automatically makes the result a float so that the decimal isn't lost.

Mathematical Equations

Using number types and operations, you can code a mathematical equation. An equation performs operations on operands to produce a result. The coding activity above codes very basic equations: addition, subtraction, multiplication, etc. You can combine these basic operations to code more complex equations, such as calculating the area of a circle from its radius or calculating a Fahrenheit temperature given a Celsius value. Use the coding exercise below to play with some of these mathematical equations.

# Area of a circle radius = 2.5 # You can change the radius pi = 3.14159 area = pi * (radius ** 2) print("Radius: " + str(radius) + " m, Area: " + str(area) + " m^2") # Celsius to Fahrenheit converter celsius = 35.0 # You can change the celsius value fahrenheit = (9/5.0) * celsius + 32 print("Celsius: " + str(celsius) + ", Fahrenheit: " + str(fahrenheit)) # Calculate distance (you can change the first 3 values) velocity_start = 2.0 # meters per second time = 10.0 # seconds acceleration = 1.5 # meters per second per second distance = (velocity_start * time) + (0.5 * acceleration * (time**2)) print("Distance travelled: " + str(distance) + " meters") # Compare price per square inch of two pizzas (you can change diameter and cost) diameter1 = 12.0 # inches radius1 = diameter1 / 2.0 cost1 = 5.00 # dollars area1 = pi * (radius1 ** 2) cost_ratio1 = cost1 / area1 print("Size: " + str(diameter1) + " in, Cost: $" + str(cost1) + ", Cost per square inch: $" + str(cost_ratio1)) diameter2 = 16.0 # inches radius2 = diameter2 / 2.0 cost2 = 7.25 # dollars area2 = pi * (radius2 ** 2) cost_ratio2 = cost2 / area2 print("Size: " + str(diameter2) + " in, Cost: $" + str(cost2) + ", Cost per square inch: $" + str(cost_ratio2))

Now that you have seen some examples of writing equations, it's time to do one on your own! Use the coding practice below to write an equation that converts a Fahrenheit temperature to a Celsius temperature. Use the following formula:

Where C is degrees Celsius and F is degrees Fahrenheit. First subtract 32 from the Fahrenheit value, then multiply the result by the fraction 5/9.

# You can change the Fahrenheit input value f = 95 # Change this line to match the conversion equation: c = f # Print solution print("F: " + str(f) + ", C: " + str(c)) f = 95 c = (5.0/9.0) * (f - 32) print("F: " + str(f) + ", C: " + str(c))
c = (5.0/9.0) * (f - 32)