Python Operators

Python is a general-purpose programming language. It is very easy to work with. It has simple syntax rules which make the code readable by anyone. It is a booming programming language in the market right now. So many people are tending towards learning the language. To get started with learning this language, you have to get strong in basics. Operators are the base foundation for any programming language. Operators are used for arithmetic or logical computation on operands. They are referred to as special symbols that carry out the computation of data. So, when you are learning python, you must first learn about the operators that it supports. If you are a beginner in python coding, this blog will best suit you. In this post, you will be able to learn about all the different operators available in Python. We will also show the syntaxes and examples of the operators along with an explanation. Let us get started.

Arithmetic operators

To perform calculations like addition, subtraction, division, etc. between two operands, we can use the arithmetic operators. The below table gives an explanation of all the arithmetic operators.

Operator Syntax Description
Addition (+) x+y To add two operands.
Subtraction (-) x-y To subtract the right operand from the left operand.
Multiplication (*) x*y To multiply one operand with the other.
Division (/) x/y To divide the left operand by the right operand.
Remainer (%) x%y To get the remainder of the division of left operand by the right operand.
Floor Division (//) x//y To divide the left operand by the right operand and get the floor value of the quotient.
Exponent (**) x**y To get the exponent of the left operand raised to the power of the right operand.

   Become a python Certified professional  by learning this HKR Python Training !

Here is a small program that performs all the arithmetic operations on two variables. Let’s take two operands as an example, and see what the results are going to be when we use different operators on them.

a = 10

b = 4

print(‘a+b = ’, a+b)

print(‘a-b = ’, a-b)

print(‘a*b = ’, a*b)

print(‘a/b = ’, a/b)

print(‘a%b = ’, a%b)

print(‘a//b = ’, a//b)

print(‘a**b = ’, a**b)

Here is the output for the above program,

a+b = 14

a-b = 6

a*b = 40

a/b = 2.5

a%b = 0.4

a//b = 2

a**b = 10000

Comparison operators

To compare the values of two operands, we can use the comparison operators. The result of these operators is either true or false i.e. a boolean value. Let’s take look at all the available comparison operators.

Operator Syntax Description
Greater than (>) x>y To check if the left operand is greater than the value of the right operand.
Less than (<) x<y To check if the left operand is less than the value of the right operand.
Equal to (==) x==y To check if both the operators are equal or not.
Not equal to (!=) x!=y To check if the left operand is not equal to the right operand.
Greater than or equal to (>=) x>=y To check if the left operand is greater than or equal to the right operand.
Less than or equal to (<=) x<=y To check if the left operand is less than or equal to the right operand

Here is a program that contains all the comparison operators.

a = 10;

b = 4;

print('a>b is ', a>b)

print('a

print('a==b is ', a==b)

print('a!=b is ', a!=b)

print('a>=b is ', a>=b)

print('a<=b is ', a<=b)

The output for the above program will be as follows.

a> b is True

a

a==b is False

a!=b is True

a>=b is True

a<=b is False

Python Training Certification

  • Master Your Craft
  • Lifetime LMS & Faculty Access
  • 24/7 online expert support
  • Real-world & Project Based Learning

Logical operators

To check if the operands satisfy a specific condition, we can use logical operators. It is mainly used for decision making. The following table explains the logical operators.

Operator Syntax Description
And X and y The result will be true if both the operands are true, otherwise, it is false.
Or X or y The result will be true if both or either of the operands is true. If both the operands are false, then the result is false.
Not not x The result will be true when the operand is false. It basically inverts the boolean value of the operand.

Here is the truth table for the ‘and’ operator.

A B A and B
True True True
True False False
False True False
False False False

Here is the truth table for the ‘or’ operator.

A B A or B
True True True
True False True
False True True
False False False

Here is the truth table for the ‘not’ operator.

A not A
True False
False True

If you want to Explore more about Python? then read our updated article - Python Tutorial.

Bitwise operators

We can use bitwise operators to compare the numbers in binary format. They operate bit by bit on the binary format of the numbers. 

Operator Syntax Description
Bitwise AND (&) x&y Performs bit by bit ‘and’ operation on the binary format of the values of the operands. If both the bits are 1, then the result will be 1. Otherwise, it will be 0.
Bitwise OR (|) x|y Performs bit by bit ‘or’ operation on the binary format of the values of the operands. If both the bits or either of the bits is 1, then the result will be 1. Otherwise, it will be 0.
Bitwise NOT (~) ~x It inverts all the bits. If the bit is 1, the result will be 0. If the bit is 0, the result will be 1.
Bitwise XOR (^) x^y If both of the bits are the same, the result will be 0. Otherwise, the result will be 1. It is also called as bitwise exclusive OR.
Bitwise right shift (>>) x>>1 To perform a bitwise right shift on an operand, i.e., shift the bits to the right side by the value of the right operand.
Bitwise left shift (<<) x<<1 To perform a bitwise left shift on an operand, i.e., shift the bits to the left side by the value of the right operand.

Let’s consider a = 10 (0000 1010) and b = 5 (0000 1011) in binary formats as example. 

a = 10

b = 5

Print('a&b = ', a&b)

Print('a|b = ', a|b

Print('~a = ', ~a)

Print('a^b = ', a^b)

Print('a>>1 = ', a>>1)

Print('a<<1 a="" 1="" p="">
The output for the above program will be as follows. Let me add the binary format for the output values for a better understanding.

a&b = 10 (0000 1010)

a|b = 13 (0000 1011)

~a = 5 (0000 0101)

a^b = 1 (0000 0001)

a >> 1 = 5 (0000 0101)

a << 1 = 22 (0001 0110)

Assignment operators

We can use assignment operators to assign values to variables. We can use the assignment operator along with an arithmetic operator. In this case, the arithmetic operation will be performed first, and then the value will be assigned to the variable.

Operator Syntax Equivalent to Description
Assign (=) x=10 - To assign a value to the left operand.
Add and assign (+=) x += 10 x = x+10 To add the value of the left operand by the value given on the right, and then assign the new value back to the left operand.
Subtract and assign (-=) x -= 10 x = x-10 To subtract the value of the left operand by the value given on the right, and then assign the new value back to the left operand.
Multiply and assign (*=) x *= 10 x = x*10 To multiply the value of the left operand by the value given on the right, and then assign the new value back to the left operand.
Divide and assign (/=) x /= 10 x = x/10 To divide the value of the left operand by the value given on the right, and then assign the new value back to the left operand.
Modulus and assign (%=) x %= 10 x = x%10 To get the remainder of the left operand by dividing it with the value given on the right, and then assign the new value back to the left operand.

Subscribe to our youtube channel to get new updates..!

The below program shows the usage of the assignment operators.

a = 5

print("a = 5 is ", a)

a += 20

print("a += 20 is ", a)

a -= 5

print("a -= 5 is ", a)

a *= 6

print("a *= 6 is ", a)

a /= 2

print("a /= 2 is ", a)

a %= 4

print("a %= 4 is ", a)

The output for the above program will be as follows.

a = 5 is 5

a += 20 is 25

a -= 5 is 20

a *= 6 is 120

a /= 2 is 60.0

a %= 4 is 0.0

Top 50 frequently asked Python interview Question and answers !

Identity operators

To check if two operands share the same memory location or not, we can use the identity operators. It doesn't mean that the two variables are the same, but they are of the same object. These come under the special operators’ category in python. 

Operator Syntax Description
is x is y The result will be true if both the variables are of the same object.
is not X is not y The result will be true if both the variables are not of the same object.

Here is a program that explains the identity operators.

a = ["hello", "hi"]

b = ["hello", "hi"]

c = a

print("a is c = ", a is c)

print("a is b = ", a is b)

a = [1, 2]

b = [1, 2]

c = b

print("a is not c = ", a is not c)

print("b is not c = ", b is not c)

The following is the output for the above program.

a is c = True

a is b = False

a is not c = True

b is not c = False

Membership operators

To check if a value or variable is found in a sequence or not, we can use membership operators. These come under the special operators’ category in python.

Operator Syntax Description
in x in y Returns true if the left operand value is in the right operand. Otherwise, it returns false.
not in X not in y Returns true if the left operand value is not in the right operand. Otherwise, it returns false.

Here is a sample program that explains the usage of the membership operators.

a = ["hello", "welcome"]

b = "hello"

print("b in a = ", b in a)

print("welcome in a =", "welcome" in a)

a = [1, 2]

b = 3

print("b not in a = ", b not in a)

print("2 not in a = ", 2 not in a)

The output of the above program is as follows.

b in a = True

welcome in a = True

b not in a = True

2 not in a = False

Order of precedence of python operators

An expression might have more than one operator. An order of precedence guides the order in which the operations should get executed. In an expression, the operator with the highest precedence will get executed first, and then the lowest precedence operator will get executed. It is very important to learn the order of precedence of python operators. The following table lists the order of precedence of operators from highest to lowest.

Operator Description
** Exponent
~ + - Bitwise NOT, unary plus, and minus
* / % // Multiplication, division, modulus and floor division
+ - Addition and subtraction
>> << Bitwise right shift and bitwise left shift
& Bitwise AND
^ | Bitwise XOR and regular OR
<= < > >= Comparison operators
<> == != Equality operators
= %= /= //= -= += *= **= Assignment operators
is is not Identity operators
in not in Membership operators
not or and Logical operators

Let’s take a look at an example with a simple program.

a = 8

b = 7

c = 4

d = 11

e = a + b * d / c

f = d - a * b + c / a

print(“The value of a + b * d / c is”, e)

print(“The value of d - a * b + c / a is”, f)

The output for the above program is,

The value of a + b * d / c is 27.25

The value of d - a * b + c / a is -44.5

Parenthesis plays an important role in changing the precedence. If you want a certain operation to be performed first in your expression, wrap it in parenthesis. Let’s take the same program mentioned above and see how the output changes when parenthesis is used in the expressions.

a = 8

b = 7

c = 4

d = 11

e = (a + b) * d / c

f = (d - a) * b + (c / a)

print('The value of (a + b) * d / c is', e)

print('The value of (d - a) * b + (c / a) is', f)

The output for the above program will be as follows.

The value of a + b * d / c is 41.25

The value of (d - a) * b + (c / a) is 21.5

Python Training Certification

Weekday / Weekend Batches

Conclusion

Python operators are the core fundamental concept. It is popular for a variety of operators that it provides. That is the reason python is the most preferable language by most of the developers. In this blog, we have discussed in detail about each of the python operators. 

Now that you know about operators, you can start writing programs in python for your applications. Play around with the operators, use them in conditions. Create compound expressions with a combination of operators to master them. Remember the order of precedence and parenthesis to set priority for your calculations. Have fun with python coding!

Related Articles:

1. Python Partial Functions

2. Python Split Method

3. Python Frameworks

4. Python Generators

5. Python List Length

Categories

Request for more information

John
John
Cloud Technologies & Cyber Security
John is a Post Graduate in Computer Science from Andhra University .She is currently working as an IT developer at hkr trainings.com. And he has great experience includes both IT development and operational roles. Connect with him on LinkedIn and Twitter. Thank you

WhatsApp
To Top