Python Operators

  • Read
  • Discuss

Python has many operators that allow you to perform operations on variables and values. This tutorial will cover the following operators:

  1. Arithmetic operators
  2. Comparison operators
  3. Assignment operators
  4. Logical operators
  5. Membership operators
  6. Identity operators
  7. Bitwise operators

1. Arithmetic Operators

Arithmetic operators allow you to perform mathematical operations on variables and values. Python has the following arithmetic operators:

OperatorMeaningExample
+Addition2 + 3
Subtraction4 – 2
*Multiplication3 * 4
/Division (float result)8 / 4
//Division (floor division)8 // 4
%Modulus (remainder)8 % 4
**Exponent (power)2 ** 3

Here are some examples of using arithmetic operators:

print(2 + 3)  # addition
5
print(5 - 2)  # subtraction
3
print(3 * 4)  # multiplication
12
print(6 / 2)  # division
3.0
print(7 // 3)  # floor division
2
print(10 % 3)  # modulus
1
print(2 ** 3)  # exponentiation
8

2. Comparison Operators

Comparison operators allow you to compare two values and return a boolean value indicating whether the comparison is True or False. Python has the following comparison operators:

OperatorMeaningExample
==Equal to3 == 4
!=Not equal to3 != 4
>Greater than3 > 4
<Less than3 < 4
>=Greater than or equal to3 >= 4
<=Less than or equal to3 <= 4

Here are some examples of using comparison operators:

print(2 == 2)  # equal to
True
print(2 != 3)  # not equal to
True
print(3 > 2)  # greater than
True
print(2 < 3)  # less than
True
print(3 >= 3)  # greater than or equal to
True
print(2 <= 1)  # less than or equal to
False

3. Assignment Operators

Assignment operators allow you to assign a value to a variable. Python has the following assignment operators:

OperatorMeaningExample
=Assign valuex = 5
+=Add and assignx += 5
-=Subtract and assignx -= 5
*=Multiply and assignx *= 5
/=Divide and assignx /= 5
//=Floor divide and assignx //= 5
%=Modulus and assignx %= 5
**=Exponent and assignx **= 5

Here are some examples of using assignment operators:

x = 2  # simple assignment
x += 3  # addition assignment
print(x)
5
x -= 1  # subtraction assignment
print(x)
4
print(x *= 2)  # multiplication assignment
x
8
x /= 4  # division assignment
print(x)
2.0
x //= 2  # floor division assignment
print(x)
1.0
x %= 2  # modulus assignment
print(x)
1.0
x **= 3  # exponentiation assignment
print(x)
1.0

4. Logical Operators

Logical operators allow you to combine multiple boolean expressions and return a boolean value. Python has the following logical operators:

OperatorMeaningExample
andBoth conditions are TrueTrue and False
orAt least one condition is TrueTrue or False
notReverse the conditionnot True

The and operator evaluates to True if both of the expressions on either side of the operator are True, and False otherwise. For example:

x = 5
y = 3
print(x > 0 and y > 0)s
True

The or operator evaluates to True if at least one of the expressions on either side of the operator is True, and False otherwise. For example

x = 5
y = 3
print(x > 0 or y > 0)
True

The not operator negates the Boolean value of an expression. If the expression is True, not will evaluate as False, and vice versa. 

For example:

x = 5
print(not x > 0)
False

5. Membership Operators

In Python, there are two membership operators.

The membership operators are in and not in. These operators are used to test if an object is a group member (such as a list, tuple, set, or dictionary).

For example:

# Test if an element is in a list
print(1 in [1, 2, 3])
True
print(4 in [1, 2, 3])
False

# Test if an element is not in a list
print(4 not in [1, 2, 3])
True
print(1 not in [1, 2, 3])
False

# Test if a key is in a dictionary
print('a' in {'a': 1, 'b': 2})
True
print('c' in {'a': 1, 'b': 2})
False

6. Identity Operators

There are two membership operators in python.

The identity operators are is and is not. These operators are used to test if two objects are the same object in memory. This is different from testing if two objects have the same value.

For example:

# Test if two variables are the same object
a = [1, 2, 3]
b = a
print(a is b)
True

# Test if two variables are not the same object
a = [1, 2, 3]
b = [1, 2, 3]
print(a is b)
False

# Test if two variables are the same object with different values
a = [1, 2, 3]
b = a
a[0] = 5
print(a is b)
True
print(a == b)
False

7. Bitwise Operators

In Python, the bitwise operators are used to performing bitwise operations on integers. The bitwise operators are

OperatorMeaning
&(bitwise AND)
(bitwise OR)
^(bitwise XOR)
~(bitwise NOT)
<<(left shift)
>>(right shift)

Here is an example of how to use the bitwise AND operator in Python:

x = 10  # 1010 in binary
y = 4   # 0100 in binary
z = x & y  # z will be 0print(z)0

The & operator compares each bit of the first number to the corresponding bit of the second number. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.

Here is an example of how to use the bitwise OR operator in Python:

x = 10  # 1010 in binary
y = 4   # 0100 in binary
z = x | y  # z will be 14print(z)14

The | operator compares each bit of the first number to the corresponding bit of the second number. If either bit is 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.

Here is an example of how to use the bitwise XOR operator in Python:

x = 10  # 1010 in binary
y = 4   # 0100 in binary
z = x ^ y  # z will be 14print(z)14

The ^ operator compares each bit of the first number to the corresponding bit of the second number. If the bits are different, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.

Here is an example of how to use the bitwise NOT operator in Python:

x = 10  # 1010 in binary
y = ~x  # y will be -11print(y)y

The ~ operator flips all of the bits in a number. The resulting number will be the two’s complement of the original number.

Here is an example of how to use the left shift operator in Python:

x = 10  # 1010 in binary
y = x << 2  # y will be 40print(y)40

The << operator shifts the bits of a number to the left by a specified number of places.

Here is an example of how to use the right shift operator in Python:

x = 10  # 1010 in binary
y = x >> 2  # y will be 2print(y)2

The >> operator shifts the bits of a number to the right by a specified number of places.

1 thought on “Python Operators”

Leave a Reply

Scroll to Top