Operators and Expressions in Python

In Python, operators are special symbols or reserved words that perform specific actions on operands (values or variables) to create expressions. Expressions are combinations of operators and operands that evaluate to a value. Here are some of the most commonly used operators and expressions in Python:

  1. Arithmetic Operators:

Arithmetic operators are used to perform mathematical operations on numerical values. The common arithmetic operators in Python are:

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulus (%)
  • Exponentiation (**)
  • Floor Division (//)

Example:

a = 10
b = 3

print(a + b)   # Output: 13
print(a - b)   # Output: 7
print(a * b)   # Output: 30
print(a / b)   # Output: 3.3333333333333335
print(a % b)   # Output: 1
print(a ** b)  # Output: 1000
print(a // b)  # Output: 3
  1. Comparison Operators:

Comparison operators are used to compare two values and return a Boolean value (True or False). The common comparison operators in Python are:

  • Equal to (==)
  • Not equal to (!=)
  • Greater than (>)
  • Less than (<)
  • Greater than or equal to (>=)
  • Less than or equal to (<=)

Example:

a = 10
b = 3

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

Difference between == and = Operator

===
It is an assignment operatorIt is a comparison operator
It is used for assigning the value to a variableIt is used for comparing two values.  It returns 1 if both the value is equal otherwise returns 0
Constant term cannot be place on left hand side Example: 1= x; is invalidConstant term can be placed in the left-hand side. Example: 1 ==1 is valid and return 1
  1. Logical Operators:

Logical operators are used to combine multiple conditions and return a Boolean value. The common logical operators in Python are:

  • and
  • or
  • not

Example:

a = 10
b = 3

print(a > 5 and b > 2)   # Output: True
print(a > 5 or b > 5)    # Output: True
print(not(a > 5))        # Output: False
  1. Assignment Operators:

Assignment operators are used to assign values to variables. The common assignment operators in Python are:

  • = (Simple Assignment)
  • += (Addition Assignment)
  • -= (Subtraction Assignment)
  • *= (Multiplication Assignment)
  • /= (Division Assignment)
  • %= (Modulus Assignment)
  • **= (Exponentiation Assignment)
  • //= (Floor Division Assignment)

Example:

a = 10
b = 3

a += b   # equivalent to a = a + b
print(a) # Output: 13

a **= b  # equivalent to a = a ** b
print(a) # Output: 2197

5. Bitwise Operators

In Python, bitwise operators perform operations on the binary representation of integer values. They operate on each bit of the binary representation separately. Here are the commonly used bitwise operators in Python:

  1. Bitwise AND (&):

The bitwise AND operator returns a 1 in each bit position where both corresponding bits in the operands are 1, otherwise, it returns a 0.

Example:

a = 60   # 0011 1100
b = 13   # 0000 1101
c = a & b # 0000 1100
print(c)  # Output: 12

2. Bitwise OR (|):

The bitwise OR operator returns a 1 in each bit position where at least one corresponding bit in the operands is 1, otherwise, it returns a 0.

Example:

a = 60   # 0011 1100
b = 13   # 0000 1101
c = a | b # 0011 1101
print(c)  # Output: 61

3. Bitwise XOR (^):

The bitwise XOR operator returns a 1 in each bit position where only one corresponding bit in the operands is 1, otherwise, it returns a 0.

Example:

a = 60   # 0011 1100
b = 13   # 0000 1101
c = a ^ b # 0011 0001
print(c)  # Output: 49

4. Bitwise NOT (~):

The bitwise NOT operator returns the complement of the binary representation of the operand.

Example:

a = 60   # 0011 1100
b = ~a   # 1100 0011
print(b)  # Output: -61

Note that the result is a negative value because the bitwise NOT operator changes the sign bit of the binary representation of the operand.

  1. Left Shift (<<):

The left shift operator shifts the binary representation of the left operand to the left by the number of bits specified by the right operand.

Example:

a = 60    # 0011 1100
b = a << 2 # 1111 0000
print(b)   # Output: 240
  1. Right Shift (>>):

The right shift operator shifts the binary representation of the left operand to the right by the number of bits specified by the right operand.

Example:

a = 60    # 0011 1100
b = a >> 2 # 0000 1111
print(b)   # Output: 15

These are the common bitwise operators in Python. Understanding them will help you write more efficient and effective Python programs.

6. Identity Operators

In Python, identity operators are used to compare the memory locations of two objects. These operators return True if the objects referred to by the variables on either side of the operator are the same object, otherwise, they return False. Here are the two identity operators in Python:

  1. is Operator:

The is operator returns True if the variables on either side of the operator refer to the same object, otherwise, it returns False.

Example:

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

print(a is b) # Output: True
print(a is c) # Output: False

In the example above, the is operator returns True when comparing a and b because they both refer to the same object in memory, while it returns False when comparing a and c because they refer to different objects in memory.

2. is not Operator:

The is not operator returns True if the variables on either side of the operator do not refer to the same object, otherwise, it returns False.

Example:

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

print(a is not b) # Output: False
print(a is not c) # Output: True

In the example above, the is not operator returns False when comparing a and b because they both refer to the same object in memory, while it returns True when comparing a and c because they refer to different objects in memory.

Identity operators are useful when you need to check whether two variables refer to the same object in memory or not.

7.Membership Operators

In Python, membership operators are used to test whether a value is a member of a sequence or not. The two membership operators in Python are:

  1. in Operator:

The in operator returns True if a value is found in a sequence, otherwise, it returns False.

Example:

a = [1, 2, 3, 4, 5]
b = 3
c = 6

print(b in a) # Output: True
print(c in a) # Output: False

In the example above, the in operator returns True when checking if b is in the list a, while it returns False when checking if c is in the list a.

2. not in Operator:

The not in operator returns True if a value is not found in a sequence, otherwise, it returns False.

Example:

a = [1, 2, 3, 4, 5]
b = 3
c = 6

print(b not in a) # Output: False
print(c not in a) # Output: True

In the example above, the not in operator returns False when checking if b is not in the list a, while it returns True when checking if c is not in the list a.

Membership operators are useful when you need to check if a value is present or not in a sequence, such as a list, tuple, or string.