Unit Ii - Python Operators and Control Flow Statements
Unit Ii - Python Operators and Control Flow Statements
# Output: x + y = 19
print('x + y =',x+y)
# Output: x - y = 11
print('x - y =',x-y)
# Output: x * y = 60
print('x * y =',x*y)
# Output: x / y = 3.75
print('x / y =',x/y)
# Output: x // y = 3
print('x // y =',x//y)
# Output: x ** y = 50625
print('x ** y =',x**y)
2. ASSIGNMENT OPERATORS
ASSIGNMENT OPERATORS ARE USED IN PYTHON TO ASSIGN VALUES TO VARIABLES.
A = 5 IS A SIMPLE ASSIGNMENT OPERATOR THAT ASSIGNS THE VALUE 5 ON THE RIGHT TO THE
VARIABLE A ON THE LEFT.
THERE ARE VARIOUS COMPOUND OPERATORS IN PYTHON LIKE A += 5 THAT ADDS TO THE
VARIABLE AND LATER ASSIGNS THE SAME. IT IS EQUIVALENT TO A = A + 5
x = 10
y = 12
# Output: x == y is False
print('x == y is‘,x==y)
# Output: x != y is True
print('x != y is‘,x!=y)
True if operand is
false
not not x
(complements the
operand)
EXAMPLE : LOGICAL OPERATORS
IN PYTHON
x = True
y = False
print('x and y is',x and y)
print('x or y is',x or y)
print('not x is',not x)
5. IDENTITY OPERATORS
IS AND IS NOT ARE THE IDENTITY OPERATORS IN PYTHON. THEY ARE USED TO CHECK IF TWO VALUES (OR VARIABLES) ARE
LOCATED ON THE SAME PART OF THE MEMORY. TWO VARIABLES THAT ARE EQUAL DOES NOT IMPLY THAT THEY ARE IDENTICAL.
# Output: False
print(x1 is not y1)
# Output: True
print(x2 is y2)
6.MEMBERSHIP OPERATORS
IN AND NOT IN ARE THE MEMBERSHIP OPERATORS IN PYTHON.
THEY ARE USED TO TEST WHETHER A VALUE OR VARIABLE IS
FOUND IN A SEQUENCE
(STRING, LIST, TUPLE, SET AND DICTIONARY).
IN A DICTIONARY WE CAN ONLY TEST FOR PRESENCE OF KEY,
NOT THE VALUE.
True if
value/variable is
in 5 in x
found in the
sequence
True if
value/variable is not
not in 5 not in x
found in the
sequence
EXAMPLE : MEMBERSHIP
OPERATORS IN PYTHON
x = 'Hello world'
y = {1:'a',2:'b'}
# Output: True
print('H' in x)
# Output: True
print('hello' not in x)
# Output: True
print(1 in y)
# Output: False
print('a' in y)
7. BITWISE OPERATORS
BITWISE OPERATORS ACT ON OPERANDS AS IF THEY WERE
STRINGS OF BINARY DIGITS. THEY OPERATE BIT BY BIT, HENCE
THE NAME.
FOR EXAMPLE, 2 IS 10 IN BINARY AND 7 IS 111.
IN THE TABLE BELOW: LET X = 10 (0000 1010 IN BINARY) AND Y =
4 (0000 0100 IN BINARY)
a=20
b=10
c=a-b
print("Subtraction is : ",c)
2. SELECTION/DECISION CONTROL STATEMENTS/CONDITIONAL STATEMENTS
statement(s)
🞆Python if Statement Flowchart
EXAMPLE OF IF
#wap to check number is even or odd using IF
n = 10
if n % 2 == 0:
print("n is an even number")
🞆Output
n is an even number
IF...ELSE STATEMENT
Syntax
if test expression:
Body of if
else:
Body of else
PYTHON IF..ELSE FLOWCHART
EXAMPLE OF IF-ELSE
n=5
if n % 2 == 0:
print("n is even")
else:
print("n is odd")
🞆Output
n is odd
PYTHON IF...ELIF...ELSE STATEMENT
🞆The elif is short for else if. It allows us to check for multiple
expressions. If the condition for if is False, it checks the
condition of the next elif block and so on. If all the
conditions are False, body of else is executed.
🞆Only one block among the several if...elif...else blocks is
executed according to the condition. The if block can have
only one else block. But it can have multiple elif blocks.
SYNTAX OF IF...ELIF...ELSE
if test expression:
Body of if
elif test expression:
Body of elif
else:
Body of else
FLOWCHART OF IF...ELIF...ELSE
EXAMPLE OF IF..ELIF..ELSE
x = 15
y = 12
if x == y:
print("Both are Equal")
elif x > y:
print("x is greater than y")
else:
print("x is smaller than y")
NESTED IF STATEMENTS
Output:
5
10
15
20
25
30
35
40
45
50
FOR LOOP USING RANGE() FUNCTION
🞆The range() function is used to generate the sequence of the
numbers. If we pass the range(10), it will generate the numbers
from 0 to 9. The syntax of the range() function is given below.
🞆Syntax:
⚫ range(start,stop,step size)
🞆The start represents the beginning of the iteration.
🞆The stop represents that the loop will iterate till
stop-1. The range(1,5) will generate numbers 1 to 4
iterations. It is optional.
🞆The step size is used to skip the specific numbers from the
iteration. It is optional to use. By default, the step size is 1.
EXAMPLE : PROGRAM TO PRINT TABLE OF GIVEN
NUMBER.
n = int(input("Enter the number "))
for i in range(1,11):
c = n*i
print(n,"*",i,"=",c)
🞆 Output:
10 * 1 = 10
10 * 2 = 20
10 * 3 = 30
10 * 4 = 40
10 * 5 = 50
10 * 6 = 60
10 * 7 = 70
10 * 8 = 80
10 * 9 = 90
FOR LOOP WITH ELSE
🞆 A for loop can have an optional else block as well. The else part is
executed if the items in the sequence used in for loop exhausts.
🞆 break statement can be used to stop a for loop. In such case, the else part
is ignored.
🞆 Hence, a for loop's else part runs if no break occurs.
digits = [0, 1, 5]
for i in digits:
print(i)
else:
print("No items left.")
🞆 Output:
0
1
5
NESTED LOOP
🞆Python programming language allows to use one loop
inside another loop is called Nested loop.
🞆loop nesting is that you can put any type of loop inside
of any other type of loop. For example a for loop can be
inside a while loop or vice versa.
🞆Types:
⚫ Nested for loop
⚫ Nested while loop
NESTED FOR LOOP IN PYTHON
🞆Python allows us to nest any number of for loops inside
a for loop is called nested for loop.
🞆The inner loop is executed n number of times for every
iteration of the outer loop.
🞆Syntax
for iterating_var1 in sequence: #outer loop
for iterating_var2 in sequence: #inner loop
#block of statements
#Other statements
EXAMPLE- 1: NESTED FOR LOOP
# User input for number of rows
rows = int(input("Enter the rows:"))
# Outer loop will print number of rows
for i in range(1,rows+1):
# Inner loop will print number of Astrisk
for j in range(i):
print("*",end =“ '')
print(“\n”)
🞆 Output:
🞆 Syntax-
while expression: #outer loop
while expression: #inner loop
statement(s)
statement(s)
EXAMPLE: NESTED WHILE LOOP
🞆 #WAP to find the prime numbers from 2 to 100
i=2
while(i < 100):
j=2
while(j <= (i/j)):
if not(i%j):
break
j=j+1
if (j > i/j) :
print (i, " is prime“)
i=i+1
print ("Good bye!“)
WHILE LOOP:
Output:
1
2
3
4
5
6
7
8
9
10
WHILE LOOP WITH ELSE
🞆Same as that of for loop, we can have an optional else
block with while loop as well.
🞆The else part is executed if the condition in the while
loop evaluates to False. The while loop can be
terminated with a break statement. In such case, the else
part is ignored. Hence, a while loop's else part runs if no
break occurs and the condition is false.
EXAMPLE: # EXAMPLE TO ILLUSTRATE THE USE
OF ELSE STATEMENT WITH THE WHILE LOOP
counter = 0
while counter < 3:
print("Inside loop")
counter = counter + 1
else:
print("Inside else")
🞆Output:
Inside loop
Inside loop
Inside loop
Inside else
LOOP MANIPULATION USING
CONTINUE, PASS, BREAK :
🞆In Python, break and continue statements can alter the
flow of a normal loop.
PYTHON BREAK STATEMENT
🞆The break statement terminates the loop containing it.
🞆Control of the program flows to the statement
immediately after the body of the loop.
🞆If break statement is inside a nested loop (loop inside
another loop), break will terminate the innermost loop.
🞆Syntax of break
break
FLOWCHART OF BREAK
THE WORKING OF BREAK STATEMENT IN
FOR AND WHILE LOOP IS SHOWN BELOW
EXAMPLE : BREAK STATEMENT WITH FOR
LOOP
str = "python"
for i in str:
if i == 'o':
break
print(i)
🞆Output:
p
y
t
h
EXAMPLE : BREAK STATEMENT WITH WHILE LOOP
i=0
while 1:
print(i," ",end="")
i=i+1
if i == 10:
break
print("came out of while loop")
🞆Output:
0 1 2 3 4 5 6 7 8 9 came out of while loop
PYTHON CONTINUE STATEMENT
🞆Example
for i in range(1,11):
if i%2==0:
pass
else:
print(“odd number:”,i)
#PROGRAM TO ADD TWO NUMBERS PROVIDED BY THE USER