Home Tutorials Python Tutorial Operators
Operators

Operators


Definition: Operators are special symbols in Python that are used to perform calculations, compare values, and manipulate data.

Why: Operators are the building blocks of logic in any program. They allow you to perform math, make comparisons, and combine logical conditions to control how your code behaves.

Main Operator Groups

  • Arithmetic Operators: Used for mathematical calculations: + (Add), - (Subtract), * (Multiply), / (Divide), % (Modulus), ** (Exponent), // (Floor Division).
  • Comparison Operators: Used to compare two values: == (Equal), != (Not Equal), > (Greater Than), < (Less Than), >=, <=.
  • Logical Operators: Used to combine conditional statements: and, or, not.
  • Assignment Operators: Used to assign values to variables: =, +=, -=, *=, /=.

Example

x = 10
y = 3

print(x + y)  # Arithmetic: 13
print(x / y)  # Arithmetic: 3.3333...
print(x % y)  # Arithmetic (Remainder): 1
print(x > y)  # Comparison: True
print(x == y) # Comparison: False

Explanation

  • x + y adds the two numbers together.
  • x / y performs a standard division, resulting in a float.
  • x % y (Modulus) returns the remainder left over after division.
  • x > y checks if 10 is greater than 3, which results in a Boolean value (True).

Key Notes

  • The = operator is for assignment (setting a value), while == is for comparison (checking if values are equal).
  • Floor division (//) rounds the result down to the nearest whole number.
  • Logical and only returns True if both conditions being checked are true.
Example

🏋️ Test Yourself With Exercises

Take our quiz on Operators to test your knowledge.

Exercise »