Types of Operators in Python
• Operators in general are used to perform operations on values and variables.
These are standard symbols used for the purpose of logical and arithmetic
operations.
• Types of Operators in Python
• Arithmetic Operators
• Comparison Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Identity Operators and Membership Operators
Types of Operators in Python
• Logical operators perform Logical AND, Logical OR, and Logical NOT operations. It
is used to combine conditional statements.
• Bitwise operators act on bits and perform bit-by-bit operations. These are used to
operate on binary numbers.
Logical operators
• Logical operators are used on conditional statements (either True or
False). They perform Logical AND, Logical OR and Logical NOT
operations.
OPERATOR DESCRIPTION SYNTAX
and Logical AND: True if both the operands are true x and y
or Logical OR: True if either of the operands is true x or y
not Logical NOT: True if operand is false not x
The Truth table for all combinations
Logical AND operator
Logical operator returns True if both the operands are True else
it returns False.
Example 1:
# Python program to demonstrate
# logical and operator
a = 10
b = 10
c = -10
if a > 0 and b > 0:
print("The numbers are greater than 0")
if a > 0 and b > 0 and c > 0:
print("The numbers are greater than 0")
else:
print("Atleast one number is not greater than 0")
Example 2:
# logical and operator
a = 10
b = 12
c = 0
if a and b and c:
print("All the numbers have boolean value as True")
else:
print("Atleast one number has boolean value as False")
Note: If the first expression evaluated to be false while using and operator, then the
further expressions are not evaluated.
Logical OR operator
• Logical or operator returns True if either of the operands is True.
Example 1:
# Python program to demonstrate
# logical or operator
a = 10
b = -10
c = 0
if a > 0 or b > 0:
print("Either of the number is greater than 0")
else:
print("No number is greater than 0")
if b > 0 or c > 0:
print("Either of the number is greater than 0")
else:
print("No number is greater than 0")
Example 2:
# Python program to demonstrate
# logical and operator
a = 10
b = 12
c = 0
if a or b or c:
print("Atleast one number has boolean value as True")
else:
print("All the numbers have boolean value as False")
Note: If the first expression evaluated to be false while using and operator, then the
further expressions are not evaluated.
Logical OR operator
• Logical or operator returns True if either of the operands is True.
Example 1:
# Python program to demonstrate
# logical or operator
a = 10
b = -10
c = 0
if a > 0 or b > 0:
print("Either of the number is greater than 0")
else:
print("No number is greater than 0")
if b > 0 or c > 0:
print("Either of the number is greater than 0")
else:
print("No number is greater than 0")
Example 2:
# Python program to demonstrate
# logical and operator
a = 10
b = 12
c = 0
if a or b or c:
print("Atleast one number has boolean value as True")
else:
print("All the numbers have boolean value as False")
Logical not operator
• Logical not operator work with the single boolean value. If the boolean value is True it returns
False and vice-versa.
Example 1:
# Python program to demonstrate
# logical not operator
a = 10
if not a:
print("Boolean value of a is True")
if not (a%3 == 0 or a%5 == 0):
print("10 is not divisible by either 3 or 5")
else:
print("10 is divisible by either 3 or 5")
Bitwise operation
OPERATOR NAME DESCRIPTION SYNTAX
Result bit 1,if both operand bits are
& Bitwise AND x&y
1;otherwise results bit 0.
Result bit 1,if any of the operand bit
| Bitwise OR x|y
is 1; otherwise results bit 0.
~ Bitwise NOT inverts individual bits ~x
Results bit 1,if any of the operand
^ Bitwise XOR bit is 1 but not both, otherwise x^y
results bit 0.
The left operand’s value is moved
>> Bitwise right shift toward right by the number of bits x>>
specified by the right operand.
The left operand’s value is moved
<< Bitwise left shift toward left by the number of bits x<<
specified by the right operand.
Bitwise AND operator
• Bitwise AND operator Returns 1 if both the bits are 1 else 0.
• a = 10 = 1010 (Binary)
b = 4 = 0100 (Binary)
a & b = 0000
= 0 (Decimal)
Example code:
# bitwise AND operator
a = 10
b=4
# Print bitwise AND operation
print("a & b =", a & b)
Output: a&b=0
Bitwise OR operator
• Bitwise OR operator Returns 1 if either of the bit is 1 else 0.
• a = 10 = 1010 (Binary)
b = 4 = 0100 (Binary)
a | b = 1110
= 14 (Decimal)
Example Code:
# bitwise OR operator
a = 10
b=4
# Print bitwise OR operation
print("a | b =", a | b)
Output: a | b = 14
Bitwise XOR operator
• Bitwise XOR operator: Returns 1 if one of the bits is 1 and the other is 0 else returns false.
• a = 10 = 1010 (Binary)
b = 4 = 0100 (Binary)
a ^ b = 1110
= 14 (Decimal)
Example code:
# bitwise XOR operator
a = 10
b=4
# print bitwise XOR operation
print("a ^ b =", a ^ b)
Output: a ^ b = 14
Bitwise NOT operator
• Bitwise NOT operator: Returns one’s complement of the number.
• For Example: a = 10 = 1010 (Binary)
In computers we usually represent numbers using 32 bits,
so binary representation of 10 is (....0000 1010)[32 bits]
~a is basically 1's complement of a
i.e ~a should be ~10 = ~(....0000 1010) = (....1111 0101) = intermediate-result
Since bitwise negation inverts the sign bit,
we now have a negative number. And we represent a negative number
using 2's complement.
Bitwise NOT operator
• 2's complement of intermediate-result is:
intermediate-res = 0101 //....1111 0101
1010 //....0000 1010 -(1's complement)
+1
-----------
= 1011 //....0000 1011
-----------
= -11 (Decimal)
thus ~a = -11
Bitwise NOT operator
• Example Code: # bitwise operators
a = 10
# Print bitwise NOT operation
print("~a =", ~a)
Output: ~a = -11
Shift Operators
• These operators are used to shift the bits of a number left or right thereby multiplying or
dividing the number by two respectively. They can be used when we have to multiply or
divide a number by two.
• Bitwise right shift: Shifts the bits of the number to the right and fills 0 on voids left( fills 1 in
the case of a negative number) as a result. Similar effect as of dividing the number with
some power of two.
• Example 1:
• a = 10 = 0000 1010 (Binary)
a >> 1 = 0000 0101 = 5
• Example 2:
• a = -10 = 1111 0110 (Binary)
a >> 1 = 1111 1011 = -5
Shift Operators
• Bitwise left shift: Shifts the bits of the number to the left and fills 0 on
voids right as a result. Similar effect as of multiplying the number with
some power of two.
• Example 1:
• a = 5 = 0000 0101 (Binary)
a << 1 = 0000 1010 = 10
a << 2 = 0001 0100 = 20
• Example 2:
• b = -10 = 1111 0110 (Binary)
b << 1 = 1110 1100 = -20
b << 2 = 1101 1000 = -40
Shift Operators
• Example:
# Python program to show
# shift operators
a = 10
b = -10
# print bitwise right shift operator
print("a >> 1 =", a >> 1)
print("b >> 1 =", b >> 1)
a=5
b = -10
# print bitwise left shift operator
print("a << 1 =", a << 1)
print("b << 1 =", b << 1)
Complex numbers
• Not only real numbers, Python can also handle complex numbers and its
associated functions using the file “cmath”.
• Complex numbers have their uses in many applications related to mathematics
and python provides useful tools to handle and manipulate them.
• Converting real numbers to complex number: An complex number is
represented by “ x + yi “.
• Python converts the real numbers x and y into complex using the function
complex(x,y).
• The real part can be accessed using the function real() and imaginary part can be
represented by imag().
Example 1:
# Python code to demonstrate the working of
# complex(), real() and imag()
# importing "cmath" for complex number operations
import cmath
# Initializing real numbers
x = 5
y = 3
# converting x and y into complex number
z = complex(x,y);
# printing real and imaginary part of complex number
print ("The real part of complex number is : ",end="")
print (z.real)
print ("The imaginary part of complex number is : ",end="")
print (z.imag)
Example 2:
# Aleternative way how can complex no can intilize
# importing "cmath" for complex number operations
import cmath
# Initializing complex number
z = 5+3j
# Print the parts of Complex No.
print("The real part of complex number is : ", end="")
print(z.real)
print("The imaginary part of complex number is : ", end="")
print(z.imag)
Loops in Python
• Python programming language provides the following types of loops to handle
looping requirements.
• Python provides three ways for executing the loops.
• While all the ways provide similar basic functionality, they differ in their syntax
and condition-checking time.
While Loop in Python
• In python, a while loop is used to execute a block of statements repeatedly until a
given condition is satisfied. And when the condition becomes false, the line
immediately after the loop in the program is executed.
• Syntax:
while expression:
statement(s)
Example of Python While Loop
# Python program to illustrate while loop
count = 0
while (count < 3):
count = count + 1
print("Hello Geek")
Output:
Hello Geek
Hello Geek
Hello Geek
Using else statement with While Loop
in Python
• The else clause is only executed when your while condition becomes false. If you
break out of the loop, or if an exception is raised, it won’t be executed.
• Syntax of While Loop with else statement:
while condition:
# execute these statements
else:
# execute these statements
Example 1:
# Python program to illustrate
# combining else with while
count = 0
while (count < 3):
count = count + 1
print("Hello Geek")
else:
print("In Else Block")
Output:
Hello Geek
Hello Geek
Hello Geek
In Else Block