Python Operators Tutorial for Beginners to Learn

In today’s class, we’ll learn about different Python operators such as Arithmetic, Comparison, Assignment, Logical, Bitwise, Identity and  Membership operators.

Also, we’ll know what are these operators for and how to use them in real-time.

The following table will assist you in learning Python operators.

Python Operators Tutorial for Beginners to Learn

Python Operators Tutorial

What are built-in Python operators?

Like many programming languages, Python reserves some special characters for acting as operators. Every operator has a pre-defined action like addition, multiplication to manipulate data and variables. The variables passed as input to an operator are known as operands. We also recommend you to read about keywords in Python.

Example-

>>> 7%4
3

In this case, ‘%’ is the modulus operator that calculates the remainder of the division. The numbers ‘7’ and ‘4’ passed as input are the operands whereas the numbers ‘3’ is the result of the action performed.

Arithmetic operators

With arithmetic operators, we can do various arithmetic operations like addition, subtraction, multiplication, division, modulus, exponent, etc. Python provides multiple ways for arithmetic calculations like eval function, declare variable & calculate, or call functions.

The table below outlines the built-in arithmetic operators in Python.

Operator
Purpose
Usage
+
Addition – Sum of two operands
a+b
Subtraction – Difference between the two operands
a-b
*
Multiplication – Product of the two operands
a*b
/
Float Division – Quotient of the two operands
a/b
//
Floor Division – Quotient of the two operands (Without fractional part)
a//b
%
Modulus – Integer remainder after division of ‘a’ by ‘b’
a%b
**
Exponent – Product of ‘a’ by itself ‘b’ times (a to the power of b)
a**b

Example-

a=7
b=4

print('Sum : ', a+b)
print('Subtraction : ', a-b)
print('Multiplication : ', a*b)
print('Division (float) : ', a/b)
print('Division (floor) : ', a//b)
print('Modulus : ', a%b)
print('Exponent : ', a**b)

Output-

Sum : 11
Subtraction : 3
Multiplication : 28
Division (float) : 1.75
Division (floor) : 1
Modulus : 3
Exponent : 2401

Back to top

Comparison operators

In Python programming, comparison operators allow us to determine whether two values are equal or if one is greater than the other and then make a decision based on the result.

The table below outlines the built-in comparison operators in Python.

Operator
Purpose
Usage
>
Greater than – if the left operand is greater than the right, then it returns true.
a>b
<
Less than – if the left operand is less than the right, then it returns true.
a<b
==
Equal to – if two operands are equal, then it returns true.
a==b
!=
Not equal to – if two operands aren’t equal, then it returns true.
a!=b
>=
Greater than or equal – if the left operand is greater than or equal to the right, then it returns true.
a>=b
<=
Less than or equal – if the left operand is less than or equal to the right, then it returns true.
a<=b

Example-

a=7
b=4

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

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

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

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

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

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

Output-

a > b is True
a < b is False
a == b is False
a != b is True
a >= b is True
a <= b is False

Back to top

Logical operators

The logical Python operators enable us to make decisions based on multiple conditions. The operands act as conditions that can result in a true or false value. The outcome of such an operation is either true or false (i.e., a Boolean value).

However, not all of these operators return a boolean result. The ‘and’ and ‘or’ operators do return one of their operands instead of a pure boolean value. Whereas the ‘not’ operator always gives a real boolean outcome.

Refer the below table and the example to know how these operators work in Python.

Operator
Purpose
Usage
and
if ‘a’ is false, then ‘a’, else ‘b’
a and b
or
if ‘a’ is false, then ‘b’, else ‘a’
a or b
not
if ‘a’ is false, then True, else False
not a

Example-

a=7
b=4

# Result: a and b is 4
print('a and b is',a and b)

# Result: a or b is 7
print('a or b is',a or b)

# Result: not a is False
print('not a is',not a)

Output-

a and b is 4
a or b is 7
not a is False

Back to top

Bitwise operators

Bitwise Python operators process the individual bits of integer values. They treat them as sequences of binary bits.

We can use bitwise operators to check whether a particular bit is set. For example, IoT applications read data from the sensors based on a specific bit is set or not. In such a situation, these operators can help.

Operator
Purpose
Usage
&
Bitwise AND – compares two operands on a bit level and returns 1 if both the corresponding bits are 1
a & b
|
Bitwise OR – compares two operands on a bit level and returns 1 if any of the corresponding bits is 1
a | b
~
Bitwise NOT – inverts all of the bits in a single operand
~a
^
Bitwise XOR – compares two operands on a bit level and returns 1 if any of the corresponding bits is 1, but not both
a ^ b
>>
Right shift – shifts the bits of ‘a’ to the right by ‘b’ no. of times
a >> b
<<
Left shift – shifts the bits of ‘a’ to the left by ‘b’ no. of times
a << b

Example-

Let’s consider the numbers 4 and 6 whose binary representations are ‘00000100’ and ‘00000110’. Now, we’ll perform the AND operation on these numbers.

a=4
b=6

#Bitwise AND: The result of 'a & b' is 4
print('a & b is',a & b)

Output-

a & b is 4

The above result is the outcome of following AND (‘&’) operation.

 0 0 0 0 0 1 0 0 &
 0 0 0 0 0 1 1 0
 ------------------
 0 0 0 0 0 1 0 0 (the binary representation of the number 4)

Back to top

Assignment operators

In Python, we can use assignment operators to set values into variables.

The instruction a = 4 uses a primitive assignment operator which assigns the value 4 to the left operand.

Below is the list of available compound operators in Python. For example, the statement a += 4 adds to the variable and then assigns the same. It will evaluate to a = a + 4.

Operator
Example
Similar to
=
a=4
a=4
+=
a+=4
a=a+4
-=
a-=4
a=a-4
*=
a*=4
a=a*4
/=
a/=4
a=a/4
%=
a%=4
a=a%4
**=
a**=4
a=a**4
&=
a&=4
a=a&4
|=
a|=4
a=a|4
^=
a^=4
a=a^4
>>=
a>>=4
a=a>>4
<<=
a<<=4
a=<<4

Back to top

Advanced Python operators

Python also bundles a few operators for special purposes. These are known as advanced Python operators like the identity operator or the membership operator.

Identity operators

These operators enable us to compare the memory locations of two Python objects/variables. They can let us find if the objects share same memory address. The variables holding equal values are not necessarily identical.

Alternatively, we can use these operators to determine whether a value is of a specific class or type.

Refer the below table to understand more about them.

Operator
Purpose
Usage
is
True – if both the operands refer to the same object, else False
a is b (True if id(a) and id(b) are the same)
is not
True – if the operands refer to different objects, else False
a is not b  (True if id(a) and id(b) are different)

Example-

# Using 'is' identity operator
a = 7
if (type(a) is int):
 print("true")
else:
 print("false")

# Using 'is not' identity operator
b = 7.5
if (type(b) is not int):
 print("true")
else:
 print("false")

Output-

true
true

Back to top

Membership operators

Membership operators enable us to test whether a value is a member of another Python objects such as strings, lists, or tuples.

In C, membership test requires iterating through a sequence and checking each value. So, it is clear that Python makes it very easy to establish membership as compared to C.

Also, please note that this operator can also test against a dictionary but only for the key, not the value.

Operator
Purpose
Usage
in
True – if the value exists in the sequence
7 in [3, 7, 9]
not in
True – if the value doesn’t found in the sequence
7 not in [3, 5, 9]

Example-

# Using Membership operator
str = 'Python operators'
dict = {6:'June',12:'Dec'}

print('P' in str) 
print('Python' in str)
print('python' not in str)
print(6 in dict) 
print('Dec' in dict)

Output-

True
True
True
True
False

Back to top

Quick wrap up – Python operators

This tutorial covered various Python operators and sought to describe each of them with examples. It should now be easier for you to use operators in Python.

If you find something new to learn today, then do share it with others. And, follow us on our social media (Facebook/Twitter) accounts to see more of this.

Best,

TechBeamers