DEV Community

V Sai Harsha
V Sai Harsha

Posted on

Master Python - Operators

Introduction

Python is a versatile and beginner-friendly programming language known for its simplicity and readability. As a Python novice, one fundamental concept you'll encounter early on is the use of operators. Operators in Python allow you to perform various operations, from basic arithmetic to complex comparisons, and they play a crucial role in writing efficient and expressive code. In this article, we'll explore the most commonly used operators in Python, breaking them down into different categories for easy understanding.

Arithmetic Operators:

  1. + (Addition): Used for adding two numbers.
  2. - (Subtraction): Used for subtracting the right operand from the left.
  3. * (Multiplication): Used for multiplying two numbers.
  4. / (Division): Used for dividing the left operand by the right.
  5. % (Modulus): Returns the remainder when the left operand is divided by the right.
  6. ** (Exponentiation): Raises the left operand to the power of the right.

Example:

a = 10
b = 5
sum_result = a + b
print(sum_result)  # Output: 15
Enter fullscreen mode Exit fullscreen mode

Comparison Operators:

  1. == (Equal to): Checks if the values of two operands are equal.
  2. != (Not equal to): Checks if the values of two operands are not equal.
  3. < (Less than): Checks if the left operand is less than the right.
  4. > (Greater than): Checks if the left operand is greater than the right.
  5. <= (Less than or equal to): Checks if the left operand is less than or equal to the right.
  6. >= (Greater than or equal to): Checks if the left operand is greater than or equal to the right.

Example:

x = 10
y = 5
is_equal = x == y
print(is_equal)  # Output: False
Enter fullscreen mode Exit fullscreen mode

Logical Operators:

  1. and (Logical AND): Returns True if both operands are True.
  2. or (Logical OR): Returns True if at least one operand is True.
  3. not (Logical NOT): Returns the opposite of the operand's value.

Example:

is_sunny = True
is_warm = False
is_good_weather = is_sunny and not is_warm
print(is_good_weather)  # Output: True
Enter fullscreen mode Exit fullscreen mode

Assignment Operators:

  1. = (Assignment): Assigns the value of the right operand to the left operand.
  2. += (Add and assign): Adds the right operand to the left and assigns the result to the left.
  3. -= (Subtract and assign): Subtracts the right operand from the left and assigns the result to the left.

Example:

counter = 0
counter += 1
print(counter)  # Output: 1
Enter fullscreen mode Exit fullscreen mode

Membership Operators:

  1. in (Membership): Returns True if a value is found in the sequence.
  2. not in (Not in membership): Returns True if a value is not found in the sequence.

Example:

fruits = ["apple", "banana", "cherry"]
is_mango_present = "mango" in fruits
print(is_mango_present)  # Output: False
Enter fullscreen mode Exit fullscreen mode

Identity Operators:

  1. is (Identity): Returns True if both operands are the same object.
  2. is not (Not identity): Returns True if both operands are not the same object.

Example:

x = [1, 2, 3]
y = [1, 2, 3]
are_identical = x is y
print(are_identical)  # Output: False
Enter fullscreen mode Exit fullscreen mode

Understanding these fundamental operators is essential for writing Python code effectively. They enable you to perform calculations, make decisions, and manipulate data, all of which are fundamental tasks in programming. As you progress in your Python journey, you'll find yourself using these operators regularly, making them a crucial part of your programming toolkit.

Top comments (0)