You don’t need to be a math whiz to program well. The truth is, few programmers need to know more than basic algebra. Of course, how much math you need to know depends on the application you’re working on. In general, the level of math required to be a programmer is lower than you might expect. Although math and computer programming aren’t as correlated as some people might believe, numbers are an integral part of any programming language, and Python is no exception.
In this tutorial, you’ll learn how to:
- Create integers and floating-point numbers
- Round numbers to a given number of decimal places
- Format and display numbers in strings
Let’s get started!
Note: This tutorial is adapted from the chapter “Numbers and Math” in Python Basics: A Practical Introduction to Python 3. If you’d prefer a video course, then check out Python Basics: Numbers and Math.
The book uses Python’s built-in IDLE editor to create and edit Python files and interact with the Python shell, so you will see references to IDLE’s built-in debugging tools throughout this tutorial. However, you should have no problems running the example code from the editor and environment of your choice.
Free Bonus: 5 Thoughts On Python Mastery, a free course for Python developers that shows you the roadmap and the mindset you’ll need to take your Python skills to the next level.
Integers and Floating-Point Numbers
Python has three built-in numeric data types: integers, floating-point numbers, and complex numbers. In this section, you’ll learn about integers and floating-point numbers, which are the two most commonly used number types. You’ll learn about complex numbers in a later section.
Integers
An integer is a whole number with no decimal places. For example, 1 is an integer, but 1.0 isn’t. The name for the integer data type is int, which you can see with type():
>>> type(1)
<class 'int'>
You can create an integer by typing the desired number. For instance, the following assigns the integer 25 to the variable num:
>>> num = 25
When you create an integer like this, the value 25 is called an integer literal because the integer is literally typed into the code.
You may already be familiar with how to convert a string containing an integer to a number using int(), which is especially useful when working with user input. For example, the following converts the string "25" to the integer 25:
>>> int("25")
25
int("25") is not an integer literal because the integer value is created from a string.
When you write large numbers by hand, you typically group digits into groups of three separated by a comma or a decimal point. The number 1,000,000 is a lot easier to read than 1000000.
In Python, you can’t use commas to group digits in integer literals, but you can use underscores (_). Both of the following are valid ways to represent the number one million as an integer literal:
>>> 1000000
1000000
>>> 1_000_000
1000000
There’s no limit to how large an integer can be, which might be surprising considering that computers have a finite amount of memory. Try typing the largest number you can think of into IDLE’s interactive window. Python can handle it with no problem!
Floating-Point Numbers
A floating-point number, or float for short, is a number with a decimal place. 1.0 is a floating-point number, as is -2.75. The name of the floating-point data type is float:
>>> type(1.0)
<class 'float'>
Like integers, floats can be created from floating-point literals or by converting a string to a float with float():
>>> float("1.25")
1.25
There are three ways to represent a floating-point literal. Each of the following creates a floating-point literal with a value of one million:
>>> 1000000.0
1000000.0
>>> 1_000_000.0
1000000.0
>>> 1e6
1000000.0
The first two ways are similar to the two techniques for creating integer literals. The third approach uses E notation to create a float literal.
Note: E notation is short for exponential notation. You may have seen this notation used by calculators to represent numbers that are too big to fit on the screen.
To write a float literal in E notation, type a number followed by the letter e and then another number. Python takes the number to the left of the e and multiplies it by 10 raised to the power of the number after the e. So 1e6 is equivalent to 1×10⁶.
Python also uses E notation to display large floating-point numbers:
>>> 200000000000000000.0
2e+17
The float 200000000000000000.0 gets displayed as 2e+17. The + sign indicates that the exponent 17 is a positive number. You can also use negative numbers as the exponent:
>>> 1e-4
0.0001
The literal 1e-4 is interpreted as 10 raised to the power -4, which is 1/10000, or 0.0001.
Unlike integers, floats do have a maximum size. The maximum floating-point number depends on your system, but something like 2e400 ought to be well beyond most machines’ capabilities. 2e400 is 2×10⁴⁰⁰, which is far more than the total number of atoms in the universe!
When you reach the maximum floating-point number, Python returns a special float value, inf:
>>> 2e400
inf
inf stands for infinity, and it just means that the number you’ve tried to create is beyond the maximum floating-point value allowed on your computer. The type of inf is still float:
>>> n = 2e400
>>> n
inf
>>> type(n)
<class 'float'>
Python also uses -inf, which stands for negative infinity and represents a negative floating-point number that is beyond the minimum floating-point number allowed on your computer:
>>> -2e400
-inf
You probably won’t come across inf and -inf very often as a programmer unless you regularly work with extremely large numbers.
Check Your Understanding
Expand the block below to check your understanding:
Write a program that creates two variables, num1 and num2. Both num1 and num2 should be assigned the integer literal 25000000, one written with underscores and one without. Print num1 and num2 on two separate lines.
You can expand the block below to see a solution:
First, assign the value 25000000 to num1 without any underscores:
num1 = 25000000
Next, on a new line, assign the value 25_000_000 to the variable num2:
num2 = 25_000_000
Print both variables on separate lines by passing each variable to separate calls of print():
print(num1)
print(num2)
In the output, you can see that both numbers are the same:
25000000
25000000
Although both variables are assigned the value 25000000, writing the value using underscores to group digits makes it much easier for a human to quickly work out what the number is. No more squinting at the screen and trying to count zeros!
When you’re ready, you can move on to the next section.
Arithmetic Operators and Expressions
In this section, you’ll learn how to do basic arithmetic, such as addition, subtraction, multiplication, and division, with numbers in Python. Along the way, you’ll learn some conventions for writing mathematical expressions in code.
Addition
Addition is performed with the + operator:
>>> 1 + 2
3
The two numbers on either side of the + operator are called operands. In the above example, both operands are integers, but operands don’t need to be the same type.
You can add an int to a float with no problem:
>>> 1.0 + 2
3.0
Notice that the result of 1.0 + 2 is 3.0, which is a float. Anytime a float is added to a number, the result is another float. Adding two integers together always results in an int.
Note: PEP 8 recommends separating both operands from an operator with a space.
Python can evaluate 1+1 just fine, but 1 + 1 is the preferred format because it’s generally easier to read. This rule of thumb applies to all the operators in this section.
Subtraction
To subtract two numbers, just put a - operator between them:
>>> 1 - 1
0
>>> 5.0 - 3
2.0
Just like adding two integers, subtracting two integers always results in an int. Whenever one of the operands is a float, the result is also a float.
The - operator is also used to denote negative numbers:
>>> -3
-3
You can subtract a negative number from another number, but as you can see below, this can sometimes look confusing:
>>> 1 - -3
4
>>> 1 --3
4
>>> 1- -3
4
>>> 1--3
4
Of the four examples above, the first is the most PEP 8 compliant. That said, you can surround -3 with parentheses to make it even clearer that the second - is modifying 3:
>>> 1 - (-3)
4
Using parentheses is a good idea because it makes your code more explicit. Computers execute code, but humans read code. Anything you can do to make your code easier to read and understand is a good thing.
Multiplication
To multiply two numbers, use the * operator:
>>> 3 * 3
9
>>> 2 * 8.0
16.0
The type of number you get from multiplication follows the same rules as addition and subtraction. Multiplying two integers results in an int, and multiplying a number with a float results in a float.
Division
The / operator is used to divide two numbers:
>>> 9 / 3
3.0
>>> 5.0 / 2
2.5
Unlike addition, subtraction, and multiplication, division with the / operator always returns a float. If you want to make sure that you get an integer after dividing two numbers, you can use int() to convert the result: