site logo

Learn to Python. Build the Future.


Category: (All)
❮  Go Back

08. Exploring Python Numbers: Integers, Floats, Complex & Conversions

Introduction

Numbers play a critical role in programming: mathematics, data processing, financial calculations, simulations, and more all depend on numeric types. Python supports several numeric types, and understanding them plus how to convert between them is essential for writing robust code. In this article, we will cover Python’s built-in numeric types—int, float, and complex—how to use them, and how to convert between types. We also touch on generating random numbers.


Numeric Types in Python

Python has three main numeric types:

  1. int — integer numbers (whole numbers)
  2. float — floating point numbers (numbers with decimals)
  3. complex — complex numbers (with a real and imaginary part)

When you assign a numeric value to a variable, Python automatically chooses the correct type.

Example:

x = 1 # int
y = 2.8 # float
z = 1j # complex

You can check the type of any variable using the type() function:

print(type(x))
print(type(y))
print(type(z))


Integer (int)

An integer is a whole number (positive or negative) without decimals. In Python, integers can be of any length (limited by available memory).

Examples:

x = 1
y = 35656222554887711
z = -3255522
print(type(x))
print(type(y))
print(type(z))

These will all show <class 'int'>.

Integers are often used for counting, indexing, loops, and situations where you need exact whole values.


Floating Point (float)

A float (floating point number) is a number that has a decimal point. It can represent fractional parts.

Examples:

x = 1.10
y = 1.0
z = -35.59
print(type(x))
print(type(y))
print(type(z))

You can also represent floats using scientific notation, using e or E to denote powers of 10:

x = 35e3 # 35000.0
y = 12E4 # 120000.0
z = -87.7e100
print(type(x))
print(type(y))
print(type(z))

All of those are float values.

Note: Floating point arithmetic can introduce rounding errors due to how these numbers are stored in memory.


Complex Numbers (complex)

A complex number combines a real part and an imaginary part. In Python, the imaginary part is denoted with a j.

Examples:

x = 3 + 5j
y = 5j
z = -5j
print(type(x))
print(type(y))
print(type(z))

These will show <class 'complex'>.

Complex numbers are useful in fields like engineering, physics, or when dealing with wave equations and Fourier transforms.


Type Conversion (Casting)

Python allows you to convert from one numeric type to another using built-in functions:

  1. int() — convert to integer
  2. float() — convert to floating point
  3. complex() — convert to complex

Example conversions:

x = 1 # int
y = 2.8 # float
z = 1j # complex

a = float(x) # convert int to float → 1.0
b = int(y) # convert float to int → 2
c = complex(x) # convert int to complex → (1+0j)

print(a, type(a))
print(b, type(b))
print(c, type(c))

Be careful: converting floats to ints truncates the decimal part (no rounding). Also, you cannot convert a complex number directly to a float or integer.


Generating Random Numbers

Python does not provide a built-in random() function at the top level, but you can use the random module to generate random numbers.

Example:


import random
print(random.randrange(1, 10))

This outputs a random integer between 1 and 9 (inclusive of 1, exclusive of 10).

The random module includes many functions for random floats, choosing random elements from lists, shuffling, and more.


Best Practices & Tips

  1. Use int when you need whole numbers (counts, indices, flags).
  2. Use float if fractional values or decimals are needed.
  3. Use complex if you explicitly need imaginary components.
  4. When converting, make sure the conversion is meaningful and safe (e.g. don’t convert a float with decimals you need into an int unintentionally).
  5. Use the random module rather than expecting built-in top-level random functions.
  6. Always check types with type() when debugging or when you are not sure what type a variable is.


Summary

Python offers three numeric types: int, float, and complex. You can freely convert between compatible types using casting functions, and use the random module to generate random numbers. Understanding these types and how to use them is foundational for more advanced programming tasks.

08. Exploring Python Numbers Integers Floats Complex Conversions

coldshadow44 on 2025-10-08



(0)





Showing comments related to this post.




Member's Sites: