Python operators
Python operators
and Logical AND: True if both the operands are true x and y
a = 10
b = 10
c = -10
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:
a = 10
if not a:
print("Boolean value of a is True")
• 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
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()
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.
Hello Geek
Hello Geek
Hello Geek
In Else Block