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}")
Output:
The 10th Fibonacci number is 55
Floating-point number
pi = 3.14159
print(f"The value of pi is approximately {pi}")
Output:
The value of pi is approximately 3.14159
Complex number
z = 3 + 4j
print(f"The absolute value of the complex number {z} is {abs(z)}")
Output:
The absolute value of the complex number (3+4j) is 5.0
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}")
Output:
The sum of 3 and 4 is 7
Subtraction
x = 3
y = 4
x_minus_y = x - y
print(f"The difference between {x} and {y} is {x_minus_y}")
Output:
The difference between 3 and 4 is -1
Multiplication
x = 3
y = 4
x_times_y = x * y
print(f"The product of {x} and {y} is {x_times_y}")
Output:
The product of 3 and 4 is 12
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}")
Output:
The result of dividing 3 by 4 is 0.75
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}")
Output:
3 raised to the power of 4 is 81
Modulo
x = 7
y = 3
x_modulo_y = x % y
print(f"The remainder when {x} is divided by {y} is {x_modulo_y}")
Output:
The remainder when 7 is divided by 3 is 1
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}")
Output:
The result of bitwise AND between 5 and 3 is 1
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}")
Output:
The result of bitwise OR between 5 and 3 is 7
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}")
Output:
The result of bitwise XOR between 5 and 3 is 6
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}")
Output:
The result of bitwise NOT on 5 is -6
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}")
Output:
The result of left shifting 5 by 1 is 10
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}")
Output:
The result of right shifting 5 by 1 is 2
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}")
Output:
The value of pi is approximately 3.14
Different separator
large_number = 1234567890
print(f"The large number is {large_number:,}")
Output:
The large number is 1,234,567,890
Underscores for readability
large_number = 1234567890
print(f"The large number is {large_number:_}")
Output:
The large number is 1_234_567_890
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)