Python Numbers: Integers, Floats & Complex Numbers

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:

  1. Integer (int)
  2. Float (float)
  3. 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:

10
-50
0

Integers can be positive, negative, or zero.

Checking Integer Type

Use the type() function.

Example:


number = 100
print(type(number))

Output:

<class ‘int’>

Float Numbers (float)

Floats represent decimal numbers.

Example:


price = 99.99
temperature = 36.5
discount = 15.75
Display value:
print(price)

Output:

99.99

Floats are useful when precision is required.

Checking Float Type

Example:


price = 99.99
print(type(price))

Output:

<class ‘float’>

Scientific Notation

Python supports scientific notation.

Example:


number = 2.5e3
print(number)

Output:

2500.0

Another example:


number = 5e2
print(number)

Output:

500.0

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:

(3+4j)

Check type:


print(type(number))

Output:

<class ‘complex’>

Accessing Real and Imaginary Parts

Example:


number = 3 + 4j
print(number.real)
print(number.imag)

Output:

3.0
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:

25.0

Convert Float to Integer

Example:


price = 99.99
result = int(price)
print(result)

Output:

99

The decimal portion is removed.

Convert String to Integer

Example:


number = "100"
result = int(number)
print(result)

Output:

100

Convert String to Float

Example:


price = "99.99"
result = float(price)
print(result)

Output:

99.99

Arithmetic Operations

Python supports various arithmetic operations.

Addition:


print(10 + 5)

Output:

15

Subtraction:


print(10 - 5)

Output:

5

Multiplication:


print(10 * 5)

Output:

50

Division:


print(10 / 5)

Output:

2.0

Modulus:


print(10 % 3)

Output:

1

Exponentiation:


print(2 ** 3)

Output:

8

Floor Division:


print(10 // 3)

Output:

3

Python Built-in Number Functions

Python provides several useful functions for numbers.

1. abs()

Returns the absolute value.

Example:


print(abs(-20))

Output:

20

2. round()

Rounds a number.

Example:


print(round(5.67))

Output:

6

3. max()

Returns the largest value.

Example:


print(max(10, 20, 30))

Output:

30

4. min()

Returns the smallest value.

Example:


print(min(10, 20, 30))

Output:

10

5. pow()

Calculates power.

Example:


print(pow(2, 4))

Output:

16

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:

5.0

2. Ceiling Function

Example:


import math
print(math.ceil(4.2))

Output:

5

3. Floor Function

Example:


import math
print(math.floor(4.9))

Output:

4

4. Pi Value

Example:


import math
print(math.pi)

Output:

3.141592653589793

Random Numbers

Python provides the random module for generating random values.

Example:


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

Possible Output:

7

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:

55000

Numbers make such calculations possible.

Shopping Cart Example

Example:


price = 999.99
quantity = 3
total = price * quantity
print(total)

Output:

2999.97

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:

ZeroDivisionError

2. Mixing Strings and Numbers

Incorrect:


age = "25"
print(age + 5)

Output:

TypeError

Correct:


age = int(age)
print(age + 5)

3. Losing Decimal Values

Example:


price = 99.99
print(int(price))

Output:

99

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:

5.0

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?
Ans: Numbers are numeric data types used to store numerical values.
Q 2: Name numeric types in Python.
Ans: int, float, and complex.
Q 3: What is a complex number in Python?
Ans: A number with real and imaginary parts, written using j.
Q 4: How do you perform arithmetic operations?
Ans: Using operators like +, -, *, /.
Q 5: Are Python numbers immutable?
Ans: Yes, numeric values in Python cannot be changed after creation.

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?






Related Python Tutorials