Python Operators 🎯

beginner
16 min

Python Operators 🎯

Welcome to our comprehensive guide on Python Operators! In this tutorial, we'll dive deep into understanding various types of operators available in Python, their usage, and how they can be leveraged to write efficient and effective code. Let's get started!

Arithmetic Operators (📝)

Arithmetic operators are used to perform mathematical operations. Here's a list of basic arithmetic operators in Python:

  1. +: Addition
  2. -: Subtraction
  3. *: Multiplication
  4. /: Division
  5. //: Floor Division
  6. %: Modulus (remainder)
  7. **: Exponentiation

Example:

python
# Arithmetic operations a = 10 b = 5 print("Addition: ", a + b) print("Subtraction: ", a - b) print("Multiplication: ", a * b) print("Division: ", a / b) print("Floor Division: ", a // b) print("Modulus: ", a % b) print("Exponentiation: ", a ** b)

Assignment Operators (💡)

Assignment operators are used to assign values to variables. Here's a list of basic assignment operators in Python:

  1. =: Simple assignment
  2. +=: Add and assign
  3. -=: Subtract and assign
  4. *=: Multiply and assign
  5. /=: Divide and assign
  6. //=: Floor divide and assign
  7. %=: Modulus and assign
  8. **=: Exponentiate and assign

Example:

python
# Assignment operations a = 10 a += 5 # a = a + 5 print("Add and Assign: ", a) a -= 3 # a = a - 3 print("Subtract and Assign: ", a)

Comparison Operators (💡)

Comparison operators are used to compare the values of variables or expressions. Here's a list of basic comparison operators in Python:

  1. ==: Equal to
  2. !=: Not equal to
  3. >: Greater than
  4. <: Less than
  5. >=: Greater than or equal to
  6. <=: Less than or equal to

Example:

python
# Comparison operations a = 10 b = 5 print("Equal to: ", a == b) print("Not equal to: ", a != b) print("Greater than: ", a > b) print("Less than: ", a < b) print("Greater than or equal to: ", a >= b) print("Less than or equal to: ", a <= b)

Logical Operators (💡)

Logical operators are used to combine conditional statements. Here's a list of basic logical operators in Python:

  1. and: Both conditions must be true
  2. or: At least one condition must be true
  3. not: Negate the condition

Example:

python
# Logical operations a = 10 b = 5 print("a is greater than 5 and less than 20: ", (a > 5) and (a < 20)) print("a is not less than 10 or b is greater than 10: ", (a > 10) or (b > 10)) print("a is not greater than 5: ", not (a > 5))

Quiz

Quick Quiz
Question 1 of 1

Which operator is used to perform division in Python?

Happy learning, and let's code! 💻🚀