Introduction
Numbers are one of the most commonly used data types in Python programming. Whether you are calculating employee salaries, processing online payments, analyzing scientific data, creating games, or building machine learning models, numbers play a crucial role in software development.
Python provides built-in support for different types of numeric values, allowing developers to perform mathematical calculations efficiently.
What are Python Numbers?
Python numbers are data types used to store and manipulate numeric values.
Python supports three main numeric types:
- Integer (int)
- Float (float)
- Complex (complex)
Example:
age = 25
price = 99.99
number = 3 + 4j
Here:
- 25 is an integer.
- 99.99 is a float.
- 3 + 4j is a complex number.
Python automatically determines the numeric type when a value is assigned.
Integer Numbers (int)
Integers are whole numbers without decimal points.
Examples:
x = 10
y = -50
z = 0
Display values:
print(x)
print(y)
print(z)
Output:
-50
0
Integers can be positive, negative, or zero.
Checking Integer Type
Use the type() function.
number = 100
print(type(number))
Output:
Float Numbers (float)
Floats represent decimal numbers.
Example:
price = 99.99
temperature = 36.5
discount = 15.75
Display value:
print(price)
Output:
Floats are useful when precision is required.
Checking Float Type
Example:
price = 99.99
print(type(price))
Output:
Scientific Notation
Python supports scientific notation.
Example:
number = 2.5e3
print(number)
Output:
Another example:
number = 5e2
print(number)
Output:
Scientific notation is commonly used in scientific and engineering applications.
Complex Numbers (complex)
Python supports complex numbers directly.
Complex numbers contain:
- Real part
- Imaginary part
Example:
number = 3 + 4j
print(number)
Output:
Check type:
print(type(number))
Output:
Accessing Real and Imaginary Parts
Example:
number = 3 + 4j
print(number.real)
print(number.imag)
Output:
4.0
Complex numbers are commonly used in electrical engineering and scientific calculations.
Number Type Conversion
Python allows conversion between numeric types.
Convert Integer to Float
Example:
age = 25
result = float(age)
print(result)
Output:
Convert Float to Integer
Example:
price = 99.99
result = int(price)
print(result)
Output:
The decimal portion is removed.
Convert String to Integer
Example:
number = "100"
result = int(number)
print(result)
Output:
Convert String to Float
Example:
price = "99.99"
result = float(price)
print(result)
Output:
Arithmetic Operations
Python supports various arithmetic operations.
Addition:
print(10 + 5)
Output:
Subtraction:
print(10 - 5)
Output:
Multiplication:
print(10 * 5)
Output:
Division:
print(10 / 5)
Output:
Modulus:
print(10 % 3)
Output:
Exponentiation:
print(2 ** 3)
Output:
Floor Division:
print(10 // 3)
Output:
Python Built-in Number Functions
Python provides several useful functions for numbers.
1. abs()
Returns the absolute value.
Example:
print(abs(-20))
Output:
2. round()
Rounds a number.
Example:
print(round(5.67))
Output:
3. max()
Returns the largest value.
Example:
print(max(10, 20, 30))
Output:
4. min()
Returns the smallest value.
Example:
print(min(10, 20, 30))
Output:
5. pow()
Calculates power.
Example:
print(pow(2, 4))
Output:
Using the Math Module
Python’s math module provides advanced mathematical functions.
Import Module
import math
1. Square Root
Example:
import math
print(math.sqrt(25))
Output:
2. Ceiling Function
Example:
import math
print(math.ceil(4.2))
Output:
3. Floor Function
Example:
import math
print(math.floor(4.9))
Output:
4. Pi Value
Example:
import math
print(math.pi)
Output:
Random Numbers
Python provides the random module for generating random values.
Example:
import random
print(random.randint(1, 10))
Possible Output:
The output changes each time the program runs.
Real-Life Example
Suppose a company calculates employee salaries.
Example:
basic_salary = 50000
bonus = 5000
total_salary = basic_salary + bonus
print(total_salary)
Output:
Numbers make such calculations possible.
Shopping Cart Example
Example:
price = 999.99
quantity = 3
total = price * quantity
print(total)
Output:
This is a common real-world use of numeric data.
Advantages of Python Numbers
1. Easy to Use
Python provides simple syntax for numeric operations.
2. Dynamic Typing
No need to declare numeric types explicitly.
3. Supports Large Numbers
Python handles very large integers automatically.
4. Rich Mathematical Functions
Built-in and external modules simplify calculations.
5. Supports Complex Numbers
Useful for scientific and engineering applications.
Common Mistakes
1. Dividing by Zero
Incorrect:
print(10 / 0)
Output:
2. Mixing Strings and Numbers
Incorrect:
age = "25"
print(age + 5)
Output:
Correct:
age = int(age)
print(age + 5)
3. Losing Decimal Values
Example:
price = 99.99
print(int(price))
Output:
The decimal portion is removed.
4. Incorrect Use of Complex Numbers
Incorrect:
number = 3 + 4i
Correct:
number = 3 + 4j
Python uses j for imaginary numbers.
5. Assuming Integer Division
Example:
print(10 / 2)
Output:
Python returns a float for division.
Use floor division for integer results:
print(10 // 2)
Conclusion
Python supports three primary numeric types: integers, floats, and complex numbers. These data types allow developers to perform simple calculations, financial operations, scientific computations, and advanced mathematical processing.
Python also provides powerful built-in functions and modules such as math and random to make numerical operations easier and more efficient.
Python Number – Interview Questions
Q 1: What are numbers in Python?
Q 2: Name numeric types in Python.
Q 3: What is a complex number in Python?
Q 4: How do you perform arithmetic operations?
Q 5: Are Python numbers immutable?
Python Number – Objective Questions (MCQs)
Q1. Which of the following is not a numeric data type in Python?
Q2. What will be the output of the following code?
x = 5
y = 2
print(x / y)
Q3. Which function is used to convert a float number into an integer in Python?
Q4. What will be the output of the following code?
x = 5 + 3j
print(type(x))
Q5. Which of the following statements about Python numbers is true?