Ch No 2 Python Operator & Control Flow-1
Ch No 2 Python Operator & Control Flow-1
Program Output
a,b=10,30
print("addition Operator +:",a+b) addition Operator +: 40
print("subtraction Operator -:",b-a) subtraction Operator -: 20
print("Multiplication Operator *=:",a*b) Multiplication Operator *=:
print("Division Operator /=:",b/a) 300
print("Modulus Operator %=:",b%a) Division Operator /=: 3.0
print("Floor Division Operator //=:",b//a) Modulus Operator %=: 0
Floor Division Operator //=:
3
Comparison (Relational) Operators
Program Output
a,b=10,30
print("Greater Than Operator Greater Than Operator >: False
>:",a>b) Less Than Operator <: True
print("Less Than Operator Greater Than or equal to Operator
<:",a<b) >=: False
print("Greater Than or equal to Less Than Operator <=: True
Operator >=:",a>=b) Equals To Operator ==: False
print("Less Than Operator not Equals to Operator !=: True
<=:",a<=b)
print("Equals To Operator
==:",a==b)
print("not Equals to Operator !
=:",a!=b)
Assignment Operators
a,b=10,30
a+=b
print("add AND Operator +=:",a) add AND Operator +=: 40
a-=b
print("subtract AND Operator -=:",a) subtract AND Operator -=: 10
a*=b
print("Multiply AND Operator *=:",a) Multiply AND Operator *=: 300
a/=b
print("Division AND Operator /=:",a) Division AND Operator /=:
c=2**3 10.0
print(" Exponent AND Operator **=:",c)
a//=b Exponent AND Operator **=:
print("Floor Division AND Operator 8
//=:",a)
d=30%3 Floor Division AND
print(" Modulus AND Operator **=:",d) Operator //=: 0.0
Modulus AND Operator **=: 0
Logical Operators
Program Output
a,b=10,30
print("Logical AND Operator and:",a>b Logical AND Operator and:
and a<b) False
print("Logical OR Operator and:",a>b or Logical OR Operator and:
a<b) True
print("Logical NOT Operator and:",not a>b Logical NOT Operator and:
) True
Bitwise Operators
Program Output
a,b=4,2
print("Bitewise AND Operator a&b=",a&b) Bitewise AND Operator
print("Bitewise OR Operator a|b=",a|b) a&b= 0
print("Bitewise NOT Operator ~a=",~a) Bitewise OR Operator a|b=
print("Bitewise XOR Operator a^b=",a^b) 6
print("Bitewise LEFT SHIFT Operator Bitewise NOT Operator
a<<b=",a<<b) ~a= -5
print("Bitewise RIGHT Operator Bitewise XOR Operator
a>>b=",a>>b) a^b= 6
Bitewise LEFT SHIFT
Operator a<<b= 16
Bitewise RIGHT Operator
a>>b= 1
Membership Operators
Program Output
list=[10,20,"AMIT",30.45]
print("10 is present in list in= ",10 in 10 is present in list in= True
list) 10 is not present in list not in=
print("10 is not present in list not in= False
",10 not in list)
Identity Operators
Program Output
a=10
b=10
c=20
print("Does a & b point to same Does a & b point to same
memory location :is= ",a is b) memory location :is= True
print("Does a & c point to same memory Does a & c point to same
location :is not= ",a is not c) memory location :is not= True
Example
a=3
b=3.5
print(a is b)
#Output:False
a=3
b=4
print(a is b)
#Output:False
Example
a=3
b=3
print(a is b)
#Output:True
x=10
print(type(x) is int)
#Output:True
x2='Hello'
y2='Hello'
print (x2 is y2)
#Output:True
x3=[1,2,3]
y3=[1,2,3]
print (x3 is y3)
#Output:False
x3=y3
print (x3 is y3)
True
Precedence of operators
Operator Desciption
** Exponential Operator
*,/,%,// Multipln,Divison,modulus,Floor Division
+,- Addition,Sutraction
<<,>> Bitwise left shift and Right shift
& Bitwise AND
^,| Bitwise XOR & OR
<=,<,>,>= Comparison Operator
<>,==,!= Equality Operator
=,%=,/=,//=,-=,+=,*=,**= Assignment Operator
is ,not is Identity Operator
in ,not in Membership Operator
Associativity
► The control flow Statement is the order in which the program's code
executes.
► The control flow of a Python program is regulated by conditional
statements, loops, and function calls.
► The Control Flow statements are as
I].Conditional statement or Decision Making Statements.
II].Loop Control Statement or Iterative Statements.
I].Conditional statement or Decision Making
Statements.
# if statement example
if 10 > 5:
print("10 greater than 5")
print("Program ended")
output
if-else statement
else:
print(n,"is odd number")
#Output:
number is even or odd
Enter the number:5
5 is odd number
string=input("Enter string:")
if(string==string[::-1]):
print("The string is palindrome")
else:
print("The string is not palindrome")
# [::-1] means start at the end of the string and end at position 0, move with the step -1
, negative one, which means one step backwards.
output
Enter string:madam
The string is palindrome
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
Flowchart of if...elif...else
example
# if-elif statement example
letter = "A"
if letter == "B":
print("letter is B")
else:
print("letter isn't A, B or C")
output
Nested if statements
# if condition 1
if i != 0:
# condition 1
if i > 0:
print("Positive")
# condition 2
if i < 0:
print("Negative")
else:
print("Zero")
output
II].Loop Control Statement or
Iterative Statements.
► A loop statement allows us to execute a statement or group of
statements multiple times.
► The following diagram illustrates a loop statement −
► Python provides the following Loop statement
while Loop
for Loop
Nested Loop
While loop
► The for loop in Python is used to iterate over a sequence (list, tuple, string)
or other iterable objects. Iterating over a sequence is called traversal.
► Syntax:
► Here, val is the variable that takes the value of the item inside the
sequence on each iteration.
► Loop continues until we reach the last item in the sequence. The body of
for loop is separated from the rest of the code using indentation.
Flowchart of for Loop
Example
#Output:
1
2
3
4
5
6
7
8
9
10
range() function
The range() function returns a sequence of numbers,starting from 0 by
default, and increments by 1(by default) and ends at a specified
numbers.
Syntax:range(start,end,step)
where,
start: An integer number specifying at which position to
start.Default is 0.
end:An integer number specifying at which position to
end,which is computed as end-1.This is mandatory argument to
specify.
step:An integer number specifying the increment.Default is 1.
x = range(3, 6)
for n in x:
print(n)
#Output:
3
4
5
Create a sequence of numbers from 3 to 19,
but increment by 2 instead of 1:
x = range(3, 20, 2)
for n in x:
print(n)
#Output:
3
5
7
9
11
13
15
17
19
Example
for i in range(1,11):
print(i,end=' ')
#Output:
1 2 3 4 5 6 7 8 9 10
Example
sum=0
for i in range(0,21,2):
print(i)
sum=sum+i
print("sum=",sum)
Output
#Output:
0
2
4
6
8
10
12
14
16
18
20
sum= 110
print table
n=int(input("Enter a number:"))
for i in range (1,11):
print(n,'*',i,'=',n*i)
output
Enter a number:9
9*1=9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90
Nested Loop
► nesting a loop means simply having a loop (let's call it outer loop)
that has inside its commands another loop
► Syntax nested for loop
nested for example
for i in range(1,5):
for j in range(1,(i+1)):
print(j,end=' ')
print()
output
1
12
123
1234
► Nested While Loop
Example nested while
i=1
while i<5:
j=1
while j<(i+1):
print(j,end=' ')
j=j+1
i=i+1
print()
output
1
12
123
1234
What is the use of break and continue in Python?
print("The end")
#Output:
s
t
r
The end
example break
i=0
while i<10:
i=i+1
if i==5:
break
print("i=",i)
#Output
i= 1
i= 2
i= 3
i= 4
Python continue statement
► The continue statement is used to skip the rest of the code inside a
loop for the current iteration only.
► Loop does not terminate but continues on with the next iteration.
► Syntax
continue
Flowchart of continue
► The working of continue statement in for and while loop is shown
below.
Example
for val in "string":
if val == "i":
continue
print(val)
print("The end")
Output:
s
t
r
n
g
The end
example continue
i=0
while i<10:
i=i+1
if i==5:
continue
print("i=",i)
Output:
i= 1
i= 2
i= 3
i= 4
i= 6
i= 7
i= 8
i= 9
i= 10
pass statement
for i in range(1,11):
if i%2==0:
pass
else:
print("odd numbers:",i)
output
odd numbers: 1
odd numbers: 3
odd numbers: 5
odd numbers: 7
odd numbers: 9
Write a program to print following
1
12
123
1234
for i in range(1,5):
for j in range(1,i+1):
print(j,end=' ')
print()
End