Python operators

Operators in Python with example, Python programing tutorial

In the last python article, we learnt the basic fundamentals of python. Now in this article, we will be covering all about Python operators and their use. The articles are curated by some of the top python instructors in Delhi

The Python operators are used to perform mathematical and logical operations.

Python operators are unique symbols or names that can be used in programmers. Each of them has a name and an operator symbol, such as +, -, and *. The presence of a number in front of an operator symbol denotes that the value is a multiple of an integer. For Python operators, there are numerous distinct rules.

Operators in Python

Strings or numbers can be used as operands when using the following operators.

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

Now you must be wondering, what is an operand?

So, the value that we use in an operation is referred as operands. These can be variables or any data type that we have in python. Here we see,

               OPERAND 1  ->  OPERATOR  ->   OPERAND 2  ->   RESULT

Now I add an operator between them and will going to get result according to the type of operator that we are using over here. Example 5+6=11

Arithmetic Operator

Here we are going to work with numerical values and so this includes operation such as

OPERATORNAMEEXAMPLE
+Addition(X + Y)
Subtraction(X – Y)
*Multiplication(X * Y)
/Division(X / Y)
%Modulus(X % Y)
**Exponentiation(X ** Y)
//Floor Division(X // Y)
arithmetic operator in python
arithmetic operator in python

Division (X / Y): This will give us the quotient.

Modulus(X % Y): This operator is going to take the first number divided by the second number and it’s going to spit out the remainder. For instance: 10 % 3 is 1

  • Floor division(X // Y): If you are willing to get only the integer part of the result regardless of the fractional part just what we can do is the use of double slash. -For instance: 10 // 20 is 0

Assignment operator

This operator is used to assign values to the variables or any other object in the python.For instance: X = 5 is a simple operator which assigns the value 5 on the right to the variable X on the left. 

Or If we say, initially x = 2

What if I say we can add 2 numbers like x = x + 2. By this we will get result as 4.

Here what all we are doing is adding a number to itself.  Instead of applying this long equation x = x + 2, we can do this in a short way by applying assignment operator.

X += 2 which means same as x = x + 2

assignment operator in python
assignment operator in python

This operator firstly adds and then assigns, subtract and then assign, multiply and then assign, furthermore same will going to happens to entire assignment operator we are running below.

NameOperatorExamplesame as  
Assign  =x = 5x = 5
Add and assign  +=x += 6x = x + 6
Subtract and assign-=x -= 6x = x – 6
Multiply and assign*=x *= 6x = x * 6
Divide and assign/=x /= 6x = x / 6
Modulus and assign%=x %= 6x = x % 6
Floor-divide and Assign//=x //= 6x = x // 6
Exponent and Assign**=x **= 6x = x ** 6
Bitwise and assign&=x &= 6x = x & 6
Bitwise OR and assign|=x |= 6x = x | 6
Bitwise XOR and assign^=x ^= 6x = x ^ 6
Bitwise right shift and assign>>=x >>= 6x = x >> 6
Bitwise left shift and assign<<=x <<= 6x = x << 6

Comparison operator

These operators are nothing but allow us to compare two or more things which can be numbers or objects. To use a comparison operator we usually use a conditional statement. Let’s list all the comparison operators below.

OperatorNameExample
==Equalx == y
!=Not equalx != y
Greater thanx > y
Lesser thanx < y
>=Greater than or equal tox >= y
<=Lesser than or equal tox <= y
comparison operator in python
comparison operator in python

Equal (==): = means assigning but == does not means the same. It means comparing two things. Its nature is questioning like “Is this the same as that?” For instance: Print (10 == 10), this will evaluate like either true or false and this statement means 10 is equals to 10.

  • Not equal (!=): Here (!) stands for “NO” and this operator says is it true that 10 is not equal to 9. This will be interpreted as 10 != 9 and so happens with rest of comparison operators.

Logical operators

Python has some logical operators and branches use these logical operators for compound decisions. There are three logical operators. There is “And” which is true when both operands are true, “Or” is true when either operand is true and “Not” is true when the operand is false.

OperatorExample
Andx < 5 and x < 10
Orx < 5 or x < 4
Notnot (x < 5 and x < 10)
Logical operators in Python

Identity operator

Identity operators are used to compare the objects, not if they are equal but if they are actually the same objects, with the same memory location. For instance:

If two operands have same identity it will turn true. Let’s say 99 is 100 and the result will turn out to be false. 

100 is 100, try and have a look on result. 

OperatorExample
Isx is y
Is notx is not y
Identity operators in Python
Identity operators in Python

Membership operator

You have seen multiple operators already and the one we are going to look at now is the membership operator. The membership operators are useful to test for membership. Here mentioned 2 different types of membership operator.

OperatorExample
Inx in y
Not inx not in y

Python Bitwise operator

These operators works on bits or we can say on binary numbers. Here 1 is considered as true value and 0 be the false value.

OperatorName
&And
|Or
^XOR
~NOT
<< Zero fill left shift
>> Signed right shift
  • When you perform the “And” operation on 2 bits then the result is true only when both bits are true or we can say when both bits are 1.
  • If you perform the “Or” function then the result will be true even if any one of the 2 bits is true or 1.

Interestingly, you will find that almost all programing languages have either same or similar number of operators with minimum or no syntactical difference. It means we have already learnt about operators in all programing languages. Cheers to that!!!

So, if in case you want to deep dive in python and learn it completely, you can join the top python programing Institute in West Delhi and learn in offline mode at Dwarka or Nangloi. If you wish to learn it online, you can check the top python online course certification and get skilled at the comfort of your home