Numbers are one of the most commonly used data types in Python. Whether you are calculating a student’s marks, storing a person’s age, measuring temperature, or performing mathematical calculations, you will use numbers in almost every Python program.
Python provides built-in support for different types of numbers, allowing programmers to perform simple arithmetic as well as advanced mathematical operations without installing any additional libraries.
Unlike many programming languages, Python automatically determines the type of a number when you assign it to a variable. This makes Python easier to learn and helps beginners focus on solving problems instead of worrying about data type declarations.
Python has three built-in numeric data types:
int) – Whole numbers without decimal points.float) – Numbers containing decimal points.complex) – Numbers used in scientific and engineering calculations.In this lesson, you will learn each numeric data type, understand when to use it, and practice working with numbers through simple examples.
After completing this lesson, you will be able to:
int, float, and complex.type() function.In Python, numbers are objects that represent numeric values. Every number belongs to one of Python’s built-in numeric data types.
| Data Type | Description | Example |
|---|---|---|
int |
Whole numbers | 25, -18, 5000 |
float |
Decimal numbers | 3.14, 99.99, -5.5 |
complex |
Complex numbers | 3+5j, 2-4j |
Python automatically detects the numeric data type when you assign a value to a variable.
age = 25
price = 99.95
number = 4 + 6j
print(type(age))
print(type(price))
print(type(number))
Output
<class 'int'>
<class 'float'>
<class 'complex'>
The type() function returns the data type of a variable or value.
int)An integer is a whole number that does not contain a decimal point. Integers can be positive, negative, or zero.
roll_number = 25
temperature = -10
balance = 0
print(roll_number)
print(temperature)
print(balance)
Output
25
-10
0
x = 500
print(type(x))
Output
<class 'int'>
a = 15
b = 4
print(a + b)
print(a - b)
print(a * b)
print(a // b)
Output
19
11
60
3
Integers are commonly used to store values such as:
students = 120
teachers = 15
print(students)
print(teachers)
Output
120
15
In this section, you learned what Python numbers are and explored the three built-in numeric data types: int, float, and complex. You studied the integer data type in detail, learned its characteristics, performed basic operations, and used the type() function to identify numeric types. In the next section, you will explore floating-point numbers (float), understand decimal values, scientific notation, and learn about complex numbers.
float)A floating-point number, or simply a float, is a number that contains a decimal point. Float values are used whenever greater precision is needed, such as measurements, percentages, prices, and scientific calculations.
height = 5.8
price = 99.99
temperature = -12.5
print(height)
print(price)
print(temperature)
Output
5.8
99.99
-12.5
x = 25.75
print(type(x))
Output
<class 'float'>
a = 12.5
b = 3.5
print(a + b)
print(a - b)
print(a * b)
print(a / b)
Output
16.0
9.0
43.75
3.5714285714285716
Python also supports scientific notation using the letter e or E.
x = 2e3
y = 5.5e2
print(x)
print(y)
Output
2000.0
550.0
Here, 2e3 means 2 × 10³, while 5.5e2 means 5.5 × 10².
Float values are commonly used for:
price = 459.99
discount = 12.5
print(price)
print(discount)
Output
459.99
12.5
complex)Python supports complex numbers as a built-in numeric data type. A complex number consists of a real part and an imaginary part.
The imaginary part is represented using the letter j.
real + imaginaryj
x = 4 + 3j
y = 2 - 5j
print(x)
print(y)
Output
(4+3j)
(2-5j)
number = 8 + 6j
print(type(number))
Output
<class 'complex'>
Python provides the real and imag attributes.
number = 5 + 8j
print(number.real)
print(number.imag)
Output
5.0
8.0
a = 3 + 2j
b = 1 + 4j
print(a + b)
print(a - b)
print(a * b)
Output
(4+6j)
(2-2j)
(-5+14j)
Although beginners may not use complex numbers frequently, they are important in several fields.
| Data Type | Stores | Example |
|---|---|---|
int |
Whole numbers | 50 |
float |
Decimal numbers | 12.75 |
complex |
Real and imaginary numbers | 3+5j |
In this section, you learned about Python’s floating-point and complex number data types. You explored how float values store decimal numbers, how scientific notation works, and how to perform arithmetic with floating-point values. You also learned what complex numbers are, how to create them, access their real and imaginary parts, and perform basic operations. In the next section, you will learn how to check numeric data types, convert between different number types, use useful built-in number functions, and understand common mistakes beginners make while working with Python numbers.
Python provides several built-in functions that make it easy to work with numbers. These functions help you identify data types, convert values, find absolute values, round numbers, and perform mathematical calculations.
Understanding these functions will help you write better Python programs and avoid common mistakes.
You can use the type() function to determine the data type of any value or variable.
age = 25
price = 19.99
number = 4 + 2j
print(type(age))
print(type(price))
print(type(number))
Output
<class 'int'>
<class 'float'>
<class 'complex'>
Python allows you to convert one numeric data type into another using built-in conversion functions.
| Function | Description | Example |
|---|---|---|
int() |
Converts a value to an integer. | int(5.8) |
float() |
Converts a value to a float. | float(10) |
complex() |
Converts a value to a complex number. | complex(8) |
x = 15
y = float(x)
z = complex(x)
print(y)
print(z)
Output
15.0
(15+0j)
When converting a float to an integer, Python removes the decimal part instead of rounding the number.
price = 25.99
print(int(price))
Output
25
| Function | Purpose | Example |
|---|---|---|
abs() |
Returns the absolute value. | abs(-15) |
round() |
Rounds a number. | round(5.67) |
pow() |
Raises a number to a power. | pow(2, 3) |
max() |
Returns the largest value. | max(4, 8, 2) |
min() |
Returns the smallest value. | min(4, 8, 2) |
print(abs(-18))
print(round(7.89))
print(pow(3, 2))
print(max(15, 25, 10))
print(min(15, 25, 10))
Output
18
8
9
25
10
Python allows underscores (_) to improve the readability of large numbers.
population = 1_450_000_000
print(population)
Output
1450000000
Underscores do not affect the actual value; they simply make large numbers easier to read.
int() to round decimal values instead of removing the decimal part.j notation.int for whole numbers.float whenever decimal precision is required.complex only when mathematical or scientific calculations require it.type() to verify data types while learning Python.int(), float(), or complex() whenever needed.In this section, you learned how to work with Python numbers using built-in functions and type conversion. You explored the type() function, converted values between different numeric data types, and practiced using useful functions such as abs(), round(), pow(), max(), and min(). You also learned common beginner mistakes and best practices for working with numeric values. In the final section, you will review the complete lesson through a summary, key takeaways, frequently asked questions, coding exercises, interview questions, a mini project, and a preview of the next lesson on Python Strings.
In this lesson, you explored Python’s built-in numeric data types: int, float, and complex. You learned what each type represents, how Python automatically identifies numeric values, and how to create variables using different number types.
You also learned how to perform basic arithmetic operations, use scientific notation, access the real and imaginary parts of complex numbers, convert between numeric data types, and work with useful built-in functions such as abs(), round(), pow(), max(), and min().
By understanding Python numbers, you now have the foundation needed to perform calculations and work with numeric data confidently in your Python programs.
int, float, and complex.type() function is used to determine the data type of a value.int(), float(), and complex().Python provides three built-in numeric data types: int, float, and complex.
int and float?An int stores whole numbers, while a float stores numbers with decimal points.
Use the type() function.
x = 25
print(type(x))
Yes. Python supports arbitrarily large integers, limited mainly by the available system memory.
j represent in a complex number?The letter j represents the imaginary part of a complex number.
int() round decimal values?No. The int() function removes the decimal portion instead of rounding.
Beginners will primarily use int and float. Complex numbers are used less frequently and mainly in scientific or mathematical applications.
The max() function returns the largest value.
int, float, and complex, then display their data types.abs(), round(), max(), and min() functions in a Python program.int and float?type() function?Create a Python program that demonstrates the use of numeric data types and arithmetic operations.
Your program should:
round() function.max() and min().Great job! You have successfully learned how Python represents and works with numeric values using the int, float, and complex data types.
In the next lesson, you will learn Python Strings, where you will explore how to create strings, access individual characters, perform string operations, and work with Python’s powerful built-in string methods.