Operators are symbols or keywords in Python used to perform operations on variables and values. Python provides a wide range of operators that are grouped into different categories based on their functionality.


Types of Python Operators

  1. Arithmetic Operators
  2. Comparison (Relational) Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Assignment Operators
  6. Membership Operators
  7. Identity Operators

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, etc.

OperatorNameDescriptionExample
+AdditionAdds two values5 + 3 → 8
-SubtractionSubtracts the second value from the first5 - 3 → 2
*MultiplicationMultiplies two values5 * 3 → 15
/DivisionDivides the first value by the second (float result)5 / 2 → 2.5
//Floor DivisionDivides and truncates the decimal part5 // 2 → 2
%ModulusReturns the remainder5 % 2 → 1
**ExponentiationRaises the first value to the power of the second2 ** 3 → 8

2. Comparison (Relational) Operators

Comparison operators compare two values and return a Boolean result (True or False).

OperatorNameDescriptionExample
==Equal toChecks if two values are equal5 == 5 → True
!=Not equal toChecks if two values are not equal5 != 3 → True
>Greater thanChecks if the first value is greater5 > 3 → True
<Less thanChecks if the first value is smaller5 < 3 → False
>=Greater than or equal toChecks if the first value is greater or equal5 >= 5 → True
<=Less than or equal toChecks if the first value is smaller or equal5 <= 3 → False

3. Logical Operators

Logical operators are used to combine conditional statements.

OperatorNameDescriptionExample
andLogical ANDReturns True if both conditions are True(5 > 3 and 3 > 1) → True
orLogical ORReturns True if at least one condition is True(5 > 3 or 3 < 1) → True
notLogical NOTReverses the Boolean valuenot(5 > 3) → False

4. Bitwise Operators

Bitwise operators work at the binary level.

OperatorNameDescriptionExample (Binary)
&ANDPerforms binary AND5 & 3 → 1 (0101 & 0011 → 0001)
``ORPerforms binary OR
^XORPerforms binary XOR5 ^ 3 → 6 (0101 ^ 0011 → 0110)
~NOTPerforms binary NOT (1’s complement)~5 → -6
<<Left ShiftShifts bits to the left5 << 1 → 10
>>Right ShiftShifts bits to the right5 >> 1 → 2

5. Assignment Operators

Assignment operators are used to assign values to variables.

OperatorNameExampleExplanation
=Assignx = 5Assigns 5 to x.
+=Add and Assignx += 3Equivalent to x = x + 3.
-=Subtract and Assignx -= 2Equivalent to x = x - 2.
*=Multiply and Assignx *= 4Equivalent to x = x * 4.
/=Divide and Assignx /= 2Equivalent to x = x / 2.
//=Floor Divide and Assignx //= 3Equivalent to x = x // 3.
%=Modulus and Assignx %= 2Equivalent to x = x % 2.
**=Exponent and Assignx **= 3Equivalent to x = x ** 3.
&=Bitwise AND and Assignx &= 2Equivalent to x = x & 2.
`=`Bitwise OR and Assign`x
^=Bitwise XOR and Assignx ^= 3Equivalent to x = x ^ 3.
>>=Right Shift and Assignx >>= 1Equivalent to x = x >> 1.
<<=Left Shift and Assignx <<= 1Equivalent to x = x << 1.

6. Membership Operators

Membership operators test for the presence of a value in a sequence (e.g., list, string).

OperatorNameDescriptionExample
inInReturns True if a value exists in a sequence'P' in "Python" → True
not inNot InReturns True if a value does not exist in a sequence'X' not in "Python" → True

7. Identity Operators

Identity operators check whether two variables refer to the same object in memory.

OperatorNameDescriptionExample
isIsReturns True if both variables point to the same objectx is y
is notIs NotReturns True if variables point to different objectsx is not y

Precedence of Operators

Operator precedence determines the order of operations in an expression. Higher precedence operators are evaluated first.

Precedence LevelOperators
1 (Highest)**
2~, +, - (Unary operators)
3*, /, //, %
4+, -
5>>, <<
6&
7^
8`
9==, !=, >, <, >=, <=
10not
11and
12 (Lowest)or

Examples

Example 1: Arithmetic Operations

a = 10
b = 3

print(a + b)  # Output: 13
print(a % b)  # Output: 1
print(a ** b) # Output: 1000

Example 2: Logical Operators

x = 5
y = 10

print(x > 2 and y < 20)  # Output: True
print(not(x == 5))       # Output: False

Example 3: Membership Operators

text = "Python"
print('P' in text)       # Output: True
print('X' not in text)   # Output: True

Example 4: Identity Operators

x = [1, 2, 3]
y = x
z = [1, 2, 3]

print(x is y)  # Output: True
print(x is z)  # Output: False

This explanation

provides a structured overview of Python operators with practical examples for a beginner-friendly Python tutorial. Let me know if you’d like more detailed examples!

Operators are symbols or keywords in Python used to perform operations on variables and values. Python provides a wide range of operators that are grouped into different categories based on their functionality.


Types of Python Operators

  1. Arithmetic Operators
  2. Comparison (Relational) Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Assignment Operators
  6. Membership Operators
  7. Identity Operators

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, etc.

OperatorNameDescriptionExample
+AdditionAdds two values5 + 3 → 8
-SubtractionSubtracts the second value from the first5 - 3 → 2
*MultiplicationMultiplies two values5 * 3 → 15
/DivisionDivides the first value by the second (float result)5 / 2 → 2.5
//Floor DivisionDivides and truncates the decimal part5 // 2 → 2
%ModulusReturns the remainder5 % 2 → 1
**ExponentiationRaises the first value to the power of the second2 ** 3 → 8

2. Comparison (Relational) Operators

Comparison operators compare two values and return a Boolean result (True or False).

OperatorNameDescriptionExample
==Equal toChecks if two values are equal5 == 5 → True
!=Not equal toChecks if two values are not equal5 != 3 → True
>Greater thanChecks if the first value is greater5 > 3 → True
<Less thanChecks if the first value is smaller5 < 3 → False
>=Greater than or equal toChecks if the first value is greater or equal5 >= 5 → True
<=Less than or equal toChecks if the first value is smaller or equal5 <= 3 → False

3. Logical Operators

Logical operators are used to combine conditional statements.

OperatorNameDescriptionExample
andLogical ANDReturns True if both conditions are True(5 > 3 and 3 > 1) → True
orLogical ORReturns True if at least one condition is True(5 > 3 or 3 < 1) → True
notLogical NOTReverses the Boolean valuenot(5 > 3) → False

4. Bitwise Operators

Bitwise operators work at the binary level.

OperatorNameDescriptionExample (Binary)
&ANDPerforms binary AND5 & 3 → 1 (0101 & 0011 → 0001)
``ORPerforms binary OR
^XORPerforms binary XOR5 ^ 3 → 6 (0101 ^ 0011 → 0110)
~NOTPerforms binary NOT (1’s complement)~5 → -6
<<Left ShiftShifts bits to the left5 << 1 → 10
>>Right ShiftShifts bits to the right5 >> 1 → 2

5. Assignment Operators

Assignment operators are used to assign values to variables.

OperatorNameExampleExplanation
=Assignx = 5Assigns 5 to x.
+=Add and Assignx += 3Equivalent to x = x + 3.
-=Subtract and Assignx -= 2Equivalent to x = x - 2.
*=Multiply and Assignx *= 4Equivalent to x = x * 4.
/=Divide and Assignx /= 2Equivalent to x = x / 2.
//=Floor Divide and Assignx //= 3Equivalent to x = x // 3.
%=Modulus and Assignx %= 2Equivalent to x = x % 2.
**=Exponent and Assignx **= 3Equivalent to x = x ** 3.
&=Bitwise AND and Assignx &= 2Equivalent to x = x & 2.
`=`Bitwise OR and Assign`x
^=Bitwise XOR and Assignx ^= 3Equivalent to x = x ^ 3.
>>=Right Shift and Assignx >>= 1Equivalent to x = x >> 1.
<<=Left Shift and Assignx <<= 1Equivalent to x = x << 1.

6. Membership Operators

Membership operators test for the presence of a value in a sequence (e.g., list, string).

OperatorNameDescriptionExample
inInReturns True if a value exists in a sequence'P' in "Python" → True
not inNot InReturns True if a value does not exist in a sequence'X' not in "Python" → True

7. Identity Operators

Identity operators check whether two variables refer to the same object in memory.

OperatorNameDescriptionExample
isIsReturns True if both variables point to the same objectx is y
is notIs NotReturns True if variables point to different objectsx is not y

Precedence of Operators

Operator precedence determines the order of operations in an expression. Higher precedence operators are evaluated first.

Precedence LevelOperators
1 (Highest)**
2~, +, - (Unary operators)
3*, /, //, %
4+, -
5>>, <<
6&
7^
8`
9==, !=, >, <, >=, <=
10not
11and
12 (Lowest)or

Examples

Example 1: Arithmetic Operations

a = 10
b = 3

print(a + b)  # Output: 13
print(a % b)  # Output: 1
print(a ** b) # Output: 1000

Example 2: Logical Operators

x = 5
y = 10

print(x > 2 and y < 20)  # Output: True
print(not(x == 5))       # Output: False

Example 3: Membership Operators

text = "Python"
print('P' in text)       # Output: True
print('X' not in text)   # Output: True

Example 4: Identity Operators

x = [1, 2, 3]
y = x
z = [1, 2, 3]

print(x is y)  # Output: True
print(x is z)  # Output: False

This explanation

provides a structured overview of Python operators with practical examples for a beginner-friendly Python tutorial. Let me know if you’d like more detailed examples!