✍️ Python Syntax & Basic Constructs
Estimated reading: 3 minutes 29 views

πŸ”’ 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:

TypeDescriptionExample
intInteger (whole numbers)5, -2, 0
floatFloating point numbers (with decimals)3.14, -0.5
complexComplex numbers with real and imaginary parts3+4j, 2j
DecimalFixed-precision floating point from decimal moduleDecimal('1.10')
FractionRational numbers from fractions moduleFraction(3, 4)
boolBoolean 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

LiteralInterpretation
123, -24, 0Integer literals
3.14, 1e-10Floating point (exponent supported)
0x9ffHexadecimal
0o377Octal
0b101010Binary
3+4jComplex

πŸ“… 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, and complex
  • Use Decimal for financial accuracy
  • Use Fraction for rational math
  • Precision errors occur with binary float
  • math and cmath 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Python Numbers

Or Copy Link

CONTENTS
Scroll to Top