Operators in Python

Reading Time: 2 minutes

Operators

Operators are special symbols used to perform operations on variables and values in Python. They help us perform calculations, comparisons, and logical decisions in a program.

Arithmetic Operators

These operators are used to perform mathematical operations.

OperatorMeaningExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulus (remainder)a % b
**Exponent (power)a ** b
//Floor divisiona // b
a = 10
b = 3

print(a + b)   # 13
print(a - b)   # 7
print(a * b)   # 30
print(a / b)   # 3.33
print(a % b)   # 1
print(a ** b)  # 1000
print(a // b)  # 3

Comparison Operators

These operators compare two values and return True or False.

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

Example:

a = 10
b = 5

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

Logical Operators

Logical operators are used to combine multiple conditions.

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

Example:

a = 10

print(a > 5 and a < 20)   # True
print(a > 15 or a < 20)   # True
print(not(a > 5))         # False

Assignment Operators

These operators are 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 = 10

x += 5
print(x)  # 15

x *= 2
print(x)  # 30