Python Operators and Expressions

Python Operator and Expressions are the building blocks of Python programming language. They allow you to perform various operations on data, manipulate values, and make decisions based on conditions. In this post, we will learn about the all different types of operators along with the examples and we will leverage the power of expressions in Python. Let’s learn about Python Operators and Expressions.

Arithmetic Operators

Arithmetic Operators are used to perform arithmetical operations on numeric values. These are the same operators which we have used in any other programming language for perming numeric operations.

  • + (Addition): Adds two values together.
  • - (Subtraction): Subtracts the second value from the first.
  • * (Multiplication): Multiplies two values.
  • / (Division): Divides the first value by the second.
  • % (Modulus): Returns the remainder of the division.
  • ** (Exponentiation): Raises the first value to the power of the second.
  • // (Floor Division): Divides and rounds down to the nearest whole number.

Let’s see some examples of Arithmetic operators

a , b = 20, 3.     # assign 20 to a and 3 to b
a+b.                     # perform addition of a and b
a-b.                      # perform subtraction of a and b
a*b.                      # perform multiplication of a and b
a/b.                      # perform division of a and b
a%b.                    # perform module of a and b and return remainder
a**b.                    # perform exponent of a and b and return Cube
a//b.                    # perform division of a and b and return integer value

Comparison Operators

Comparison operators are used to compare two values and return a Boolean result ( True or False ). The Comparison operators help us to perform conditional operations. Mostly we use comparison operators with if, while or is in operators.

  • == (Equal to): Checks if two values are equal.
  • != (Not equal to): Checks if two values are not equal.
  • < (Less than): Checks if the first value is less than the second.
  • > (Greater than): Checks if the first value is greater than the second.
  • <= (Less than or equal to): Checks if the first value is less than or equal to the second.
  • >= (Greater than or equal to): Checks if the first value is greater than or equal to the second.

Let’s see some examples of Comparison operators

a , b = 10, 20     # assign 10 to a and 20 to b
a == b                   # Check both values are equal. Here returns False
a != b                    # Check both values are not equal. Here returns True
a < b                      # Check first value is less than second. Here returns True
a > b                      # Check first value is greater than second. Here returns False
a <= b                   # Check first value is less than or equal to second. Here returns True
a >= b                   # Check first value is greater than or equal to  second. Here returns False

Logical Operators

Logically combining multiple statements together involves the use of logical operators. There are three logical operators present in Python.

  • and : Returns True if both values are True .
  • or : Returns True if at least one value is True .
  • not : Returns the opposite of the Boolean value.

Let’s see some examples of Logical operators

a , b = 10, 20               # assign 10 to a and 20 to b
a != b and a<b           # Check both conditions are True. Here returns True
a != b and a>b           # Check both conditions are True. Here returns False
a != b or a<b              # Check any condition is True. Here returns True
a == b or a>b             # Check any condition is True. Here returns False
not (a == b)                # Return negative value of result. Here returns True
not (a != b)                 # Return negative value of result. Here returns False 

Assignment Operators

When using shorthand notation, assignment operators assign values either with or without performing arithmetic operations.

  • ** =** : Assigns the value on the right to the variable on the left.
  • += (Add and assign): Adds the right value to the variable’s current value.
  • -= (Subtract and assign): Subtracts the right value from the variable’s current value.
  • *= (Multiply and assign): Multiplies the variable’s current value by the right value.
  • /= (Divide and assign): Divides the variable’s current value by the right value.

Let’s see some examples of Assignment operators

a = 10                         # Assign value 10 to variable a
b, c = 20, 30            # Assign value 20 to variable b and value 30 to variable c
c += b                         # Perform c = c + b
c -= b                          # Perform c = c - b
c *= b                         # Perform c = c * b
c/= b                          # Perform c = c / b

Bitwise Operators

Bitwise operators perform operations at the bit level.

  • & (Bitwise AND): Performs bitwise AND operation.
  • | (Bitwise OR): Performs bitwise OR operation.
  • ^ (Bitwise XOR): Performs bitwise exclusive OR operation.
  • ~ (Bitwise NOT): Inverts the bits.
  • << (Left shift): Shifts bits to the left.
  • >> (Right shift): Shifts bits to the right.

Conditional (Ternary) Operator

Conditional or Ternary operator is a different way to write If and else in different way or in a single line.

value_if_true if condition else value_if_false

Returns value_if_true if the condition is True , otherwise returns value_if_false .

a, b = 20, 10                                       # Assign value 20 to variable a and value 10 to variable b
c = 'True' if a > b else 'False'.     # Return True if condition satisfy else return False

Membership Operators

The membership operators enable checking for membership within any sequence, such as List, Tuple, array, and more.

  • in (Membership test)
  • not in (Negated membership test)
a = [1, 2, 3, 4, 5]       # Initialise list with 5 values
5 in a                             # Return True if 5 is present in a otherwise return False
6 not in a                    # Return True if 6 is not present in a otherwise return False

Identity Operators

In Python, you use identity operators to compare the memory locations of two objects in order to determine if they represent the same object or not. These operators are useful for checking object identity, particularly when dealing with mutable objects like lists and dictionaries

  • is : This operator returns True if two variables reference the same object in memory and False otherwise.
  • is not : This operator returns True if two variables do not reference the same object in memory and False if they do.
x = [1, 2, 3]
y = x
z = [1, 2, 3]
print(x is y)           # True, x and y reference the same object
print(x is z)           # False, x and z are different objects
print(x is not z)  # True, x and z are not the same object

Expressions

To produce a result, expressions evaluate combinations of values, variables, and operators.

x = 5
y = 10
result = x + y * 2

In this expression, x + y * 2 , the addition and multiplication operators are used to calculate the result.

I hope all the above sections clear the concepts of Python Operator and Expressions. There are some more important concepts related to operations in Python which we can cover in the next session. To learn about SQL, follow SQL Tutorials. Happy offline learning… Let’s meet in the next session. Check the quiz before next session Click here.

Leave a Comment