Basic Operators In Python Programming Language

In this lesson of Python Programming Language, we will learn Basic Operators in Python Programming Language their types and how we can use them in our code.

Now we will discuss each of the above topics in detail to learn to understand them easily. I am sure after going through this lesson you will be able to implement these python operators in your code easily.

Python Basic Operators

Python operators are special symbols dedicated to performing specific tasks in a programming language. These operators consist of two parts one is the operator symbol itself and the other is operand and the final result is called output.  See the illustration below.

20 * 20 = 400

In the above illustration, both side 20 and 20 are called the operands and the symbol of multiplication( * ) is called an operator and the final result which is 400 is called output.

Now we will discuss each of the above operators in detail.

Python Arithmetic Operators

These operators are used to perform the operation like addition, subtraction, multiplication, and division. See the table below to understand more easily.

Symbol of Arithmetic Operators Name and Its FunctionsDemo Illustration
It uses to add two values.
10 + 10 = 20
-Subtraction:
It uses to subtract one values from another value.
10 - 5 = 5
*Multiplication:
It uses to multiply values with each other.
5 * 5 = 25
/Division:
It always perform division from left operand to right one.
10 / 5 = 2
%Modulus:
It divides the left operand with right operand and return remainder in the output.
12 / 5 = 2.4
** Exponent:
It multiply the right operand to the power of left operand.
4**3 = 32

Example Of Python Arithmetic Operators

# Examples of Arithmetic Operator
x = 10
y = 20

# Python Addition of numbers
addition = x + y
# Python Subtraction of numbers
subtraction = x - y
# Python Multiplication of number
multiplication = x * y
# Python Division(float) of number
division = x / y
# Python Division(floor) of number
division2 = x // y
# Python Modulo of both number
modulus = x % y

# Output of the above results

print(subtraction)
print(multiplication)
print(division)
print(division2)
print(modulus)

We the above program code executes the following results shows

30
-10
200
0
0
10

Python Comparison Operators

These operators are used to compare the values and it gives us result back to us in the form of True or False. These operators are also called “Relational Operators“. See the table below to understand more easily.

Symbol of Comparison OperatorsName and Its FunctionsDemo Illustration
!ERROR! unexpected operator '='Equal To: Return True if values on both sides are equal otherwise return False.a == b
!=Not equal to: Return True if values on both sides are not equal otherwise return Falsea !=b
<>Not equal to: Work same like the above not equal operator(a <> b ) work same like a != b
>Greater than: If the value on the left side is bigger than the value on the right side then it return True Otherwise return False.a > b
<Less Than: If the value on the left side is smaller than the value on the right side it returns True otherwise False. a < b
>=Greater Than Or Equal To: If the value on the left side is greater or equal to the value on the right side it returns True otherwise returns False. a >= b
<= Smaller Than Or Equal To: If the value on the left side is smaller or equal to the value on the right side it returns True otherwise returns False. a <= b
Recommended:  While Loop In Python Programming Language

Example Of Python Comparison Operators

a = 15
b = 12

if ( a == b ):
print "This Shows - a is equal to b"
else:
print "This Shows - a is not equal to b"

if ( a != b ):
print "This Shows - a is not equal to b"
else:
print "This Shows - a is equal to b"

if ( a <> b ):
print "This Shows - a is not equal to b"
else:
print "This Shows - a is equal to b"

if ( a < b ):
print "This Shows - a is less than b"
else:
print "This Shows - a is not less than b"

if ( a > b ):
print "This Shows - a is greater than b"
else:
print "This Shows - a is not greater than b"

if ( a <= b ):
print "This Shows - a is either less than or equal to  b"
else:
print "This Shows - a is neither less than nor equal to  b"

if ( b >= a ):
print "This Shows - b is either greater than  or equal to b"
else:
print "This Shows - b is neither greater than  nor equal to b"

After execution of the above code the following results shows.

This Shows - a is not equal to b
This Shows - a is not equal to b
This Shows - a is not equal to b
This Shows - a is not less than b
This Shows - a is greater than b
This Shows - a is neither less than nor equal to  b
This Shows - b is neither greater than  nor equal to b

Python Assignment Operators

When you need to assign the value of one operand to other operand or variable you need to use assignment operators.  There are different types of assignment operators. See the table below to understand more easily.

Symbol of Assignment OperatorsName and Its FunctionsDemo Illustration
=Assignment: It assigns the value of right operand to the left operand.z = x + y
It assigns the value of ( x + y ) to z
+=Add AND: It first add value of right operand to the left operand and then assign added value to the operand left side of the assignment operator.z+= x is equal to
z = z + x
-= Subtract AND: It first subtract the value of right operand from left operand and then assign subtracted value to the operand left side of the assignment operator.z-= x is equal to
z = z - x
*= Multiply AND: It first multiply right operand with left operand and assigns the multiplied value to the operand left side of the assignment operator. z*= x is equal to
z = z * x
/=Divide AND: It divides the left operand with the right operand and assigns the divided value to the operand left side of the assignment operator. z/= x is equal to
z = z / x
%= Modulus AND: It returns the remainder after division of two operands. z %= x is equal to
z = z % x
**= Exponent AND: It performs exponential power calculation and store the result to operand left side of the assignment operator. z**= x is equal to
z = z** x
//=Floor Division: This operators do floor division on the operands and assigns value to the left operand.z //= x is equivalent to z = z // x
Recommended:  Decision Making Statements In Python Programming Language

Example Of Python Assignment Operators

x = 10
y = 12
z = 0

z = x + y
print "This Shows - Value of c is ", z

z += x
print "This Shows that the Value of c is ", z

z *= x
print "This Shows that the Value of c is ", z

z /= x
print "This Shows that the Value of c is ", z

z  = 2
z %= x
print "This Shows that the Value of c is ", z

z **= x
print "This Shows that the Value of c is ", z

z //= x
print "This Shows that the Value of c is ", z

After execution of the above code, the following results will be shown.

This Shows that the Value of c is  22
This Shows that the Value of c is  32
This Shows that the Value of c is  320
This Shows that the Value of c is  32
This Shows that the Value of c is  2
This Shows that the Value of c is  1024
This Shows that the Value of c is  102

Python Logical Operators

Python Programming Language supports some of the logical operators which are as follows:

Logical OperatorsFunctionsDemo Illustration
and Logical ANDThis operator returns True if only both the operands are true. ( x and y ) is True
or Logical ORThis operator returns True if any one of the both operands are non-zero then it becomes true.( x or y ) is True
not Logical NOTThis operator is use to reverse the current state of the operands. Not(a and b) is false

Example Of Python Logical Operators

x = True
y = False

# Output: x and y is False
print('x and b is',x and y)

# Output: x or y is True
print('x or y is',x or y)

# Output: not x is False
print('not x is',not x)

After execution of the above code, the following results will be shown.

('x and y is', False)
('x or y is', True)
('not x is', False)

Python Bitwise Operators

Another type of operators that work in Python Programming Language is called Bitwise Operators. They work on bits and perform their actions in a bit by bit sequence. We will take some values for better understanding.

Let’s assume x = 10 which is  0000 1010 in binary and y = 4 which is 0000 0100

Now we do calculation using Python Bitwise Operators.

x = 0000 1010

y = 0000 0100

——————-

Symbol of Bitwise OperatorsName and Its FunctionsDemo Illustration
&Binary AND: It gets the bit to the results if it exists in both operands.x& y = 0 (0000 0000)
|Binary OR: It gets the bit in the result
if it exists in either operands
x | y = 14 (0000 1110)
~Binary NOT: It's function is to reverse the value.
It is also called Binary one's complement
~x = -11 (1111 0101)
^Binary XOR: It gets the bit if it is available in one operand but not both.x ^ y = 14 (0000 1110)
>>Binary Right Shift: The left operands value is moved right by the number of bits specified by the right operand.x>> 2 = 2 (0000 0010)
<<Binary Left Shift: It's main function is that The left operands value is moved left by the number of bits specified by the right operand.x<< 2 = 40 (0010 1000)
Recommended:  How To Make Basic Calculator Using Python Programming Langauge

Python Membership Operators

In Python Programming Language membership operators are used to checking whether a value or variable present in a  sequence in lists, strings or in tuples. There are two membership operators which are as follows:

Symbol of Membership OperatorsFunctionsDemo Illustration
inIt evaluates and If a value of variable found in a sequence it is True otherwise False. 10 in x
not inIt evaluates and check If a value of variable not found in a sequence it is
True otherwise False.
10 not in x

Example of Python Membership Operators

x = 5
y = 10
list = [1, 2, 3, 4, 5 ];

if ( x in list ):
print "This shows that x is available in the given list with sequence"
else:
print "This shows that x is not available in the given list with sequence"

if ( y not in list ):
print "This shows that y is not available in the given list with sequence"
else:
print "This shows that y is available in the given list with sequence"

x = 2
if ( x in list ):
print "This shows that x is available in the given list with sequence"
else:
print "This shows that x is not available in the given list with sequence"

After execution following result will be shown.

This shows that x is available in the given list with sequence
This shows that y is not available in the given list with sequence
This shows that x is available in the given list with sequence

Python Identity Operators

The Python Identity Operators are used in the case when we want to find out if two values or variable placed on the same memory address or location. Remember if two values of the variable are equal it does not mean they are identical.

SymbolFunctionDemo Illustration
isIt returns True if the operands are identical. x is y here it will true is x is identical to y
is notIt returns True if the operands are not identical x is not y here it will true is x is not identical to y

Example of Python Identity Operators

a1 = 7
b1 = 7
a2 = 'Hello'
b2 = 'Hello'
a3 = [1,2,3]
b3 = [1,2,3]

# Output Check: It is False
print(a1 is not b1)

# Output Check: It is True
print(a2 is b2)

# Output Check: It is also False
print(a3 is b3)

After execution following result will be shown.

False
True
False

Jack Sparrow

Hay I am Admin Of This Website. I am very passionate about Computers and technology. This website is the result of my passion. I love to eliminate the barriers comes in the for seeking education. This website is the free source of learning about new technology and up to date knowledge.

This Post Has 4 Comments

1. What IDE should I use to type python?

1. You should use Atom IDE because it’s easy to understand.

2. What a good and detailed article. Good one.

1. Thanks.