….
🔢 Python Numbers — A Complete Guide by GoNimbus
In Python, numbers are not just digits — they are powerful data types used in calculations, statistics, data science, simulations, and more.
Let’s explore the three main number types Python provides and how to work with them effectively.
📌 Number Types in Python
Python supports three basic numeric types:
Type | Description |
---|---|
int | Whole numbers (positive, negative, zero) |
float | Decimal numbers, including scientific notation |
complex | Numbers with a real and imaginary part |
Python automatically assigns a type when you assign a value:
a = 10 # int
b = 3.14 # float
c = 2 + 3j # complex
Use the type()
function to check the type of any number:
print(type(a)) # <class 'int'>
print(type(b)) # <class 'float'>
print(type(c)) # <class 'complex'>
🔢 Integer (int
)
Integers are whole numbers without a decimal. They can be positive, negative, and can be of unlimited length.
small = 42
large = 9876543210123456789
negative = -100
print(type(small)) # int
print(type(large)) # int
print(type(negative)) # int
🔣 Float (float
)
Floats represent decimal numbers. They can also be written in scientific notation using e
or E
.
pi = 3.14159
price = 99.0
negative_float = -5.67
sci_number = 2e2 # 2 x 10^2 = 200.0
print(type(sci_number)) # float
🧮 Complex Numbers (complex
)
Complex numbers include a real and imaginary part. Python uses j
(not i
) for imaginary numbers.
x = 4 + 5j
y = -3j
z = 6
print(type(x)) # complex
print(type(y)) # complex
print(type(z)) # int
🧠 Tip: Complex numbers are useful in electrical engineering, simulations, and advanced math.
🔁 Type Conversion
Python lets you convert between numeric types:
a = 7 # int
b = float(a) # float → 7.0
c = complex(a) # complex → 7+0j
d = 5.8
e = int(d) # int → 5 (decimal truncated)
print(b, c, e)
❗ You cannot convert complex
to int
or float
.
🎲 Generate Random Numbers in Python
Need a random number? Python has a built-in module called random
.
import random
print(random.randint(1, 100)) # Random integer between 1 and 100
print(random.uniform(1.0, 5.0)) # Random float between 1.0 and 5.0
print(random.choice([10, 20, 30])) # Random choice from a list
Explore more in the random
module for games, AI, testing, and simulations.
📘 Summary
Task | Function/Example |
---|---|
Check type | type(x) |
Convert int to float | float(5) |
Convert float to int | int(7.9) |
Generate random number | random.randint(1, 10) |
Use scientific notation | x = 6.5e3 → 6500.0 |
Create complex number | x = 3 + 4j |
🚀 Try It on GoNimbus!
Use our built-in Python editor to test this yourself:
import random
score = random.randint(10, 100)
print("Your random score is:", score)
Want to explore data types, casting, or math operations next? Let’s build your coding confidence one topic at a time.