Python – Operators

After reading this Python operators topic, you will understand its theory and examples also you will able to use various types of Python operators.

An operator is a symbol that defines the kind of operation that Python interpreter has to perform. Python having various types of operators are classified as

  • Arithmetic operators
  • Assignment operators
  • Relational operators
  • Logical operators
  • Bitwise operators

Python – Arithmetic Operators

The arithmetic operator is used to performing the arithmetic operation on variables or constants. For explanation purpose, we will discuss arithmetic operators in table form as shown below,

OperationOperator
Addition+
Subtraction-
Multiplication*
division/
Modulus%
Floor division//
Exponentiation**

Example:

Program (1): To demonstrate the use of arithmetic operators.

a=2
b=3
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)

Output (1)

2 + 3 = 5
2 - 3 = -1
2 * 3 = 6
2 / 3 = 0.6666666666666666
2 % 3 = 2
2 // 3 = 0
2 ** 3 = 1

Python – Logical Operators:

Logical operators are used to combining two or more expression having the relational operator. Logical operators present in expression returns zero when the condition is evaluated as false otherwise it returns non-zero when the condition is evaluated as true. For explanation purpose, we will discuss logical operators in table form as shown below,

OperationOperatorExample
Logical ANDandA and B
Logical ORorA or B
Logical NOTnotA not B

Example:

Program (1): To demonstrate the use of logical operators.

a=0
b=10
print(a, 'not', 'is',not a)
print(a, 'or', b, 'is',a or b)
print(a, 'and', b, 'is', a and b)

Output (1)

0 not is True
0 or 10 is 10
0 and 10 is 0

Python – Relational Operators:

The relational operator can be used in an expression or to compare two numbers or characters.  For explanation purpose, we will discuss relational operators in table form as shown below,

OperatorOperation
!=Not equal to
<Less than
>Greater than
<=Less than equal to
>=Greater than equal to

The Relational operators with examples are

Example:

Program (1): To demonstrate the use of relational operators.

a=2
b=3
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 (1)

2 != 3 is True
2 < 3 is True
2 > 3 is False
2 <= 3 is True
2 >= 3 is False
2 == 3 is False

Python – Assignment Operators:

Assignment operators are used to assigning a value of variable, constant or expression to a variable. For explanation purpose, we will discuss assignment operators in table form as shown below,

OperatorStatement in Python programResult in Python
+=a+=ba=a+b
-=a-=ba=a-b
*=a*=ba=a*b
/=a/=ba=a/b
%=a%=ba=a%b
<<=a<<=ba=a<
>>=a>>=ba=a>>b
&=a&=ba=a&b
^=a^=ba=a^b

Example:

Program (1): To demonstrate the use of Assignment operators.

a=5
b=10
a=a+b
print('a += b','is',a)
a=5
b=10
a=a-b
print('a -= b', 'is',a)
a=5
b=10
a=a*b
print('a *= b', 'is', a)
a=5
b=10
a=a/b
print('a /= b','is', a)
a=5
b=10
a=a%b
print('a %= b', 'is', a)
a=5
b=10
a=a<<b
print('a <<= b', 'is', a)
a=5
b=10
a=a>>b
print('a >>= b', 'is', a)
a=5
b=10
a=a&b
print('a &= b', 'is', a)
a=5
b=10
a=a^b
print('a ^= b', 'is', a)

Output (1)

a += b is 15
a -= b is -5
a *= b is 50
a /= b is 0.5
a %= b is 5
a <<= b is 5120
a >>= b is 0
a &= b is 0
a ^= b is 15

Python – Bitwise Operators:

Bitwise operators are used to performing operations on bits. A bit is either 0 or 1.

Published by

Electrical Workbook

We provide tutoring in Electrical Engineering.

Leave a Reply

Your email address will not be published. Required fields are marked *