Python, like most programming languages, has built-in numeric data types. Integers and floats are the two common numeric types in Python. Python supports other types of numbers such as Decimal, Fraction, and complex numbers. But in this mini-tutorial, we’ll focus on learning about integers and floats.

Number values help us represent all kinds of information. Numbers can be data from a visualization, the score in a video game, web application data, and so on. Let’s take a look at the operators available for numeric data types.

Python supports the order of operations. Parentheses can be used to alter the order of operations. The following table lists all the math operators in Python from highest to lowest precedence.

OperatorOperationExampleResult
**Exponent2 ** 24
%Modulus/Remainder10 % 33
//Integer Division18 // 36
/Division100 / 254
*Multiplication2 * 36
-Subtraction9 - 54
+Addition4+ 48

Here are some expressions using the operators:

>>> 2 - 10 + 32
24

>>> (3 * (4 - 2)) / 1
6.0

>>> 2 ** 5
32

Integers

Integers are whole numbers. They have no decimal or fractional part. Integers include both negative and positive whole numbers, and the number zero. Examples of integers are: 0, -1, -199, 2, 3, 99, -2.

In Python, an integer has type int. Take careful notice when using the / and // operators. The // operator should be used when you want an integer result. Here are examples of expressions involving integers:

>>> 22 // 9
2

>>> x = 8 % 6
2

>>> y = 9 * x
9

Floats

Floats are numbers with a decimal point, such as 2.71828. Float is short for floating-point number. This term is used in other programming languages. Examples of floats are: -1.0, 3.14, 0.3333333, 0.25, 0.0.

Floats have type float. If an expression contains mixed type operands, then the result will always be a float. Here are examples to illustrate:

>>> 2.0 + 4.2
6.2

>>> 1 + 2 + 3 + 4.0
10.0

>>> 0.0 + 100
100.0

Miscellaneous Tidbits

Built-in Functions

The int() and float() are built-in functions for converting a number or string into the respective type.

>>> int("22")
22

>>> int(3.14)
3

>>> float("2.7")
2.7

String Concatenation

If you try to concatenate a number to a string, the following error will occur:

>>> "Hello " + 20
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str

The number needs to be explicitly converted to a string because Python doesn’t do this automatically—unlike other programming languages like Java or JavaScript.

Infinity

Use the math module or float() for infinity or negative infinity.

>>> import math
>>> math.inf > 9999
True

>>> float("-inf") < 0
True

Useful Packages

Here are some numeric and mathematical related packages that every Python developer should know:

Summary

This mini-tutorial should give you a better understanding of the int and float types in Python. Floats should be used when precision is an absolute concern. Integers, however, are used when the fractional part is not needed for computation or manipulation.

Here are additional resources to help you get started learning Python:

Python Challenges:

  1. Implement a script that adds three integers and returns the floating-point representation
  2. Implement a script that takes in a user’s numeric input from the command line and prints the integer representation
  3. Implement a number guessing game

Thanks for reading! Let me know what you think of this post on Twitter.