Arithmetic Operators in Python

arithmetic operators

Python, a powerful and versatile programming language, is widely appreciated for its simplicity and readability. One of its fundamental building blocks is the use of operators, which are special symbols or keywords used to perform operations on variables and values. Operators are crucial for manipulating data, controlling program flow, and solving computational problems. This article provides a comprehensive overview of the basic operators in Python, categorized for clarity and ease of understanding.


1. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, and division.

Operator Description Example
+ Addition 5 + 3 = 8
- Subtraction 5 - 3 = 2
* Multiplication 5 * 3 = 15
/ Division 5 / 2 = 2.5
% Modulus (remainder) 5 % 2 = 1
// Floor division (quotient) 5 // 2 = 2
** Exponentiation (power) 5 ** 3 = 125

Examples:

x = 10
y = 3
print(x + y)  # Output: 13
print(x % y)  # Output: 1
print(x ** y) # Output: 1000

2. Comparison Operators

Comparison operators compare two values and return a Boolean result: True or False.

Operator Description Example
== Equal to 5 == 3 is False
!= Not equal to 5 != 3 is True
> Greater than 5 > 3 is True
< Less than 5 < 3 is False
>= Greater than or equal to 5 >= 3 is True
<= Less than or equal to 5 <= 3 is False

Examples:

a = 7
b = 10
print(a > b)  # Output: False
print(a <= b) # Output: True

3. Logical Operators

Logical operators are used to combine conditional statements.

Operator Description Example
and Returns True if both conditions are True (5 > 3 and 2 < 4) is True
or Returns True if at least one condition is True (5 > 3 or 2 > 4) is True
not Reverses the result of a condition not(5 > 3) is False

Examples:

x = True
y = False
print(x and y)  # Output: False
print(x or y)   # Output: True
print(not x)    # Output: False

4. Assignment Operators

Assignment operators are used to assign values to variables.

Operator Description Example
= Assigns a value x = 5
+= Adds and assigns x += 3 (x = x + 3)
-= Subtracts and assigns x -= 3 (x = x – 3)
*= Multiplies and assigns x *= 3 (x = x * 3)
/= Divides and assigns x /= 3 (x = x / 3)
//= Floor divides and assigns x //= 3 (x = x // 3)
**= Exponentiates and assigns x **= 3 (x = x ** 3)
%= Finds remainder and assigns x %= 3 (x = x % 3)

Examples:

x = 10
x += 5
print(x)  # Output: 15
x **= 2
print(x)  # Output: 225

5. Bitwise Operators

Bitwise operators operate on binary representations of numbers.

Operator Description Example
& Bitwise AND 5 & 3 = 1
` ` Bitwise OR `5 3 = 7`
^ Bitwise XOR 5 ^ 3 = 6
~ Bitwise NOT ~5 = -6
<< Left shift 5 << 1 = 10
>> Right shift 5 >> 1 = 2

Examples:

a = 5  # Binary: 101
b = 3  # Binary: 011
print(a & b)  # Output: 1 (Binary: 001)
print(a | b)  # Output: 7 (Binary: 111)

6. Membership Operators

Membership operators check if a value exists in a sequence such as a list, tuple, or string.

Operator Description Example
in Returns True if value exists 'a' in 'apple' is True
not in Returns True if value does not exist 'x' not in 'apple' is True

Examples:

fruits = ['apple', 'banana', 'cherry']
print('apple' in fruits)  # Output: True
print('mango' not in fruits)  # Output: True

7. Identity Operators

Identity operators compare the memory locations of two objects.

Operator Description Example
is Returns True if objects are identical x is y
is not Returns True if objects are not identical x is not y

Examples:

a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b)  # Output: True
print(a is c)  # Output: False

8. Special Operator Precedence

Operator precedence determines the order in which operations are executed. Operators with higher precedence are evaluated first.

Precedence Table (High to Low):

  1. ** (Exponentiation)
  2. ~ + - (Unary operators)
  3. * / // % (Multiplication, Division, Modulus)
  4. + - (Addition, Subtraction)
  5. >> << (Bitwise shifts)
  6. & (Bitwise AND)
  7. ^ (Bitwise XOR)
  8. | (Bitwise OR)
  9. == != > >= < <= is is not in not in

Conclusion

Understanding Python operators is fundamental for writing efficient and readable code. From arithmetic to logical and bitwise operators, each category serves a distinct purpose. By mastering these basic operators, developers can manipulate data, implement algorithms, and solve real-world problems effectively. Practice and experimentation are key to becoming proficient in their usage.

Useful links:

Subscribe our channel : https://www.youtube.com/@SAIDataScience

https://www.w3schools.com/python/

Write a comment