Python Numbers
🔢 Python Numbers
Python Numbers have three main numeric data types:
| Type | Description | Example |
|---|---|---|
int | Whole numbers (positive/negative, no decimals) | 10, -5, 1000 |
float | Decimal numbers | 3.14, 10.5 |
complex | Numbers with imaginary part (j) | 3 + 5j |
1️⃣ Integer (int)
No decimal point
Can be positive, negative, or zero
Example:
2️⃣ Float (float)
Numbers with decimal point
Python automatically converts any decimal value to float.
Example:
Scientific notation is also allowed:
3️⃣ Complex Numbers (complex)
Used for mathematical and scientific calculations.
Format:
Example:
Access real & imaginary parts:
🔄 Type Conversion (Casting)
Python allows converting one number type to another.
int()
float()
complex()
➕ Arithmetic with Numbers
Examples:
📏 Built-in Number Functions
abs() Absolute Value
pow() Power
round() Round a number
📚 Python math Module (Advanced)
To use advanced math functions:
🧪 Random Numbers (random module)
Use for games, testing, simulations.
🏁 Summary Table
| Feature | int | float | complex |
|---|---|---|---|
| Whole numbers | ✔ | ❌ | ❌ |
| Decimal values | ❌ | ✔ | ❌ |
| Supports imaginary part | ❌ | ❌ | ✔ |
| Allows math operations | ✔ | ✔ | ✔ |
📝 Practice Challenges
Try these:
Create variables of all three number types and print their types.
Convert float to int and int to complex.
Generate a random number between 50 and 200.
Use math module to calculate square root of 144.
Perform exponent, modulus, and floor division on numbers.
