Skip to Content
πŸŽ‰ Welcome to my notes πŸŽ‰
Python4. Operators

πŸ”’ Operators

Operators are symbols used to perform operations on variables and values.

βž• 1. Arithmetic Operators:

Used to perform basic mathematical operations

  • + addition
  • - subtraction
  • * multiplication
  • / division
  • % modulus (remainder)
  • ** exponentiation (power)
  • // floor division
arithmatic_operators.py
a = 10 b = 5 print("Addition:", a + b) # Output: 15 print("Subtraction:", a - b) # Output: 5 print("Multiplication:", a * b) # Output: 50 print("Division:", a / b) # Output: 2.0 print("Modulus:", a % b) # Output: 0 print("Exponentiation:", a ** b) # Output: 100000 print("Floor Division:", a // b) # Output: 2

🟰 2. Comparison Operators:

Used to compare values

  • == equal to
  • != not equal to
  • > greater than
  • < less than
  • >= greater than or equal to
  • <= less than or equal to
comparison_operators.py
a = 10 b = 5 print("Equal to:", a == b) # Output: False print("Not equal to:", a != b) # Output: True print("Greater than:", a > b) # Output: True print("Less than:", a < b) # Output: False print("Greater than or equal to:", a >= b) # Output: True print("Less than or equal to:", a <= b) # Output: False

βž— 3. Logical Operators:

Combine conditional statements

  • and returns True if both are true
  • or returns True if one is true
  • not reverses the result
logical_operators.py
a = True b = False print("AND:", a and b) # Output: False print("OR:", a or b) # Output: True print("NOT:", not a) # Output: False
Last updated on