A8. Python Operators


In this tutorial will explore the various types of operators in Python. We will cover:

  • The syntax of different operators
  • How to use each operator with examples

By the end of this tutorial, you will understand how to use arithmetic, comparison, logical, identity, membership, and bitwise operators in Python to perform calculations and logical evaluations on data.

The rewritten version provides more detail on the specific topics that will be covered in the tutorial and the outcomes the reader will achieve by following along.

The original is very brief, so expanding on the key points that will be discussed helps set more context for the reader.

Python Arithematic Operator

Arithmetic operators are used to perform mathematical operations on numeric data types such as integers and floating-point numbers. Here are the most common arithmetic operators in Python:

OperatorDescription
+Addition
-Subtraction
*Multiplication
/Division
%Modulus (returns the remainder of a division operation)
**Exponentiation (raises a number to a power)

Below are some examples of using arithmetic operators in Python:

# Addition
a = 5
b = 3
c = a + b
print(c)  # Output: 8

# Subtraction
d = 7
e = 2
f = d - e
print(f)  # Output: 5

# Multiplication
g = 4
h = 6
i = g * h
print(i)  # Output: 24

# Division
j = 10
k = 3
l = j / k
print(l)  # Output: 3.3333333333333335

# Modulus
m = 10
n = 3
o = m % n
print(o)  # Output: 1

# Exponentiation
p = 2
q = 3
r = p ** q
print(r)  # Output: 8

In the examples above, we used arithmetic operators to perform various calculations on numeric values. The results of the operations were then printed to the console using the print() function.

Keep in mind that operator precedence and associativity can affect the order in which operations are performed. You can use parentheses to group operations and ensure that they are performed in the desired order.

Python Assignment Operators

Assignment operators are used to assign a value to a variable. They combine an arithmetic operator with the assignment operator (=) to perform an operation and assign the result to a variable in a single step.

Here are some examples of common assignment operators in Python:

OperatorExampleEquivalent To
=x = 5x = 5
+=x += 3x = x + 3
-=x -= 3x = x - 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3
%=x %= 3x = x % 3
**=x **= 3x = x ** 3

Here are some examples of using assignment operators in Python:

# Simple assignment
x = 5
print(x)  # Output: 5

# Addition assignment
x += 3
print(x)  # Output: 8

# Multiplication assignment
x *= 2
print(x)  # Output: 16

# Exponentiation assignment
x **= 2
print(x)  # Output: 256

In the examples above, we used assignment operators to perform arithmetic operations and assign the results to the variable x. The results of the operations were then printed to the console using the print() function. If you feel like exploring python print() check the link

Assignment operators are a convenient way to perform an operation and assign the result to a variable in a single step, which can help make your code more concise and easier to read.

Python Comparison Operators

Comparison operators are used to compare two values and return a Boolean value (True or False) depending on whether the comparison is true or false.

Let’s discuss the comparison operators in Python:

OperatorDescription
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

Here are some examples of using comparison operators in Python:

# Equal to
a = 5
b = 5
print(a == b)  # Output: True

# Not equal to
c = 7
d = 5
print(c != d)  # Output: True

# Greater than
e = 10
f = 5
print(e > f)  # Output: True

# Less than
g = 3
h = 5
print(g < h)  # Output: True

# Greater than or equal to
i = 7
j = 7
print(i >= j)  # Output: True

# Less than or equal to
k = 3
l = 5
print(k <= l)  # Output: True

In the examples above, we used comparison operators to compare two values and return a Boolean value.

Comparison operators are often used in conditional statements, such as if statements and loops, to control the flow of a program based on the value of a variable or the result of a comparison.

Python Logical Operators

Logical operators are used to combine multiple conditions and return a Boolean value (True or False) depending on whether the conditions are true or false.

Check out the common logical operators in Python:

OperatorDescription
andReturns True if both conditions are true
orReturns True if at least one condition is true
notReturns True if the condition is false, and vice versa

Here are some examples of using logical operators in Python:

# and operator
a = 5
b = 10
c = 7
print(a < b and b < c)  # Output: False

# or operator
d = 3
e = 6
f = 9
print(d < e or e > f)  # Output: True

# not operator
g = 5
h = 7
print(not g < h)  # Output: False

In the examples above, we used logical operators to combine multiple conditions and return a Boolean value.

Logical operators are often used in conditional statements, such as if statements and loops, to control the flow of a program based on multiple conditions or the result of a comparison.

More example for Logical operators :

# logical AND
print(True and True)     # True
print(True and False)    # False

# logical OR
print(True or False)     # True

# logical NOT
print(not True)          # False

Python Bitwise operators

Bitwise operators are used to perform bitwise operations on binary numbers. Bitwise operators work on individual bits of a number and return a new binary number as the result.

Below are bitwise operators in Python:

OperatorDescription
&Bitwise AND
``
^Bitwise XOR
~Bitwise NOT
<<Bitwise left shift
>>Bitwise right shift

Here are some examples of using bitwise operators in Python:

# Bitwise AND
a = 5  # 101 in binary
b = 3  # 011 in binary
c = a & b
print(c)  # Output: 1

# Bitwise OR
d = 5  # 101 in binary
e = 3  # 011 in binary
f = d | e
print(f)  # Output: 7

# Bitwise XOR
g = 5  # 101 in binary
h = 3  # 011 in binary
i = g ^ h
print(i)  # Output: 6

# Bitwise NOT
j = 5  # 101 in binary
k = ~j
print(k)  # Output: -6 (in two's complement form)

# Bitwise left shift
l = 5  # 101 in binary
m = l << 2
print(m)  # Output: 20 (10100 in binary)

# Bitwise right shift
n = 5  # 101 in binary
o = n >> 1
print(o)  # Output: 2 (10 in binary)

In the examples above, we used bitwise operators to perform bitwise operations on binary numbers.

Bitwise operators are often used in low-level programming and to perform bit-level manipulations on binary data.

Python Special operators

In Python, there are some special operators that are used for specific purposes.

Common special operators in Python:

OperatorDescription
isReturns True if two variables refer to the same object
is notReturns True if two variables do not refer to the same object
inReturns True if a value is present in a sequence
not inReturns True if a value is not present in a sequence

Here are some examples of using special operators in Python:

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

# is not operator
c = [1, 2, 3]
d = [1, 2, 3]
print(c is not d)  # Output: True

# in operator
e = [1, 2, 3]
f = 2
print(f in e)  # Output: True

# not in operator
g = [1, 2, 3]
h = 4
print(h not in g)  # Output: True

In the examples above, we used special operators to perform specific operations.

Bonus : Python Membership operators

Membership operators are used to test whether a value is a member of a sequence or not. Here are the two membership operators in Python:

OperatorDescription
inEvaluates to True if a value is found in the sequence
not inEvaluates to True if a value is not found in the sequence

Here are some examples of using membership operators in Python:

# in operator
a = [1, 2, 3]
b = 2
print(b in a)  # Output: True

# not in operator
c = [1, 2, 3]
d = 4
print(d not in c)  # Output: True

In the examples above, we used membership operators to test whether a value is a member of a sequence or not.

Membership operators/Special operators are often used in conditional statements, such as if statements and loops, to check whether a value is present in a list or other sequence.