Python Numbers

 🔢 Python Numbers

Python Numbers have three main numeric data types:

TypeDescriptionExample
intWhole numbers (positive/negative, no decimals)10, -5, 1000
floatDecimal numbers3.14, 10.5
complexNumbers with imaginary part (j)3 + 5j

1️⃣ Integer (int)

  • No decimal point

  • Can be positive, negative, or zero

Example:

a = 10
b = -20
c = 0
print(type(a))

2️⃣ Float (float)

  • Numbers with decimal point

  • Python automatically converts any decimal value to float.

Example:

x = 10.5
y = -3.14
print(type(x))

Scientific notation is also allowed:

z = 2e3 # 2 × 10³ → 2000.0
print(z)

3️⃣ Complex Numbers (complex)

Used for mathematical and scientific calculations.

Format:

a + bj

Example:

z = 3 + 5j
print(type(z))

Access real & imaginary parts:

print(z.real)
print(z.imag)

🔄 Type Conversion (Casting)

Python allows converting one number type to another.

int()

x = int(10.8)
print(x) # 10

float()

y = float(10)
print(y) # 10.0

complex()

z = complex(5)
print(z) # (5+0j)

➕ Arithmetic with Numbers

Examples:

a = 10
b = 3
print(a + b) # Addition
print(a – b) # Subtraction
print(a * b) # Multiplication
print(a / b) # Division
print(a // b) # Floor Division
print(a % b) # Modulus
print(a ** b) # Exponent (Power)

📏 Built-in Number Functions

abs()   Absolute Value

print(abs(-10)) # 10

pow()   Power

print(pow(2, 3)) # 8

round()   Round a number

print(round(3.14159, 2)) # 3.14

📚 Python math Module (Advanced)

To use advanced math functions:

import math

print(math.sqrt(25)) # Square root
print(math.ceil(4.1)) # Round up
print(math.floor(4.9)) # Round down


🧪 Random Numbers (random module)

Use for games, testing, simulations.

import random

print(random.randint(1, 10)) # Random number between 1 and 10


🏁 Summary Table

Featureintfloatcomplex
Whole numbers
Decimal values
Supports imaginary part
Allows math operations

📝 Practice Challenges

Try these:

  1. Create variables of all three number types and print their types.

  2. Convert float to int and int to complex.

  3. Generate a random number between 50 and 200.

  4. Use math module to calculate square root of 144.

  5. Perform exponent, modulus, and floor division on numbers.

You may also like...