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'>