2.1. Working with Numeric Data#

Amanda R. Kube Jotte

Python has several different data types, which are just different kinds of values that Python can work with. Some of the most common built-in data types are integers, floats, booleans, and strings.

In the next few sections, we’ll explore these data types one by one and learn some of the operations we can do with them. Later, we’ll also see how to use functions and methods to perform more complex calculations.

We’ll begin with the numeric data types: integers and floats.

Integers & Floats#

Integers are whole numbers (like 3, -42, or 0).
Floats are numbers with decimal points (like 3.14 or -0.001).

Floats can represent very precise values (up to about 15–17 decimal digits), which makes them useful for calculations that require accuracy. However, working with integers is usually a little simpler and faster for the computer.

Both data types are widely used, and choosing between them depends on what you’re trying to calculate. Let’s take a closer look at how they work in Python.

print(4567)
print(45.67)
4567
45.67

Python includes many built-in functions—little tools that perform specific tasks. For now, all you need to know is:

  • Functions are written with a name followed by parentheses.

  • You put values inside the parentheses as inputs (also called arguments).

  • The function gives you back an output.

We’ll learn much more about functions in Section 3.5.

One useful built-in function is type(), which tells us what kind of data (data type) a value is:

type(3)
int
type(3.14)
float

Tip

Don’t worry if functions still feel new.

At this point, all you need to know is how to call them: write the function name, add parentheses, and put any values you want to check inside. We’ll go into more detail on how functions work later.

Python also lets us convert between numeric data types using built-in functions.

  • The float() function turns a value into a float (a decimal number).

  • The int() function turns a value into an integer (a whole number).

Let’s see what happens when we convert the integer 4567 to a float, and the float 45.67 to an integer:

float(4567)
4567.0
int(45.67)
45

We can see that the conversion of an integer to a float simply adds one significant figure after the decimal place. When you convert a float to an integer, Python does not round the number. It simply removes (truncates) the decimal part.

For example:

int(9.99)
9

Commenting Code#

Sometimes, it is useful to write notes to ourselves when we are writing code. We call these notes comments. The comment character # signals to Python that anything written after it should be ignored. For example, the text below is a comment.

# This is a comment

Typically, we use comments to explain what is happening in code, especially when it’s complex. Comments are helpful both when sharing code with others and when revisiting your own code later—you won’t have to wonder “What was I doing here?” In this book, we’ll also use comments to add explanations inside code cells.

# The code below displays the data type of 12.34 which should be a float
type(12.34)
float
int(12.34) # This code truncates 12.34, removing the .34
12

If you put # in front of a piece of code, it will turn that code into a comment. Which means it will be ignored by Python. Turning code into a comment like this is called commenting out. This is especially useful if you want to keep a line of code around without actually running it.

# This will not display any result below this 
# code cell because Python will not run it

# int(12.34)

Operations#

To work with numeric data in Python, we need to know the basic mathematical operators. In this section, we will see how to use Python to perform mathematical operations such as addition, subtraction, multiplication, division, and exponentiation.

Addition and Subtraction#

Addition and subtraction are relatively self explanatory in Python. We use the operators + and - as we would on a calculator. For example, we can type 3 + 4 in a Jupyter notebook cell and run it to output the solution, 7, as follows:

3 + 4
7

We can do the same for subtraction, subtracting six from ten to get four.

10 - 6 
4

Just as you learned in elementary school, in Python subtraction is the same as addition of a negative number.

10 + -6
4

We can create longer expressions as well.

2 + 5 - 4 + 10 + 2 # This is evaluated from left to right
15

Multiplication and Division#

The operator for multiplication in Python is *, which we can use as follows.

3 * 6
18
5 * -7
-35

There are multiple types of division in Python. We can use / for normal division which will result in a number with a decimal (which you may recall is called a float). For example:

7 / 2
3.5

Notice that, even when a number divides evenly, the result is still a float - with just a zero after the decimal point.

6 / 2
3.0

We can also do floor division using // which is equivalent to dividing and then taking the floor of the result (rounding down to the nearest whole number). Floor division will always result in an integer (a number without a decimal).

7 // 2
3

We can also use the modulo operator % to get the remainder after division. Using the same example as above. We know that two goes into seven three times leaving a remainder of one. Therefore, seven modulo two gives the result 1.

7 % 2
1

Let’s try another example.

13 / 5
2.6

Thirteen divided by five is 2.6. The floor division of thirteen by five is two as five goes into thirteen two times.

13 // 5
2

And thirteen modulo five is three as three is the remainder.

13 % 5
3

Exponentiation#

We can also compute exponents using Python. The operator for this is **. This is an example of a time where syntax, or the arrangement of letters and symbols, is important. Adding a space as in * * will not work!

2 ** 3
8
2 * * 3
  Cell In[25], line 1
    2 * * 3
        ^
SyntaxError: invalid syntax

The expression 2 * * 3 produces an error as Python does not recognize that you meant **.

To take the root of a number in Python, we simply exponentiate by the reciprocal. For example, to take the square root of four or the cubed root of eight, we would do the following.

4 ** (1/2)
2.0
8 ** (1/3)
2.0

Order of Operations#

Notice that we had to use parentheses above. If we would not have included those parentheses, we would have gotten a different result. For example:

8 ** 1/3
2.6666666666666665

Python follows the same order of operations that you learned in elementary school. You might remember learning a mnemonic phrase such as ‘Please Excuse My Dear Aunt Sally’ to remember in what order to execute operations: Parentheses, Exponentiation, Multiplication and Division, Addition and Subtraction. In the previous code cell, Python computed exponentiation first, computing eight to the first power, then divided by three. Using parentheses not only avoids this issue, but makes your code easier to read as well! For example, these give the same result, but the second is much more clear.

5 + 10 ** 2 - 5 // 2 * 4
97
5 + (10 ** 2) - ((5 // 2) * 4)
97

Below is a table of summarizing the operators we learned in this chapter.

Operator

Used For

Example Expression

Result

+

Addition

5 + 2

7

-

Subtraction

5 - 2

3

*

Multiplication

5 * 2

10

/

Division

5 / 2

2.5

//

Floor Division

5 // 2

2

%

Modulo

5 % 2

1

**

Exponentiation

5 ** 2

25

Each of these operators can act on numeric data types (integers or floats). In the next sections, we will discuss non-numeric data types and how to work with them in Python.