Notes
Categories

Operators in Python [ English ]

< Prev Next >

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.

Operator Meaning Example
+ Addition 5 + 2 = 7
- Subtraction 5 - 2 = 3
* Multiplication 5 * 2 = 10
/ Division 5 / 2 = 2.5
// Floor Division 5 // 2 = 2
% Modulus 5 % 2 = 1
** Exponent 2 ** 3 = 8

Example

a = 10
b = 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.

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

Example

x = 5
y = 3

print(x > y)
print(x == y)

5. Logical Operators

Used to combine conditional statements.

Operator Meaning
and True if both conditions are True
or True if at least one is True
not Reverses the result

Example

a = 10

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

6. Assignment Operators

Used to assign values to variables.

Operator Example Meaning
= x = 5 Assign value
+= x += 3 x = x + 3
-= x -= 2 x = x - 2
*= x *= 2 x = x * 2
/= x /= 2 x = x / 2

Example

x = 5
x += 3
print(x)

7. Membership Operators

Used to check if a value exists in a sequence.

Operator Meaning
in Value exists
not in Value 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.

Operator Meaning
is Same object
is not Different objects

Example

a = 10
b = 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 * 4
print(result)

Output

14

10. Summary

< Prev Next >