Introduction to Numbers in Python in Python

Introduction to Numbers in Python

Numeric Types in Python

Python supports several numeric data types. Each type has specific characteristics that make it suitable for different situations. Understanding these types will help you choose the right one for your needs.

Integers (int)

Integers in Python are whole numbers without a decimal point. They can be positive, negative, or zero.

  • Characteristics: Integers have no theoretical upper limit, but are limited by the available memory of the computer.
  • Notation: Simply written as a number, e.g., 123, -456.

Example 

# Declaring integers
a = 10
b = -42
c = 0
# Displaying values
print(a)  # 10
print(b)  # -42
print(c)  # 0

 Floats (float)

Floating-point numbers have a decimal point and are used for representing real numbers and approximations.

  • Characteristics: Floats are represented in decimal notation, such as 3.14, or scientific notation, such as 1.23e4 (which represents 12300.0).
  • Notation: Written with a decimal part, e.g., 3.14, -2.718.

Example 

# Declaring floats
x = 3.14
y = -0.001
z = 1.23e4  # 12300.0 in scientific notation
# Displaying values
print(x)  # 3.14
print(y)  # -0.001
print(z)  # 12300.0

 Complex Numbers (complex)

Complex numbers have both a real part and an imaginary part, represented as a + bj, where a is the real part and b is the imaginary part.

  • Characteristics: The real part is a float, and the imaginary part is also a float. In Python, the suffix j is used for the imaginary part.
  • Notation: Written in the form a + bj, e.g., 1 + 2j.

Example 

# Declaring complex numbers
z1 = 4 + 5j
z2 = -1 - 3j
# Displaying values
print(z1)  # (4+5j)
print(z2)  # (-1-3j)

 Memory Representation of Numbers

In Python, each numeric type is represented differently in memory:

  • Integers (int): Stored using direct binary representation. Python uses a flexible implementation that can handle integers of arbitrary size, limited only by the available memory.
  • Floats (float): Represented using the IEEE 754 standard for floating-point numbers, allowing for a high degree of precision but potentially introducing rounding errors.
  • Complex Numbers (complex): Stored as a pair of floats, one for the real part and one for the imaginary part.

Basic Operations with Numbers

Basic operations you can perform with numbers include:

  • Addition (+): Adds two numbers.
  • Subtraction (-): Subtracts one number from another.
  • Multiplication (*): Multiplies two numbers.
  • Division (/): Divides one number by another, returning a float.
  • Integer Division (//): Divides one number by another, returning an integer.
  • Modulo (%): Returns the remainder of the division of one number by another.
  • Exponentiation (**): Raises a number to the power of another.

Examples 

a = 10
b = 3
# Addition
print(a + b)  # 13
# Subtraction
print(a - b)  # 7
# Multiplication
print(a * b)  # 30
# Division
print(a / b)  # 3.3333333333333335
# Integer Division
print(a // b)  # 3
# Modulo
print(a % b)  # 1
# Exponentiation
print(a ** b)  # 1000

 Scientific Notation and Precision

Python supports scientific notation for floating-point numbers. This notation is useful for representing very large or very small numbers.

  • Notation: 1e3 is equivalent to 1000.0, 2.5e-4 is equivalent to 0.00025.

Example 

# Scientific notation
a = 1e3
b = 2.5e-4
print(a)  # 1000.0
print(b)  # 0.00025

 Interactions Between Numeric Types

Python automatically performs type conversion when mixing numeric types in operations. For example:

  • Integer and Float: Adding an integer and a float results in a float.
  • Float and Complex: Adding a float and a complex number results in a complex number.

Example 

i = 10
f = 5.5
c = 2 + 3j
# Addition of int and float
result1 = i + f
print(result1)  # 15.5
# Addition of float and complex
result2 = f + c
print(result2)  # (7.5+3j)

 Checking Types

It’s often useful to check the type of a variable. Python provides the type() function for this.

Example 

a = 10
b = 3.14
c = 1 + 2j
print(type(a))  # <class 'int'>
print(type(b))  # <class 'float'>
print(type(c))  # <class 'complex'>

 

 

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *