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
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
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
price = 49.95
temperature = -4.5
speed_of_light = 2.99e8
print(price)
print(speed_of_light)
Your output will appear here...
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 ExercisesComplex 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.
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)
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)
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)
Your output will appear here...
Final Challenge: Number Mixer
Test your understanding of Python numbers with this challenge.
Your Task:
- Create a float variable named
scorewith the value95.8. - Convert
scoreto an integer and store it in a variable namedfinal_grade. - Print the
final_gradeto see how Python truncates the decimal. - Check the data type of
final_gradeusingtype().
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