Skip to content

Python Numbers

Numbers in Python can be of 3 types: int, float and complex.

Integer numbers

Integer numbers are represented using the int class. You can define an integer using a value literal:

age = 8

You can also define an integer number using the int() constructor:

age = int(8)

To check if a variable is of type int, you can use the type() global function:

type(age) == int #True

Floating point numbers

Floating point numbers (fractions) are of type float. You can define an integer using a value literal:

fraction = 0.1

Or using the float() constructor:

fraction = float(0.1)

To check if a variable is of type float, you can use the type() global function:

type(fraction) == float #True

Complex numbers

Complex numbers are of type complex.

You can define them using a value literal:

complexNumber = 2+3j

or using the complex() constructor:

complexNumber = complex(2, 3)

Once you have a complex number, you can get its real and imaginary part:

complexNumber.real #2.0
complexNumber.imag #3.0

Again, to check if a variable is of type complex, you can use the type() global function:

type(complexNumber) == complex #True

Arithmetic operations on numbers

You can perform arithmetic operations on numbers, using the arithmetic operators: +, -, *, / (division), % (remainder), ** (exponentiation) and // (floor division):

1 + 1 #2
2 - 1 #1
2 * 2 #4
4 / 2 #2
4 % 3 #1
4 ** 2 #16
4 // 2 #2

and you can use the compound assignment operators

to quickly perform operations on variables, too:

age = 8
age += 1

Built-in Functions

There are 2 built-in functions that help with numbers:

abs() returns the absolute value of a number.

round() given a number, returns its value rounded to the nearest integer:

round(0.12) #0

You can specify a second parameter to set the decimal points precision:

round(0.12, 1) #0.1

Several other math utility functions and constants are provided by the Python standard library:

We’ll explore some of those separately later on.


→ Get my Python Handbook
→ Get my Python Handbook

download all my books for free

  • javascript handbook
  • typescript handbook
  • css handbook
  • node.js handbook
  • astro handbook
  • html handbook
  • next.js pages router handbook
  • alpine.js handbook
  • htmx handbook
  • react handbook
  • sql handbook
  • git cheat sheet
  • laravel handbook
  • express handbook
  • swift handbook
  • go handbook
  • php handbook
  • python handbook
  • cli handbook
  • c handbook

subscribe to my newsletter to get them

Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing [email protected]. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.

Related posts about python: