Lessons
Courses
Notes in current language not found. Defaulting to English.

Operators in Python [ English ]

1. Introduction

Operators in Python are symbols used to perform operations on variables and values. They are essential for performing calculations, comparisons, and logical decisions in programs.

2. Types of Operators in Python

Python provides the following main types of operators:

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

3. Arithmetic Operators

These operators are used to perform mathematical operations.

OperatorMeaningExample
+Addition5 + 2 = 7
-Subtraction5 - 2 = 3
*Multiplication5 * 2 = 10
/Division5 / 2 = 2.5
//Floor Division5 // 2 = 2
%Modulus5 % 2 = 1
**Exponent2 ** 3 = 8

Example

a = 10b = 3 print(a + b)print(a % b)print(a ** b)

4. Comparison Operators

These operators are used to compare values and return True or False.

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

Example

x = 5y = 3 print(x > y)print(x == y)

5. Logical Operators

Used to combine conditional statements.

OperatorMeaning
andTrue if both conditions are True
orTrue if at least one is True
notReverses the result

Example

a = 10 print(a > 5 and a < 20)print(not(a > 5))

6. Assignment Operators

Used to assign values to variables.

OperatorExampleMeaning
=x = 5Assign value
+=x += 3x = x + 3
-=x -= 2x = x - 2
*=x *= 2x = x * 2
/=x /= 2x = x / 2

Example

x = 5x += 3print(x)

7. Membership Operators

Used to check if a value exists in a sequence.

OperatorMeaning
inValue exists
not inValue does not exist

Example

text = "Python" print("P" in text)print("z" not in text)

8. Identity Operators

Used to compare memory locations of objects.

OperatorMeaning
isSame object
is notDifferent objects

Example

a = 10b = 10 print(a is b)

9. Operator Precedence

Operator precedence determines which operation is performed first.

Order (high to low):

  1. ** (Exponent)
  2. *, /, //, %
  3. +, -
  4. Comparison operators
  5. Logical operators

Example

result = 2 + 3 * 4print(result)

Output

14

10. Summary

  • Operators are used to perform operations on data.

  • Types include:

    • Arithmetic
    • Comparison
    • Logical
    • Assignment
    • Membership
    • Identity
  • Operator precedence controls execution order.

  • Operators are essential for calculations, conditions, and logic building.