Understanding Python Numbers: A Comprehensive Guide

Dive into the world of numbers in Python, including integer, float, and complex types.

Explore the various numeric types in Python, including integers, floats, and complex numbers.

Python Number Types: int, float, complex

Python provides three distinct numeric types to represent numbers: integers, floats, and complex numbers.

Integers

Integers in Python are zero, positive, or negative whole numbers without a fractional part, possessing unlimited precision. Examples include 0, 100, and -10. Here's how you can define integers:

>>> 0
0
>>> 100
100
>>> -10
-10
>>> 1234567890
1234567890

Integers can also be represented in binary, octal, and hexadecimal formats:

>>> 0b11011000 # binary
216
>>> 0o12 # octal
10
>>> 0x12 # hexadecimal
18

All integer literals or variables are objects of the int class. You can verify this with the type() function:

>>> type(100)
<class 'int'>
>>> x = 1234567890
>>> type(x)
<class 'int'>

Note: Leading zeros in integers are not allowed, and Python uses underscores _ as a delimiter for readability:

>>> x = 1_234_567_890
>>> x
1234567890

If you include a fractional part, the number becomes a float:

>>> x = 5
>>> type(x)
<class 'int'>
>>> x = 5.0
>>> type(x)
<class 'float'>

The int() function can convert strings or floats to integers:

>>> int('5.5')
5
>>> int('100', 2)
4

Binary, Octal, and Hexadecimal Representations

Binary

Binary numbers are prefixed with 0b:

>>> x = 0b11011000
>>> x
216

Octal

Octal numbers start with 0o:

>>> x = 0o12
>>> x
10

Hexadecimal

Hexadecimal numbers utilize 0x:

>>> x = 0x12
>>> x
18

Floats

Floats are real numbers with a fractional part, denoted using a decimal point or scientific notation E or e.

>>> f = 1.2
>>> f
1.2

Underscores can separate float digits for readability:

>>> f = 123_42.222_013
>>> f
12342.222013

Floats have a system-dependent maximum size, exceeding it results in infinity:

>>> f = 2e400
>>> f
inf

Scientific Notation

Floats can be expressed succinctly in scientific notation:

>>> f = 1e3
>>> f
1000.0

Convert strings and integers to floats using float():

>>> float('-5')
-5.0

Complex Numbers

A complex number combines real and imaginary components. Use j or J for the imaginary part:

>>> a = 5 + 2j
>>> a
(5+2j)

Arithmetic Operators

Here are the primary arithmetic operations:

  • Addition (+): Adds two values.
  • Subtraction (-): Subtracts one value from another.
  • Multiplication (*): Multiplies two values.
  • Division (/): Divides one value by another.
  • Modulus (%): Gives the remainder of the division.
  • Exponent (**): Raises a number to a power.
  • Floor Division (//): Division with floors result.

Complex Arithmetic

Operations on complex numbers combine real and imaginary parts:

>>> a = 6 + 4j
>>> b = 3 + 2j
>>> a + b
(9 + 6j)

Type Casting

Convert between numeric types using these functions:

  • int: Converts to an integer.
  • float: Converts to a floating-point number.
  • complex: Converts to a complex number.
  • hex: Converts to a hexadecimal.
  • oct: Converts to an octal.

These insights into Python's numeric types should equip you with the tools needed for handling numbers effectively in your programs.