Numbers

Python has three numeric types:

  • int
  • float
  • complex

Unless you are doing something specifically mathematical you will only use int and float. So we will cover just these in this lesson.

1. ints and floats

The following table provides a summary of int and float.

Data TypeDescription
intAn integer. There is no upper or lower limit to how high or low it can be (Python 3).
floatA decimal number. A double-precision floating-point number, equivalent to double in many other programming languages.

1.1 int

An int (integer) is simply a whole number such as 5 or -100. If you assign a whole number to a variable, python will automatically know that the type of object is an int.

Type the following two lines in the terminal:

a = 10
type(a)

You will see that the output confirms that a is of type int.

1.2 float

An float (floating point number) is a number containing one or more decimals, such as 5.3 or -100.43. If you assign a decimal number to a variable, python will automatically know that the type of object is an float.

Type the following two lines in the terminal:

a = 10.6
type(a)

You will see that the output confirms that a is of type float.

You can find out the minimum and maximum float using

import sys
print(sys.float_info.min)
print(sys.float_info.max)

On the Repl.it systems it is -2.2250738585072014e+308 to 1.7976931348623157e+308. Which is approximately -2.23*(10**308) to 1.80*(10**308)

That is astronomically big!

1.3 Dynamic Typing

You can combine an int and a float. Python will internally understand to convert the int to a float so that you can combine two floats.

For example, type the following into the terminal,

a = 1 + 3.3
print(a)
type(a)

the first line is adding an int and a float which results in an object of type float with the value 4.3.

2. Arithmetic Operators

Number objects can be combined with the following arithmetic operators.

OperatorNameExample
+Additionx + y
-Subtractionx - y
*Multiplicationx * y
/Divisionx / y
%Modulusx % y
**Exponentiationx ** y
//Floor Divisionx // y

All of +, -, * and / will be familiar to you. However, %, ** and // may not be.

2.0.1 Modulus (%)

The modulus operator is very useful for testing if a whole number is divisible by another whole number. It is an implementation of modulo (clock) arithmetic in mathematics.

If a number is divisible by another the output of x % y will be 0 (x and y can be positive or negative).

10 % 3 # output is 1 (not divisible)
10 % 2 # output is 0 (divisible)
14 % 4 # output is 2 (not divisible)
14 % 7 # output is 0 (divisible)

You will see this in computational mathematics, for more info look at Modulus Operator - Real Python

It can also be used to find the remainder, but be careful, this doesn't work in all circumstances. I would always use math.remainder().

import math # imports extra maths stuff
print(math.remainder(10, 3)) # remainder of 10/3 is 1
print(10 % 3) # output is 1. (% WORKS)

print(math.remainder(-10, 3)) # remainder of 10/3 is 1
print(-10 % 3) # output is 2. (% DOES NOT WORK)

print(math.remainder(10, -3)) # remainder of 10/3 is -1
print(10 % -3) # output is -2. (% DOES NOT WORK)

print(math.remainder(-10, -3)) # remainder of 10/3 is -1
print(-10 % -3) # output is -1. (% WORKS)

So it works when both numbers are positive or both numbers are negative.

My advice is don't use it for the remainder.

2.0.2 Exponentiation (**)

The exponentiation operator ** takes the number x (called the base) to the power of y (called the exponent).

2**3 # Evaluates to 8 (i.e. 2*2*2)
2**4 # Evaluates to 16 (i.e. 2*2*2*2)
5**2 # Evaluates to 25 (i.e. 5*5)
5**3 # Evaluates to 125 (i.e. 5*5*5)

2.0.3 Floor Division (//)

The floor division operator // takes the number x/y and rounds it down.

For example,

10/4 is 2.5 rounded down is 2. So,

10//4 # Evaluates to 2

-10/4 is -2.5 rounded down is -3. So,

-10//4 #Evaluates to -3

2.1 Order of Operations

When using arithmetic operators you need to be aware of the order in which Python evaluates an operator. This is known as the operator precedence.

For example,

3 + 5 * 2

is 16 right? If you enter this into the terminal you will get 13. Well done if you spotted this.

This is because the multiplication * operator has higher precedence than the addition + operator. Python is doing the following

# This is not code, we are manually evaluating to see how Python works with this expression

3 + 5 * 2     # (Evaluate 5 * 2)
3 + 10        # (Evaluate 3 + 10)
13            # (Final Object)

If you want to force the 3 + 5 to be evaluated first, then you need to use parentheses.

(3 + 5) * 2

This now evaluates to 16. Try it in the terminal.

The following table gives the arithmetic operator precedence. Higher entries have higher precedence.

OperatorName
()Parentheses
**Exponentiation
*, /, %, //Multiplication, Division, Modulus, Floor Division
+, -Addition, Subtraction

2.2 Left-to-right Evaluation

You will notice from the table that some operators have the same precedence as each.

What happens then with the following:

5 - 2 + 1

If you try this in the terminal you will get the answer 4. However, this could have been read as either 3 + 1 or 5 - 3 depending on whether you evaluated the - or + first.

Python follows the left-to-right convention. That is, if two operators are of the same precedence, then Python will evaluate the leftmost first. Hence:

# This is not code, we are manually evaluating to see how Python works with this expression

5 - 2 + 1     # (Evaluate 5 - 2)
3 + 1         # (Evaluate 3 + 1)
4             # (Final Object)

3. Comparison Operators

You can also compare two numbers and they will result in a bool object. Either True or False.

OperatorNameExample
==Equalx == y
!=Not equalx != y
>Greater thanx > y
<Less thanx < y
>=Greater than or equal tox >= y
<=Less than or equal tox <= y

For example, the following expressions evaluate to:

ExpressionResult
3 < 5True
3 > 3False
3 >= 3True
3 == 5False
3 != 5True

3.1 Order of Precedence

All the comparison operators given above have lower precedence than the arithmetic operators.

OperatorName
()Parentheses
**Exponentiation
*, /, %, //Multiplication, Division, Modulus, Floor Division
+, -Addition, Subtraction
==, !=, <, >, <=, >=Comparison Operators

Therefore something like the following expression,

3 + 5 == 3 * 5

is evaluated as follows:

# This is not code, we are manually evaluating to see how Python works with this expression
3 + 5 == 3 * 5   # Evaluate 3 + 5
8 == 3 * 5       # Evaluate 3 * 5
8 == 15          # Evaluate 8 == 15
False

Note: I would always want to convey my intention and not rely on the order of precedence, it is easy to forget. So I would rewrite the above as:

(3 + 5) == (3 * 5)

This is identical, but it tells the reader more explicitly that the stuff in the parentheses is evaluated first.


=== TASK ===

Copy the following code into a new Python file.

# DO NOT TOUCH THE FOLLOWING LINES 4, 5 and 6
# THESE ARE USED FOR THE INPUT. 

a = int(input("Please enter a number: ")) # leave this line alone
b = int(input("Please enter a number: ")) # leave this line alone
c = int(input("Please enter a number: ")) # leave this line alone

# TASK 1. 
# print out the type of 10.3 + 5

# TASK 2.
# print(a + b * c) # Correct this so that the ``+`` is evaluated first
# print(a**b+c)    # Correct this so that the exponent is b+c

# TASK 3. 
# print the output of a < b

# TASK 4. 
# print the output of (a < b) == (a <= b)

This reads in three whole numbers and stores them in variables a, b and c. Try running the code and it will ask you for three numbers.

  1. Print the type of 10.3 + 5

  2. Fix the following two lines:

print(a + b * c) # Correct this so that the ``+`` is evaluated first
print(a**b+c)    # Correct this so that the exponent is b+c
  1. Print the result of a < b

  2. Print the result of (a < b) == (a <= b)


If you enter 2, 3 and 2 into your program then a=2, b=3 and c=2. If you have made the above changes correctly, your program should output:

<class 'float'>
10
32
True
True

References

W3schools Python Operators

Precedence and Associativity of Operators in Python