Python Operators


Operators in Python

Operators are special symbols in Python that perform operations on variables and values. Python supports various types of operators, each performing a different operation on data. These include arithmetic, comparison, logical, assignment, bitwise, identity, and membership operators.

1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations.

OperatorDescriptionExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulus (remainder)a % b
**Exponentiation (power)a ** b
//Floor Division (integer div)a // b

Example:

a = 10 b = 3 # Addition print(a + b) # Output: 13 # Modulus (remainder of the division) print(a % b) # Output: 1 # Exponentiation (10 to the power of 3) print(a ** b) # Output: 1000 # Floor division (division without the remainder) print(a // b) # Output: 3

2. Comparison Operators

Comparison operators compare two values and return either True or False.

OperatorDescriptionExample
==Equal toa == b
!=Not equal toa != b
>Greater thana > b
<Less thana < b
>=Greater than or equal toa >= b
<=Less than or equal toa <= b

Example:

a = 5 b = 10 # Greater than print(a > b) # Output: False # Less than or equal to print(a <= b) # Output: True

3. Logical Operators

Logical operators are used to combine conditional statements.

OperatorDescriptionExample
andReturns True if both conditions are truea and b
orReturns True if at least one condition is truea or b
notReverses the result, returns False if the condition is truenot a

Example:

x = True y = False # Logical AND print(x and y) # Output: False # Logical OR print(x or y) # Output: True # Logical NOT print(not x) # Output: False

4. Assignment Operators

Assignment operators are used to assign values to variables. You can also combine assignment with arithmetic operations.

OperatorDescriptionExample
=Assign valuea = 10
+=Add and assigna += 5
-=Subtract and assigna -= 3
*=Multiply and assigna *= 2
/=Divide and assigna /= 4
%=Modulus and assigna %= 2
**=Exponent and assigna **= 2
//=Floor divide and assigna //= 3

Example:

a = 10 # Add and assign a += 5 # Same as a = a + 5 print(a) # Output: 15 # Multiply and assign a *= 2 # Same as a = a * 2 print(a) # Output: 30

5. Bitwise Operators

Bitwise operators perform operations on binary representations of integers.

OperatorDescriptionExample
&Bitwise ANDa & b
``Bitwise OR
^Bitwise XORa ^ b
~Bitwise NOT~a
<<Bitwise left shifta << 2
>>Bitwise right shifta >> 2

Example:

a = 10 # Binary: 1010 b = 4 # Binary: 0100 # Bitwise AND print(a & b) # Output: 0 (0000) # Bitwise OR print(a | b) # Output: 14 (1110) # Bitwise XOR print(a ^ b) # Output: 14 (1110) # Left shift print(a << 2) # Output: 40 (Binary: 101000) # Right shift print(a >> 2) # Output: 2 (Binary: 0010)

6. Identity Operators

Identity operators are used to compare the memory locations of two objects.

OperatorDescriptionExample
isReturns True if both variables refer to the same objecta is b
is notReturns True if they do not refer to the same objecta is not b

Example:

x = [1, 2, 3] y = [1, 2, 3] z = x # Identity check print(x is z) # Output: True (x and z refer to the same object) print(x is y) # Output: False (x and y are different objects with the same content) print(x is not y) # Output: True

7. Membership Operators

Membership operators are used to test whether a value is present in a sequence (such as a string, list, or tuple).

OperatorDescriptionExample
inReturns True if the value is present in the sequencex in list
not inReturns True if the value is not present in the sequencex not in list

Example:

fruits = ["apple", "banana", "cherry"] # Membership check print("banana" in fruits) # Output: True print("grape" not in fruits) # Output: True

Operator Precedence

Python follows a specific order when evaluating expressions with multiple operators. The order (from highest to lowest) is:

  1. Parentheses: ()
  2. Exponentiation: **
  3. Multiplication, Division, Floor Division, Modulus: *, /, //, %
  4. Addition, Subtraction: +, -
  5. Bitwise Shift: <<, >>
  6. Bitwise AND: &
  7. Bitwise OR, XOR: |, ^
  8. Comparisons: ==, !=, <, >, <=, >=
  9. Identity, Membership: is, is not, in, not in
  10. Logical NOT: not
  11. Logical AND: and
  12. Logical OR: or

Example:

# Operator precedence example result = 10 + 2 * 3 # Output: 16 (Multiplication is evaluated before addition) print(result)

Summary:

  • Python provides several types of operators for performing arithmetic, comparison, logical, and bitwise operations, as well as assignment, identity, and membership tests.
  • Operator precedence determines the order in which operations are evaluated.