DEV Community

Paulo GP
Paulo GP

Posted on • Updated on

Python: Understanding Numbers and Basic Math Operations

Introduction

This article delves into the diverse world of numeric operations in Python. From understanding the different types of numbers to executing basic arithmetic, and culminating in the art of representing numbers with f-strings, we embark on a journey through Python's numeric landscape.

Index

  • Types of Numbers in Python
  • Math Operations in Python
  • Number Representations using f-strings

Types of Numbers in Python

Python encompasses various numeric types, including integers, floating-point numbers, and complex numbers. Integers stand as whole numbers, while floating-point numbers embrace decimals. Complex numbers, denoted as x + yi, possess both real and imaginary components, with x and y as floating-point numbers.

Examples

Integer

fibonacci_10 = 55
print(f"The 10th Fibonacci number is {fibonacci_10}")
Enter fullscreen mode Exit fullscreen mode
Output:
The 10th Fibonacci number is 55
Enter fullscreen mode Exit fullscreen mode

Floating-point number

pi = 3.14159
print(f"The value of pi is approximately {pi}")
Enter fullscreen mode Exit fullscreen mode
Output:
The value of pi is approximately 3.14159
Enter fullscreen mode Exit fullscreen mode

Complex number

z = 3 + 4j
print(f"The absolute value of the complex number {z} is {abs(z)}")
Enter fullscreen mode Exit fullscreen mode
Output:
The absolute value of the complex number (3+4j) is 5.0
Enter fullscreen mode Exit fullscreen mode

Math Operations in Python

Python presents an array of built-in math operators facilitating basic arithmetic like addition, subtraction, multiplication, division, exponentiation, and modulo operations.

Examples

Addition

x = 3
y = 4
x_plus_y = x + y
print(f"The sum of {x} and {y} is {x_plus_y}")
Enter fullscreen mode Exit fullscreen mode
Output:
The sum of 3 and 4 is 7
Enter fullscreen mode Exit fullscreen mode

Subtraction

x = 3
y = 4
x_minus_y = x - y
print(f"The difference between {x} and {y} is {x_minus_y}")
Enter fullscreen mode Exit fullscreen mode
Output:
The difference between 3 and 4 is -1
Enter fullscreen mode Exit fullscreen mode

Multiplication

x = 3
y = 4
x_times_y = x * y
print(f"The product of {x} and {y} is {x_times_y}")
Enter fullscreen mode Exit fullscreen mode
Output:
The product of 3 and 4 is 12
Enter fullscreen mode Exit fullscreen mode

Division

x = 3
y = 4
x_divided_by_y = x / y
print(f"The result of dividing {x} by {y} is {x_divided_by_y}")
Enter fullscreen mode Exit fullscreen mode
Output:
The result of dividing 3 by 4 is 0.75
Enter fullscreen mode Exit fullscreen mode

Power

x = 3
y = 4
x_to_the_power_of_y = x ** y
print(f"{x} raised to the power of {y} is {x_to_the_power_of_y}")
Enter fullscreen mode Exit fullscreen mode
Output:
3 raised to the power of 4 is 81
Enter fullscreen mode Exit fullscreen mode

Modulo

x = 7
y = 3
x_modulo_y = x % y
print(f"The remainder when {x} is divided by {y} is {x_modulo_y}")
Enter fullscreen mode Exit fullscreen mode
Output:
The remainder when 7 is divided by 3 is 1
Enter fullscreen mode Exit fullscreen mode

Bitwise Operators

Bitwise operators are also fundamental in Python, allowing manipulation of individual bits within integers. Here's a simple example:

Bitwise AND

a = 5  # 101 in binary
b = 3  # 011 in binary

result_and = a & b  # 001 (1 in decimal)
print(f"The result of bitwise AND between {a} and {b} is {result_and}")
Enter fullscreen mode Exit fullscreen mode

Output:

The result of bitwise AND between 5 and 3 is 1
Enter fullscreen mode Exit fullscreen mode

Bitwise OR

a = 5  # 101 in binary
b = 3  # 011 in binary

result_or = a | b  # 111 (7 in decimal)
print(f"The result of bitwise OR between {a} and {b} is {result_or}")
Enter fullscreen mode Exit fullscreen mode

Output:

The result of bitwise OR between 5 and 3 is 7
Enter fullscreen mode Exit fullscreen mode

Bitwise XOR

a = 5  # 101 in binary
b = 3  # 011 in binary

result_xor = a ^ b  # 110 (6 in decimal)
print(f"The result of bitwise XOR between {a} and {b} is {result_xor}")
Enter fullscreen mode Exit fullscreen mode

Output:

The result of bitwise XOR between 5 and 3 is 6
Enter fullscreen mode Exit fullscreen mode

Bitwise NOT

a = 5  # 101 in binary

result_not_a = ~a  # -6 (in decimal)
print(f"The result of bitwise NOT on {a} is {result_not_a}")
Enter fullscreen mode Exit fullscreen mode

Output:

The result of bitwise NOT on 5 is -6
Enter fullscreen mode Exit fullscreen mode

Left Shift

a = 5  # 101 in binary

result_left_shift = a << 1  # 1010 (10 in decimal)
print(f"The result of left shifting {a} by 1 is {result_left_shift}")
Enter fullscreen mode Exit fullscreen mode

Output:

The result of left shifting 5 by 1 is 10
Enter fullscreen mode Exit fullscreen mode

Right Shift

a = 5  # 101 in binary

result_right_shift = a >> 1  # 10 (2 in decimal)
print(f"The result of right shifting {a} by 1 is {result_right_shift}")
Enter fullscreen mode Exit fullscreen mode

Output:

The result of right shifting 5 by 1 is 2
Enter fullscreen mode Exit fullscreen mode

Number Representations using f-strings

Introduced in Python 3.6, f-strings offer a succinct means to format strings. They allow embedding expressions within string literals via curly braces {}. f-strings prove invaluable in representing numbers diversely, be it specifying decimal places, employing various separators, or enhancing readability with underscores.

Examples

Two decimal places

pi = 3.14159
print(f"The value of pi is approximately {pi:.2f}")
Enter fullscreen mode Exit fullscreen mode
Output:
The value of pi is approximately 3.14
Enter fullscreen mode Exit fullscreen mode

Different separator

large_number = 1234567890
print(f"The large number is {large_number:,}")
Enter fullscreen mode Exit fullscreen mode
Output:
The large number is 1,234,567,890
Enter fullscreen mode Exit fullscreen mode

Underscores for readability

large_number = 1234567890
print(f"The large number is {large_number:_}")
Enter fullscreen mode Exit fullscreen mode
Output:
The large number is 1_234_567_890
Enter fullscreen mode Exit fullscreen mode

Conclusion

In conclusion, Python equips practitioners with a robust suite of tools for numerical manipulation, arithmetic computation, and expressive number representation. By harnessing its built-in numeric types, math operators, and the prowess of f-strings, Python emerges as an unparalleled choice for tackling numerical challenges with clarity and efficiency.

Top comments (0)