Python Numbers (Integers, Floats, and Complex)

What Are Python Numbers?

Python numbers are data types that store numeric values. They let you represent and work with mathematical quantities in your code, whether you're performing calculations, analyzing data, or building scientific applications.

For example, you might store someone's age as 30, a product price as 19.99, or a voltage value for an electrical circuit.

Python has three distinct numeric types to handle different mathematical needs:

  • Integers (int): Whole numbers like 10, -5, or 0
  • Floating-Point Numbers (float): Numbers with decimals like 3.14 or -0.5
  • Complex Numbers (complex): Numbers with real and imaginary parts like 3 + 4j

Want to master Python fundamentals? This lesson is part of our Free Python course covering variables, data types, numbers, functions, and more.

Explore this free Python course →

Integers (int)

An integer is a whole number that can be positive, negative, or zero. Integers don't have a fractional part. Unlike some programming languages, Python integers can be any size - they're limited only by your computer's available memory.

Example: Creating Integers

You create an integer by assigning a whole number to a variable. Try running the code below.

# Positive integer student_count = 1500 # Negative integer account_balance = -250 # Zero start_value = 0 print(student_count) print(account_balance)
# Positive integer
student_count = 1500
account_balance = -250
start_value = 0

print(student_count)
print(account_balance)
Your output will appear here...

Common Integer Operations

Integers support all standard arithmetic operations. Two especially useful operators are integer division (//) and modulo (%):

  • Addition (+): Adds two numbers
  • Subtraction (-): Subtracts one number from another
  • Multiplication (*): Multiplies two numbers
  • Integer Division (//): Divides and returns only the whole number, discarding any remainder
  • Modulo (%): Divides and returns just the remainder

Example: Division vs Modulo

a = 10 b = 3 # Integer Division (Floor division) print("Division Result:", a // b) # Modulo (Remainder) print("Remainder:", a % b)
a = 10
b = 3

print(a // b)
print(a % b)
Your output will appear here...

Floating-Point Numbers (float)

A float is a number with a decimal point that represents real numbers. Floats help you work with fractional values when you need precise calculations. You can use them to accurately represent quantities like measurements, prices, or scientific constants - for example, pi = 3.14159 or temperature = 98.6.

You can also express very large or small numbers using scientific notation with "e" or "E". For instance, 1.5e6 equals 1,500,000.

Example: Working with Floats

# Standard float price = 49.95 # Negative float temperature = -4.5 # Float with scientific notation (2.99 x 10^8) speed_of_light = 2.99e8 print(price) print(speed_of_light)
price = 49.95
temperature = -4.5
speed_of_light = 2.99e8 

print(price)
print(speed_of_light)
Your output will appear here...
Understanding Float Precision: Floats can sometimes have small precision errors because of how computers store these numbers internally. For example, 0.1 + 0.2 doesn't give exactly 0.3, but rather a very close approximation. If you need high-precision calculations for financial work, Python's Decimal module is a better choice.

You're making great progress! Ready to tackle more Python challenges with real-world exercises?

Python Exercises

Complex Numbers (complex)

A complex number has both a real and an imaginary part, written as a + bj. While they're less common in general programming, complex numbers are essential in many scientific and engineering fields. You might use them in electrical engineering to describe circuit impedance or in physics to represent wave functions.

You can create a complex number by writing it directly or using the complex() function. The imaginary part is marked with the suffix j or J.

Example: Creating and Accessing Complex Numbers

Use the .real and .imag attributes to access specific parts of the number.

# Direct creation c1 = 3 + 5j # Using the complex() function c2 = complex(3, 5) print("Complex Number:", c1) # Accessing parts z = 4 - 6j print("Real part:", z.real) print("Imaginary part:", z.imag)
c1 = 3 + 5j
z = 4 - 6j

print(c1)
print(z.real)
print(z.imag)
Your output will appear here...

Type Conversion in Numbers

Python lets you convert numbers from one type to another using built-in functions - a process called type casting. The most common conversion functions are int(), float(), and complex().

1. Convert to Integer with int()

The int() function converts a float or compatible string to an integer. When you convert a float to an integer, Python truncates the number, meaning it cuts off the decimal part without rounding.

value = 10.7 int_value = int(value) print(int_value)
value = 10.7
int_value = int(value)
print(int_value)
Your output will appear here...

2. Convert to Float with float()

The float() function converts an integer or compatible string to a floating-point number.

count = 25 float_count = float(count) print(float_count)
count = 25
float_count = float(count)
print(float_count)
Your output will appear here...

3. Convert to Complex with complex()

The complex() function creates a complex number from other number types.

real_part = 7 imag_part = 2 complex_number = complex(real_part, imag_part) print(complex_number)
real_part = 7
imag_part = 2
complex_number = complex(real_part, imag_part)
print(complex_number)
Your output will appear here...

Final Challenge: Number Mixer

Test your understanding of Python numbers with this challenge.

Your Task:

  1. Create a float variable named score with the value 95.8.
  2. Convert score to an integer and store it in a variable named final_grade.
  3. Print the final_grade to see how Python truncates the decimal.
  4. Check the data type of final_grade using type().
# TODO: Create the score variable # score = ... # TODO: Convert score to int # final_grade = ... # TODO: Print final_grade and its type
score = 95.8
final_grade = int(score)

print(final_grade)
print(type(final_grade))
Your output will appear here...
🏆

Lesson Completed

You have successfully learned about Python Integers, Floats, Complex numbers, and how to convert between them.

📘

Full Python Course

Master Python with 11+ hours of content, 50+ exercises, and real-world projects.

Enroll Now
📝

Next Lesson

Continue with the next lesson on Python Strings.

Next Lesson ->
Scroll to Top