Python Numbers – Complete Guide for Beginners

Last updated 5 months, 1 week ago | 378 views 75     5

Tags:- Python

In Python, numbers are a fundamental data type used for math, logic, and data manipulation. Python supports multiple types of numeric values, including integers, floating-point numbers, and complex numbers.

This article covers:

  • Numeric types in Python

  • Type conversion

  • Math operations

  • Common pitfalls

  • Tips and complete examples


Numeric Types in Python

Python supports three built-in numeric types:

Type Description Example
int Integer (whole numbers) 5, -10, 1000
float Floating-point (decimals) 3.14, -0.001
complex Complex numbers (real + imaginary) 2 + 3j

Integers (int)

Integers are whole numbers without decimals.

x = 10
y = -3
z = 0

There is no limit on integer size in Python 3 — they can grow as large as memory allows.


Floating-Point Numbers (float)

Floats are real numbers with decimal points.

pi = 3.14159
temperature = -20.5

Scientific Notation:

avogadro = 6.022e23   # 6.022 × 10^23

Complex Numbers (complex)

Complex numbers have a real and imaginary part.

z = 2 + 3j
print(z.real)  # 2.0
print(z.imag)  # 3.0

Useful in advanced mathematics and signal processing.


Checking Number Types

Use type() or isinstance():

x = 42
print(type(x))               # <class 'int'>
print(isinstance(x, int))    # True

Type Conversion

Use these functions to convert between types:

float(3)        # 3.0
int(3.9)        # 3 (truncates decimal)
complex(2)      # (2+0j)
str(42)         # "42"

Beware: converting from float to int truncates, not rounds.

int(3.9)  # Output: 3

➕ Arithmetic Operators

Python supports all basic arithmetic operations:

Operator Description Example Result
+ Addition 5 + 2 7
- Subtraction 5 - 2 3
* Multiplication 5 * 2 10
/ Division (float) 5 / 2 2.5
// Floor Division 5 // 2 2
% Modulus (remainder) 5 % 2 1
** Exponentiation 2 ** 3 8

Built-in Math Functions

Python includes useful built-in functions:

abs(-7)        # 7
round(3.1415, 2)  # 3.14
pow(2, 3)      # 8

Math Module

For advanced math, use the math module:

import math

print(math.sqrt(16))     # 4.0
print(math.floor(3.9))   # 3
print(math.ceil(3.1))    # 4
print(math.pi)           # 3.141592653589793

Example: Simple Calculator

# A simple calculator
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))

print(f"Addition: {a + b}")
print(f"Subtraction: {a - b}")
print(f"Multiplication: {a * b}")
print(f"Division: {a / b}")

Tips for Working with Numbers

  • Use float() when working with decimals or division.

  • Use round() for precision formatting in financial calculations.

  • Prefer math.isclose(a, b) for comparing floating-point numbers (avoids rounding errors).

  • Use int() carefully — it truncates decimals, which can lead to bugs.


⚠️ Common Pitfalls

Mistake Problem Fix
int("3.14") Raises ValueError Convert to float first: int(float("3.14"))
3/2 expecting integer Returns 1.5 Use // if you want 1
Using == with floats May not match exactly Use math.isclose(a, b)
Forgetting integer division truncates 5 // 2 == 2 Use / if you need decimals

What's Next?

Once you're comfortable with numbers:

  • Explore the decimal module for precision math (e.g. for finance)

  • Learn the fractions module for rational numbers

  • Understand numeric data in NumPy for scientific computing


 Summary

Type Description Example
int Whole numbers 100, -3
float Decimal numbers 3.14, -0.001
complex Real + imaginary 2 + 3j
Arithmetic +, -, *, /, **, %, //  
Type conversion int(), float(), complex()