π’ Python Numbers β Everything You Need to Know (With Examples)
πͺ² Introduction β Why Numbers Matter in Python
Whether you’re calculating scientific values, building finance apps, or analyzing dataβnumbers are at the core of Python programming. Python provides powerful tools to handle integers, floating points, complex numbers, and even fractions and decimals.
In this guide, youβll learn:
- π What types of numbers Python supports
- π§ How numeric operations work
- π‘ Best practices for working with each number type
- β Practical examples with line-by-line explanations
π Numeric Types in Python
Python categorizes numbers into the following core types:
Type | Description | Example |
---|---|---|
int | Integer (whole numbers) | 5 , -2 , 0 |
float | Floating point numbers (with decimals) | 3.14 , -0.5 |
complex | Complex numbers with real and imaginary parts | 3+4j , 2j |
Decimal | Fixed-precision floating point from decimal module | Decimal('1.10') |
Fraction | Rational numbers from fractions module | Fraction(3, 4) |
bool | Boolean values (special case of integers: True=1 , False=0 ) | True , False |
π Python integers support unlimited precision, so large calculations like 2**1000
are possible without overflow.
π§ͺ Numeric Literals
Literal | Interpretation |
---|---|
123 , -24 , 0 | Integer literals |
3.14 , 1e-10 | Floating point (exponent supported) |
0x9ff | Hexadecimal |
0o377 | Octal |
0b101010 | Binary |
3+4j | Complex |
π
Any number with a decimal or exponent is a float
, otherwise it’s an int
.
β Arithmetic Operations with Explanation
a = 10 # Assign integer value 10 to variable 'a'
b = 3 # Assign integer value 3 to variable 'b'
print(a + b) # 13: Adds a and b
print(a - b) # 7: Subtracts b from a
print(a * b) # 30: Multiplies a and b
print(a / b) # 3.333...: Divides a by b (float result)
print(a // b) # 3: Floor division, drops decimal part
print(a % b) # 1: Modulo, remainder of a divided by b
print(a ** b) # 1000: a raised to the power of b
π§ Floating Point Precision
print(0.1 + 0.2 == 0.3) # False: due to binary float inaccuracy
π To fix this, use decimal.Decimal
:
from decimal import Decimal
print(Decimal('0.1') + Decimal('0.2') == Decimal('0.3')) # True
βοΈ Math Functions
import math
print(math.sqrt(16)) # 4.0: square root
print(math.floor(2.9)) # 2: round down
print(math.ceil(2.1)) # 3: round up
print(math.pow(2, 3)) # 8.0: same as 2 ** 3
print(abs(-10)) # 10: absolute value
π Complex Numbers
c = 3 + 4j
print(c.real) # 3.0: real part
print(c.imag) # 4.0: imaginary part
Use cmath
for complex-specific math:
import cmath
print(cmath.sqrt(-1)) # 1j
π Fractions and Decimals
β Fractions
from fractions import Fraction
f = Fraction(3, 4)
print(f + Fraction(1, 4)) # 1
π΅ Decimals
from decimal import Decimal
price = Decimal('19.99') + Decimal('0.01')
print(price) # 20.00
π§ Summary β Recap & Key Takeaways
Python provides robust numeric types and operations suitable for simple calculations to advanced scientific computing.
πΉ Key Takeaways:
- Python has built-in support for
int
,float
, andcomplex
- Use
Decimal
for financial accuracy - Use
Fraction
for rational math - Precision errors occur with binary
float
math
andcmath
provide advanced math tools
π‘ Real-World Relevance:
- Used in finance apps, simulations, machine learning, and data analytics
β FAQ β Python Numbers
π€ What is the difference between int
and float
?
int
is for whole numbers, float
includes decimals.
π« Why is 0.1 + 0.2 != 0.3
?
Because of binary floating-point inaccuracies. Use Decimal
for precision.
π€ Can I use really large integers in Python?
Yes. Python supports arbitrarily large integers like 2 ** 10000
.
π«Ή How do I represent exact fractions?
Use the fractions.Fraction
class for precise rational arithmetic.
π Can I do trigonometry or log operations?
Yes, use the math
or cmath
modules for advanced functions.
Share Now :