Python Operators

In this article, we will discuss Python operators. An operator is a symbol that performs a specific operation on one or more operands. Operators are the building blocks of logic in a program, enabling the execution of various tasks. Python, like other programming languages, has a wide range of operators, including arithmetic, comparison, assignment, logical, bitwise, membership, and identity operators.

1. Arithmetic Operators

Arithmetic operators perform mathematical operations between two operands. They include basic operations like addition, subtraction, multiplication, division, as well as more advanced ones like exponentiation and floor division.

Operator Description
+ Adds two operands. Example: a + b returns the sum of a and b.
- Subtracts the second operand from the first. Example: a - b returns the difference.
* Multiplies two operands. Example: a * b returns the product.
/ Divides the first operand by the second. Example: a / b returns the quotient.
% Returns the remainder of division. Example: a % b returns the remainder.
** Raises the first operand to the power of the second. Example: a ** b returns a to the power of b.
// Performs floor division. Example: a // b returns the floor of the quotient.

Example:

a = 10
b = 3

print("Addition: ", a + b) # Output: 13
print("Subtraction: ", a - b) # Output: 7
print("Multiplication: ", a * b) # Output: 30
print("Division: ", a / b) # Output: 3.3333333333333335
print("Modulus: ", a % b) # Output: 1
print("Exponent: ", a ** b) # Output: 1000
print("Floor Division: ", a // b) # Output: 3

2. Comparison Operators

Comparison operators are used to compare two values, returning a Boolean value (`True` or `False`) based on the comparison.

Operator Description
== Returns True if the values of the two operands are equal.
!= Returns True if the values of the two operands are not equal.
< Returns True if the first operand is less than the second operand.
<= Returns True if the first operand is less than or equal to the second operand.
> Returns True if the first operand is greater than the second operand.
>= Returns True if the first operand is greater than or equal to the second operand.

Example:

a = 10
b = 20

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

3. Assignment Operators

Assignment operators assign the value of the right-hand expression to the left-hand operand. They can also perform an operation and then assign the result.

Operator Description
= Assigns the value of the right-hand expression to the left-hand operand. Example: a = b assigns the value of b to a.
+= Adds the right operand to the left operand and assigns the result to the left operand. Example: a += b is equivalent to a = a + b.
-= Subtracts the right operand from the left operand and assigns the result to the left operand. Example: a -= b is equivalent to a = a - b.
*= Multiplies the right operand with the left operand and assigns the result to the left operand. Example: a *= b is equivalent to a = a * b.
/= Divides the left operand by the right operand and assigns the result to the left operand. Example: a /= b is equivalent to a = a / b.
%= Performs modulus on the left operand by the right operand and assigns the remainder to the left operand. Example: a %= b.
**= Raises the left operand to the power of the right operand and assigns the result to the left operand. Example: a **= b.
//= Performs floor division on the left operand by the right operand and assigns the result to the left operand. Example: a //= b.

Example:

a = 5
b = 10

a += b # a = a + b
print("a += b: ", a) # Output: 15

a -= b # a = a - b
print("a -= b: ", a) # Output: 5

a *= b # a = a * b
print("a *= b: ", a) # Output: 50

a /= b # a = a / b
print("a /= b: ", a) # Output: 5.0

a %= b # a = a % b
print("a %= b: ", a) # Output: 5.0

a **= b # a = a ** b
print("a **= b: ", a) # Output: 9765625.0

a //= b # a = a // b
print("a //= b: ", a) # Output: 976562.0

4. Bitwise Operators

Bitwise operators operate on the individual bits of integer operands. They are useful for low-level programming tasks.

Operator Description
& Bitwise AND. Copies a bit to the result if it exists in both operands.
^ Bitwise XOR. Copies the bit if it is set in one operand but not both.
~ Bitwise NOT. Inverts all the bits of the operand.
<< Left shift. Shifts the bits of the left operand left by the number of positions specified by the right operand.
>> Right shift. Shifts the bits of the left operand right by the number of positions specified by the right operand.

Example:

a = 10 # Binary: 1010
b = 4 # Binary: 0100

print(a & b) # Output: 0
print(a | b) # Output: 14
print(a ^ b) # Output: 14
print(~a) # Output: -11
print(a << 2) # Output: 40 print(a>> 2) # Output: 2

5. Logical Operators

Logical operators are used to combine conditional statements and return a Boolean result.

Operator Description
& Bitwise AND. Copies a bit to the result if it exists in both operands.
^ Bitwise XOR. Copies the bit if it is set in one operand but not both.
~ Bitwise NOT. Inverts all the bits of the operand.
<< Left shift. Shifts the bits of the left operand left by the number of positions specified by the right operand.
>> Right shift. Shifts the bits of the left operand right by the number of positions specified by the right operand.

Example:

a = True
b = False

print(a and b) # Output: False
print(a or b) # Output: True
print(not a) # Output: False

6. Membership Operators

Membership operators test for membership in a sequence, such as strings, lists, or tuples.

Operator Description
in Returns True if the value is found in the specified sequence.
not in Returns True if the value is not found in the specified sequence.

Example:

myList = [1, 2, 3, 4, 5]
print( 3 in myList) # Output: True
print(10 not in myList) # Output: True

7. Identity Operators

Identity operators compare the memory locations of two objects to determine whether they are the same.

Operator Description
is Returns True if the operands refer to the same object in memory.
is not Returns True if the operands do not refer to the same object in memory.

Example:

a = [1, 2, 3]
b = [1, 2, 3]
c = a

print("a is b: ", a is b) # Output: False (a and b are different objects)
print("a is c: ", a is c) # Output: True (a and c are the same object)
print("a is not b: ", a is not b) # Output: True

Operator Precedence

Operator precedence determines the order in which operators are evaluated in an expression. Higher precedence operators are evaluated before lower precedence operators.

Precedence Level Operator(s)
Highest ** (Exponentiation)
~ , + , - (Unary operations)
* , / , % , // (Multiplication, division, modulus, floor division)
+ , - (Addition, subtraction)
> , << (Bitwise shifts)
& (Bitwise AND)
^ , ` (Bitwise XOR)
<= , < ,> , >= (Comparison operators)
== , != (Equality operators)
= (Assignment operators)
is , is not (Identity operators)
in , not in (Membership operators)
Lowest not , and , or (Logical operators)

Example:

a = 10
b = 5
c = 2

result = a + b * c # Multiplication happens before addition
print("Result: ", result) # Output: 20

result = (a + b) * c # Parentheses change the precedence
print("Result with parentheses: ", result) # Output: 30