Python Diaries ….. Day 3

Nishitha Kalathil
5 min readSep 16, 2023

Welcome to Day 3 of Python Diaries! The Operators…

In today’s class, we’ll be delving deep into the world of Python operators. Operators are fundamental tools that allow us to perform operations on variables and values. They range from arithmetic operators for mathematical calculations to logical operators for decision-making in our programs.

Understanding and mastering operators is crucial for writing efficient and effective Python code. So, let’s roll up our sleeves and dive into this essential aspect of Python programming. Get ready to explore the diverse set of operators that Python offers, and learn how to leverage them to accomplish a wide range of tasks. Let’s get started!

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations on numbers. Here are the commonly used arithmetic operators:

  • Addition (+): Adds two operands.
  • Subtraction (-): Subtracts the right operand from the left operand.
  • Multiplication (*): Multiplies two operands.
  • Division (/): Divides the left operand by the right operand (result is always a float).
  • Modulus (%): Returns the remainder of the division.
  • Exponentiation ()**: Raises the left operand to the power of the right operand.
  • Floor Division (//): Performs division and rounds down to the nearest whole number.
x = 10
y = 5

addition = x + y # Output: 15
subtraction = x - y # Output: 5
multiplication = x * y # Output: 50
division = x / y # Output: 2.0
modulus = x % y # Output: 0
exponentiation = x ** y # Output: 100000
floor_division = x // y # Output: 2

Comparison Operators

Comparison operators are used to compare two values. They return True if the comparison is true, and False otherwise. Here are the commonly used comparison operators:

  • Equal to (==): Checks if two operands are equal.
  • Not equal to (!=): Checks if two operands are not equal.
  • Greater than (>): Checks if the left operand is greater than the right operand.
  • Less than (<): Checks if the left operand is less than the right operand.
  • Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right operand.
  • Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand.
x = 10
y = 5

is_equal = x == y # Output: False
not_equal = x != y # Output: True
greater_than = x > y # Output: True
less_than = x < y # Output: False
greater_or_equal = x >= y # Output: True
less_or_equal = x <= y # Output: False

Logical Operators

Logical operators are used to combine conditional statements. They return True or False based on the evaluation of the conditions. Here are the commonly used logical operators:

  • And (and): Returns True if both conditions are true.
  • Or (or): Returns True if at least one condition is true.
  • Not (not): Returns the opposite of the condition.
x = True
y = False

logical_and = x and y # Output: False
logical_or = x or y # Output: True
logical_not_x = not x # Output: False
logical_not_y = not y # Output: True

Assignment Operators

Assignment operators are used to assign values to variables. They combine an arithmetic operation with an assignment.

  • Assign (=): Assigns the value of the right operand to the left operand.
  • Add and Assign (+=): Adds the right operand to the left operand and assigns the result to the left operand.
  • Subtract and Assign (-=): Subtracts the right operand from the left operand and assigns the result to the left operand.
  • Multiply and Assign (*=): Multiplies the left operand by the right operand and assigns the result to the left operand.
  • Divide and Assign (/=): Divides the left operand by the right operand and assigns the result to the left operand.
  • Modulus and Assign (%=): Calculates the modulus of the left operand with the right operand and assigns the result to the left operand.
  • Exponentiate and Assign (=)**: Raises the left operand to the power of the right operand and assigns the result to the left operand.
  • Floor Divide and Assign (//=): Performs floor division and assigns the result to the left operand.
x = 10  # x is assigned the value 10

# Add and Assign
x += 5 # Equivalent to x = x + 5, x is now 15

# Subtract and Assign
x -= 3 # Equivalent to x = x - 3, x is now 12

# Multiply and Assign
x *= 2 # Equivalent to x = x * 2, x is now 24

# Divide and Assign
x /= 4 # Equivalent to x = x / 4, x is now 6.0

# Modulus and Assign
x %= 2 # Equivalent to x = x % 2, x is now 0

# Exponentiate and Assign
x **= 3 # Equivalent to x = x ** 3, x is now 0

# Floor Divide and Assign
x //= 5 # Equivalent to x = x // 5, x is now 0

Identity Operators

Identity operators are used to compare the memory location of two objects.

  • Is (is): Returns True if both variables are the same object.
  • Is Not (is not): Returns True if both variables are not the same object.
x = [1, 2, 3]
y = [1, 2, 3]
z = x

is_x = x is y # Output: False
is_not_x = x is not y # Output: True
is_z = x is z # Output: True

Membership Operators

Membership operators are used to test if a sequence is presented in an object.

  • In (in): Returns True if a value is found in the specified sequence.
  • Not In (not in): Returns True if a value is not found in the specified sequence.
x = [1, 2, 3]

in_list = 2 in x # Output: True
not_in_list = 4 not in x # Output: True

Understanding and utilizing these operators will significantly enhance your ability to manipulate data and control the flow of your Python programs. Practice with them, and you’ll be well on your way to becoming a proficient Python programmer. Happy coding!

You can access the other topics in this tutorial series right here:

--

--