Python Operators

Hello Programmers, Python learners, and Python beginners!! Today we will see everything about Python Operators. In this blog, we will not only see theoretical things but will see examples of Python Operators with coding and output.

What is an Operator:

In programming, an operator is used to perform operations between two values or two variables. In other words, we can call the operators as constructs or special symbols that can be used to manipulate the values of the operands. Now you will say what is this operand? So Operands are the values that we use in the operation and this Operand can be any data type that we have in python or any variables. Let’s see it by an example:

       a + b = result

Here,  a is operand
       b is operand
       + is operator 
       = is operator
       result is operand
So, the above statement can be written as:   
Operator1 + Operator2 = result
       
The + operator will produce the result after performing the operation on a and b operands. Means it will add both the values. Now the = operator will assign the value generated by the '+' operator on the left hand side to the result operand at the right hand side. So, operators produce a result after performing the operation on the operands and produce the value based on which operator we are using.

What is Python Operators:

Python Operators are the Operators that we use in python and these operators are supported by Python Programming Language.

The different types of operators in Python are:

  • Arithmetic Operators
  • Assignment Operators
  • Comparison operators
  • Logical Operators
  • Membership Operators
  • Identity Operators
  • Bitwise Operators

Let’s look at each of the operators in detail:

Arithmetic Operators in Python:

Arithmetic operators are used to perform arithmetic operations between variables or two values or two operands.

Different arithmetic operators in python are:

  • + (addition operator): It adds the values on the left-hand side and right-hand side of it.
  • – (subtraction operator): It subtracts the right-hand side value from the left-hand side value.
  • * (multiplication operator): It multiplies both the values on its left and right-hand side.
  • / (division operator): It divides the left-hand-side value by the right-hand-side value.
  • % (modulus operator): It returns the remainder after dividing the left-hand side value by the right-hand-side value.
  • ** (exponentiation operator): It returns the result of raising the first operand to the power of the second operand.
  • // (floor division operator): It returns floor value for both integer and floating-point arguments after dividing left operand by right operand. 

We can implement these operators by code. Let’s look at the code below and understand how these Arithmetic operators are used in python.

Python Operators

Let’s move to the other python operators.

Assignment Operators in Python:

As it’s name suggests, assignment operators are used to assign values in an operand or any variable in python.

Different assignment operators in python are:

Assignment OperatorsFor Example:It will be same as:
=x = 25x = 25
+=x += 25x = x + 25
-=x -= 25x = x – 5
*=x *= 25x = x * 25
/=x /= 25x = x / 25
%=x %= 25x = x % 25
**=x **= 25x = x ** 25
//=x //= 25x = x // 25
|=x |= 25x = x | 25
^=x ^= 25x = x ^ 25
&=x &= 25x = x & 25
cozmocard.com

We can implement these operators by code. Let’s look at the code below and understand how these Assignment operators are used in python.

Python Operators
Assignment operators
cozmocard.com

Explanation of the code:

At first, we assign a value in variable x=25. Now x=25 and this was the = operator which we have used for assigning the value in the variable x. Now the next statement is x += 5 which can be written as x = x + 5. So the value of x was 25 earlier and now we are adding 5 to the value of x and the result would be 25 + 5 = 30. Similarly other assignment operators -=, *=, /=, etc will work. Let’s move to the other python operators.

Comparison Operators in Python:

Comparison Operators are used to compare two operands or two values or two variables which are used in python. It is different from the assignment operators because assignment operators are used to assign the value but comparison operators are used to compare values and returns True or False after comparison. Comparison operators in python are also called relational operators in python. So what is a relational operators? We can say that relational operators in python are also called as comparision operators which is used to compare values.

Different comparison operators used in python are:

  • == (equal to operator): It is used to check whether both operands are equal to each other.
  • != (not equal to operator): It is used to check whether both operands are not equal to each other.
  • > (greater than operator): It is used to check whether the first operator is greater than the second one.
  • < (less than operator): It is used to check whether the first operator is less than the second one.
  • >= (greater than or equal to operator): It is used to check whether the first operator is greater than or equal to the second operator.
  • <= (less than or equal operator): It is used to check whether the first operator is less than or equal to the second operator.

We can implement these operators by code. Let’s look at the code below and understand how these Comparison operators are used in python.

comparison operators
cozmocard.com

Explanation of the code:

We have assigned x=25 and y=5. Now by x==y code, we are checking whether x is equal to y or not. In this case, both are not equal so the output is False. But when we write x != y and run it then the output comes as True because x is not equal to y. So similary all the comparison operators will compare the values according to it and will give output as True or False. Let’s move to the other python operators.

Logical Operators in Python:

Logical Operators are the operators which is used to combine the conditional statements.

Different types of Logical Operators in Python are:

  • and operator: It returns True if both the statements are True.
  • or operator: It returns True if either one of the statements is True or both the statements are True.
  • not operator: As its name suggests, it reverses the result, so it returns True if the result is False.

Let’s look at the implementation of logical operators by code:

Logical Operators
cozmocard.com

Explanation of the code:

We assign x=25. Now x>5 and x>10 is returning True because both the statements are true. But x>5 and x<11 returns False because both the statements are not True because x<11 is False as the value of x is 25. When we write code for or operator, x>5 or x<19 it’s returning True because x>5 is True and if anyone statement is True then the or operator will return true. Similarly, x>5 or x>11 returns True as both the statements are True. Finally, not (x>5 or x>11) is returning False because (x>5 or x>11) is True as we saw right now but the “not” operator reverses the result. Let’s move to the other python operators.

Membership Operators in python:

Membership Operator basically checks the membership or presence of a sequence in an object and it will test if a sequence is present in the given object.

Different types of Membership operators in python are:

  • in operator: It checks if a sequence with the specified value is present in the object and returns True if it is present otherwise returns False.
  • not in operator: It checks if a sequence with the specified value is not present in the object and returns True if it is not present otherwise returns True.

We can implement these operators by code. Let’s look at the code below and understand how these Membership operators are used in python.

membership operators
cozmocard.com

Explanation of the code:

We have declared a list as list=[5,11,25] and then checking whether 5 in list by using ‘in’ operator and 5 is in the list so this statement gives output as True. Similarly, we are checking 1 in list but the output is False because 1 is not in the list. Now coming to not in operator, 1 not in list is True because 1 is not in the list. But 11 not in list is False because 11 is present in the list. Let’s move to the other python operators.

Identity Operators in Python:

So the Identity operators are used to compare the objects in python programming, not to check if they are equal, but to check if they are actually the same object, with the same memory location. In other words, we can say that it is used to check whether the objects are identical to each other.

Different types of identity operators in python are:

  • is operator: It returns True if both the variables are the same object else it will return False.
  • is not operator: It returns False if both the variables are the same object else it will return True. It is the opposite of is operator.

Let’s understand these operators by python code:

identity operators in python
cozmocard.com

Explanation of the code:

We have declared two list as list1 and list2. list1=[5,9,6] and list2=[5,6,9]. We are assigning x=list1 and now x is equal to list1. Now x is list1 code is generating output as True because x is identical to list1 because we have assigned the values of list1 in x. But when we look at is not operator, the code x is not list1 is producing an output as False because it reverse the result of x is list1. Now let’s have a look at the next python operators.

Bitwise Operators in python:

Bitwise operators are used to compare binary values or binary numbers in python programming language.

Different types of bitwise operators in python are:

  • & (AND Operator): It sets each bit to 1 if both bits are 1.
  • | (OR Operator): It sets each bit to 1 if one of two bits or both the bits is 1.
  • ^ (XOR Operator): It sets each bit to 1 if only one of two bits is 1.
  • ~ (NOT Operator): It inverts all the bits.
  • << (Zero fill left shift Operator): It shifts left by pushing zeros in from the right and let the leftmost bits fall off.
  • >> (Signed right shift Operator): It shifts right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off.

Let’s look at some of the Bitwise Operators by python code. Here is the code:

bitwise operator in python
bitwise operator cozmocard.com

Explanation of code:

These bitwise operators operate on binary values or binary numbers. So when we use the code 10 & 4 it means that & operator will check the binary values of 10 and 4 and then operate over them. So the binary value of 10 is 1010 and of 4 is 0100. So this & operator will perform its operation over the binary values which will look like 1010 & 0100 and gives output as 0000 because no any same bit in both the binary values is same. So 0000 is the binary value of 0 and that’s why we can see we got 0 as output. Similarly, all the operators under bitwise operators will perform operations on binary values and give output based on its type.

Conclusion:

So that’s all about the python operators and I hope you really enjoyed reading this blog. Python Operators are very basic but really important topic to learn in the python programming language because without python operators, one can’t write a simple python code. Comment here if you have still any doubt left regarding python operators.

You can try the above codes by yourself here in this compiler:

This Post Has 2 Comments

Leave a Reply