Operators in Programming

🚀 What are Operators?

Operators are special symbols that perform operations on variables and values. They’re like the action words in programming that tell the computer what calculations or comparisons to make.

💡 Real-World Analogy

Think of operators like buttons on a calculator:

  • + button adds numbers together
  • – button subtracts
  • × button multiplies
  • Each button performs a specific operation

🎯 Types of Operators

  • Arithmetic Operators: +, -, *, /, % (for math calculations)
  • Comparison Operators: ==, !=, <, >, <=, >= (for comparing values)
  • Logical Operators: and, or, not (for combining conditions)
  • Assignment Operators: =, +=, -=, *=, /= (for assigning values)

📝 Arithmetic Operators

/code

a = 10
b = 3

print(“Addition:”, a + b) # 13
print(“Subtraction:”, a – b) # 7
print(“Multiplication:”, a * b) # 30
print(“Division:”, a / b) # 3.333…
print(“Modulus:”, a % b) # 1

Output:
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3.3333333333333335
Modulus: 1

⚡ Comparison Operators

Comparison operators compare two values and return True or False:

  • == (equal to)
  • != (not equal to)
  • < (less than)
  • > (greater than)
  • <= (less than or equal to)
  • >= (greater than or equal to)

💭 Remember

  • Operators are essential for performing calculations
  • Choose the right operator for your task
  • Comparison operators return True or False
  • Practice using different operators in your code

🔥 Practice Challenge

  1. Calculate: (15 + 5) * 2
  2. Compare: Is 10 greater than 5?
  3. Use modulus to check if 17 is odd or even