Skip to content

Numbers in Python

Introduction

Numbers are one of the most fundamental data types in Python. Whether you’re adding prices, calculating averages, or working with scientific data, understanding how numbers work is crucial.

Python makes working with numbers simple and intuitive, but there are a few key concepts to understand to avoid errors and write clean code.


Types of Numbers in Python

Python has three main types of numbers:

  1. Integers (int) – Whole numbers, positive or negative.
  2. Floats (float) – Numbers with a decimal point.
  3. Complex (complex) – Numbers with a real and imaginary part.
x = 10 # Integer
y = 3.14 # Float
z = 2 + 3j # Complex

Basic Arithmetic

Python can handle basic arithmetic operations just like a calculator:

a = 10
b = 3
# Addition
print(a + b) # 13
# Subtraction
print(a - b) # 7
# Multiplication
print(a * b) # 30
# Division
print(a / b) # 3.3333 (float division)
print(a // b) # 3 (integer division)
# Modulus (remainder)
print(a % b) # 1
# Power
print(a ** b) # 1000 (10 to the power of 3)

Type Conversion

You can convert between different types of numbers easily:

x = 10 # int
y = 3.5 # float
# Convert int to float
x_float = float(x) # 10.0
# Convert float to int
y_int = int(y) # 3 (rounds down)
# String to number
z = "42"
z_int = int(z) # 42

Common Operations

Python provides many built-in functions to work with numbers:

# Absolute value
print(abs(-7)) # 7
# Round a number
print(round(3.14159, 2)) # 3.14
# Max and Min
print(max(5, 10, 15)) # 15
print(min(5, 10, 15)) # 5
# Sum of a list
print(sum([1, 2, 3, 4])) # 10

Working with Large Numbers

Python can handle very large numbers without overflow:

big_number = 10 ** 100 # 1 followed by 100 zeros
print(big_number)
# Scientific notation
sci_number = 3e5 # 3 * 10^5
print(sci_number) # 300000.0

Best Practices

  • Use floats carefully – Floats can sometimes lead to precision errors:
    print(0.1 + 0.2) # 0.30000000000000004
    Use round() to handle precision issues.
  • Avoid dividing by zero – Python will raise an error if you divide by zero.
  • Use meaningful variable namesprice, total, and quantity are better than x, y, z.

Conclusion

Numbers in Python are powerful and easy to work with. By understanding the basics and following best practices, you can avoid common pitfalls and write more efficient code.

Happy coding!