← Back to Chapters

Python Numbers

? Python Numbers

? Quick Overview

In Python, numbers are a basic data type used for mathematical operations, calculations, and data processing. Python supports three main numeric types: integers, floating-point numbers, and complex numbers. You can easily convert between these types and use built-in numeric functions to perform common tasks.

? Key Concepts

  • Integers (int) represent whole numbers like 10, -25.
  • Floats (float) represent decimal numbers like 3.14, -0.001.
  • Complex numbers (complex) represent numbers with a real and imaginary part like 2+3j.
  • You can check the type of any value using the type() function.
  • You can convert between numeric types using int(), float(), and complex().
  • Useful built-in functions include abs(), pow(), and round() for common numeric operations.

? Syntax and Theory

Python automatically assigns a numeric type based on the value you write:

  • x = 5x is of type int.
  • y = 3.14y is of type float.
  • z = 2 + 3jz is of type complex.

For complex numbers, you can access:

  • z.real → the real part (as a float).
  • z.imag → the imaginary part (as a float).

Type conversion allows you to change values from one numeric type to another using constructor functions: int(), float(), and complex().

? Code Examples

? View Example: Basic Number Types
x = 5         # int
y = 3.14      # float
z = 2 + 3j    # complex

print(type(x))
print(type(y))
print(type(z))
? View Example: Accessing Complex Parts
z = 2 + 3j
print(z.real)  # 2.0
print(z.imag)  # 3.0
♻️ View Example: Type Conversion
a = float(5)
b = int(4.8)
c = complex(3)

print(a, b, c)
?️ View Example: Useful Numeric Functions
print(abs(-10))       # 10
print(pow(2, 3))      # 8
print(round(3.7))     # 4

? Output and Explanation

? Understanding the Results

  • For the basic number types example, the output will show: <class 'int'>, <class 'float'>, and <class 'complex'>, confirming the type of each variable.
  • For the complex number example, z.real gives 2.0 and z.imag gives 3.0, which are the real and imaginary parts of 2+3j.
  • In the type conversion example, a becomes 5.0 (float), b becomes 4 (int, decimal part removed), and c becomes (3+0j) (complex number with zero imaginary part).
  • For the numeric functions:
    • abs(-10) returns 10 (distance from zero).
    • pow(2, 3) returns 8 (2 raised to the power 3).
    • round(3.7) returns 4 (nearest integer).

✅ Tips and Best Practices

  • Use type() often to check what kind of number (int, float, complex) you are working with.
  • Prefer float when working with division or values that require decimals.
  • Remember that int() truncates the decimal part (for example, int(4.9) becomes 4, not 5).
  • Use abs() when you need the magnitude of a number regardless of sign.
  • pow(x, y) is equivalent to x ** y and can sometimes be clearer to read.

? Try It Yourself

  • Create a variable with a float value, convert it to int using int(), and print both values.
  • Create a complex number like 5+2j and print its real and imaginary parts using .real and .imag.
  • Use pow() to calculate 3**4 and verify the result with manual multiplication.
  • Experiment with round() on different floats, such as round(3.2), round(3.5), and round(3.8).