PWP UNIT 2
PWP UNIT 2
Types of Operator :
Python language supports the following types of operators.
Arithmetic Operators
Comparison (Relational) Operators
Assignment Operators
Logical Operators
Bitwise Operators
Membership Operators
Identity Operators
Arithmetic operators:
Mrs.K.G.Raut
Programming with Python(22616)
Operator Meaning
== If the value of two operands is equal, then the condition becomes true.
!= If the value of two operands is not equal then the condition becomes true.
<= If the first operand is less than or equal to the second operand, then the
condition becomes true.
>= If the first operand is greater than or equal to the second operand, then the
condition becomes true.
> If the first operand is greater than the second operand, then the condition
becomes true.
< If the first operand is less than the second operand, then the condition
becomes true.
Assignment Operators :
Mrs.K.G.Raut
Programming with Python(22616)
Logical Operators :
or Logical OR If any of the two operands are true then condition becomes true. x=5
print(x < 3 or x < 4)
output : False
x=5
print(not(x > 3 and x < 10))
not Logical NOT Used to reverse the logical state of its operand.
Output : False
Reverse the result, returns
False if the result is true
Bitwise Operators :
The bitwise operators perform bit by bit operation on the values of the two operands.
Example : if a = 10; b = 4; then,
binary (a) = 1010 binary (b) = 0100
Mrs.K.G.Raut
Programming with Python(22616)
<<(left The left operand value is moved left by the number of a<<1 = 10<<1
0100
shift) bits present in the right operand.
>>(right The left operand is moved right by the number of bits a>>1 = 10>>1
0101
shift) present in the right operand.
Membership Operators :
Python membership operators are used to check the membership of value inside a Python data
structure. If the value is present in the data structure, then the resulting value is true otherwise it
returns false.
Operator Meanings
in It is evaluated to be true if the first operand is found in the second
operand (list, tuple, or dictionary).
Example : x=”Hello”
Print(“ll” in x)
Output : True
not in It is evaluated to be true if the first operand is not found in the second
operand (list, tuple, or dictionary).
Example : x=”Hello”
Print(“ll” not in x)
Output : False
Identity Operators :
Operator Meanings
is It is evaluated to be true if the reference present at both sides point to
the same object.
Example :
x=8
y=9
print(x is y)
false
is not It is evaluated to be true if the reference present at both side do not
point to the same object.
Example :
x=8
y=9
print (x is not y) True
Mrs.K.G.Raut
Programming with Python(22616)
1. P: Parentheses have the highest precedence and can be used to force an expression to
evaluate solution: in the order you want. Since expressions in parentheses are evaluated
first, for ex : 1 * (10-5) is 5
3. MDAS: Multiplication and Division have the same precedence, which is higher than
Addition and Subtraction, which also have the same precedence. So 2+3*4 yields 14
rather than 20.
4. Operators with the same precedence are evaluated from left to right. So in the
expression 3-2+1 will result 2. As subtraction is performed first and then addition will
be performed.
Control Flow :
The control statements are of three types :
1. conditional statements
2. Loop Statements
3. Loop Manipulation
Conditional Statements
1. if statement
2. if-else statement
3. Nested if statement
4. If-elif-else statement
1. If Statement
The if statement is used to test particular condition. If the condition is true then it executes the
block of statements which is called as if block.
Mrs.K.G.Raut
Programming with Python(22616)
Syntax :
if condition:
statement
Example 1 :
O/p :
Enter value of n
10
Even Number
Example 2:
O/p :
Enter value of a
5
Mrs.K.G.Raut
Programming with Python(22616)
Enter value of b
6
Enter value of c
7
c is largest
2. If-Else
The if-else statement provides an else block combined with the if statement which is executed
in the false case of the condition.
If the condition is true, then the if-block is executed. Otherwise, the else-block is executed.
Syntax :
if condition:
#block of statements
else:
#another block of statements (else-block)
Example 1 :
n=int(input("Enter value of n"))
if n%2==0:
print ("Even Number")
else:
print(“Odd Number”)
O/p :
Enter value of n
5
Odd Number
Example 2 :
a = int (input ("Enter value of a "))
b = int(input ("Enter value of b "))
c = int(input ("Enter value of c "))
Mrs.K.G.Raut
Programming with Python(22616)
O/p :
Enter value of a
1
Enter value of b
2
Enter value of c
3
c is largest
3. Nested if statement
When one if condition is present inside another if then it is called nested if conditions. Any
number of these statements can be nested inside one another. Indentation is the only way to
figure out the level of nesting.
Example :
num = 10
if num > 5:
print("Bigger than 5")
if num <= 15:
print("Between 5 and 15")
O/p:
Bigger than 5
Between 5 and 15
Mrs.K.G.Raut
Programming with Python(22616)
4. If-elif-else statement
The elif statement enables us to check multiple conditions and execute the specific block of
statements depending upon the true condition among them. However, using elif is optional.
The chained conditional execution will be such that each condition is checked in order.
The elif is basically abbreviation of else if.
There is no limit on the number of elif statements.
If there is else clause then it should be at the end.
Example :
O/p :
Mrs.K.G.Raut
Programming with Python(22616)
Loop Statements
While Loop :
The while loop is to be used in the scenario where we don't know the number of
iterations in advance. The block of statements is executed in the while loop until the
condition specified in the while loop is satisfied. It is also called a pre-tested loop.
i=1
while i<=10:
print(i)
i=i+1
O/p:
1 2 3 4 5 6 7 8 9 10
Example 2 :
Mrs.K.G.Raut
Programming with Python(22616)
print(a,"x",i,"=",a*i)
i=i+1
O/p:
5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
For Loop :
The for loop in Python is used to iterate the statements or a part of the program
several times. It is frequently used to traverse the data structures like list, tuple, or
dictionary.
Example 1 :
Mrs.K.G.Raut
Programming with Python(22616)
O/P :
Enter the value of n
5
The sum of 5 numbers is: 1
The sum of 5 numbers is: 3
The sum of 5 numbers is: 6
The sum of 5 numbers is: 10
The sum of 5 numbers is: 15
Nested Loop :
o Nested loops are the loop structure in which one loop is present inside the other loop.
o There can be nested for loops. That means one for loop is present inside the other for loop.
o In this case, the control first enters inside the outer loop then enters inside the inner for loop, it
then executes inner loop completely and then switch back to outer for loop.
Example :
*
**
***
****
Example :
n = int(input("Enter the number of rows you want to print?"))
i,j=0,0
for i in range(0,n):
print()
for j in range(0,i+1):
print("*",end="")
Loop Manipulation :
1. Break
2. Continue
3. Pass
4.Else
Mrs.K.G.Raut
Programming with Python(22616)
1. Break :
The break statement breaks the loops one by one, i.e., in the case of nested loops, it
breaks the inner loop first and then proceeds to outer loops.
break is used to abort the current execution of the program and the control goes to the
next line after the loop.
The break is commonly used in the cases where we need to break the loop for a given
condition.
Example :
num = 0
for i in range(10):
num += 1
if num == 3:
break
print("The num has value:", num)
print("Out of loop")
O/p:
Mrs.K.G.Raut
Programming with Python(22616)
2. Continue :
The continue statement in python is used to bring the program control to the beginning
of the loop.
The continue statement skips the remaining lines of code inside the loop and start with
the next iteration.
It is mainly used for a particular condition inside the loop so that we can skip some
specific code for a particular condition.
Example :
Mrs.K.G.Raut
Programming with Python(22616)
O/P :
1 2 4 5 6 7 8 9 10
3. Pass
Example :
for i in [1,2,3,4,5]:
if i==3:
pass
print(" pass when value is",i)
print(i)
o/p:
4. Else
The else block is executed when the condition given in the loop statement becomes false.
Example :
i=1
while i<=5:
print(i)
Mrs.K.G.Raut
Programming with Python(22616)
i=i+1;
O/p:
1
2
3
4
5
the while loop exhausted
Mrs.K.G.Raut