0% found this document useful (0 votes)
6 views

PWP UNIT 2

The document provides an overview of Python operators and control flow statements, detailing various types of operators including arithmetic, comparison, assignment, logical, bitwise, membership, and identity operators. It also explains control flow statements such as conditional statements (if, if-else, nested if, if-elif-else) and loop statements (while and for loops), along with examples for each. Additionally, it covers operator precedence and loop manipulation techniques like break and continue.

Uploaded by

phalketanu1205
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

PWP UNIT 2

The document provides an overview of Python operators and control flow statements, detailing various types of operators including arithmetic, comparison, assignment, logical, bitwise, membership, and identity operators. It also explains control flow statements such as conditional statements (if, if-else, nested if, if-elif-else) and loop statements (while and for loops), along with examples for each. Additionally, it covers operator precedence and loop manipulation techniques like break and continue.

Uploaded by

phalketanu1205
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Programming with Python(22616)

UNIT II : Python Operators and Control Flow Statements


 Basic Operators:
 Operators are the constructs which can manipulate the value of operands.
 The values of operator uses for performing computation are called operands.

 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:

Operator Meaning Example


+(Addition) It is used to add two operands if a = 2, b = 1
a+b=3
-(Subtraction) It is used to subtract the second operand from the first if a = 20, b = 10
operand. If the first operand is less than the second a - b = 10
operand, the value results negative.
*(multiplication) It is used to multiply one operand with the other if a = 20, b = 10
a * b = 200
/(Divide) It returns the quotient after dividing the first operand by , if a = 20, b = 10
the second operand a/b = 2
%(Mod) It returns the reminder after dividing the first operand by if a = 20, b = 10
the second operand a%b = 0
**(Exponent) It is an exponent operator represented as it calculates the 2**3=8
first operand power to second operand.
//(Floor It gives the floor value of the quotient produced by 10//3=3
Dividion) dividing the two operands

Mrs.K.G.Raut
Programming with Python(22616)

 Comparison / Relational Operators :

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 :

Operator Meaning Example


= It assigns the value of the right expression to the left
operand.
+= It increases the value of the left operand by the value of if a = 10, b = 20 a+ = b
the right operand and assign the modified value back to will be equal to a = a +
left operand. b and therefore, a = 30
-= It decreases the value of the left operand by the value of if a = 20, b = 10 => a-
the right operand and assign the modified value back to = b will be equal to a =
left operand. a- b and therefore, a =
10.
*= It multiplies the value of the left operand by the value if a = 10, b = 20 => a*
of the right operand and assign the modified value back = b will be equal to a =
to left operand. a* b and therefore, a =
200
%= It divides the value of the left operand by the value of if a = 20, b = 10 => a
the right operand and assign the reminder back to left % = b will be equal to
operand. a = a % b and
therefore, a = 0.
**= a**=b will be equal to a=a**b if a = 4, b =2, a**=b

Mrs.K.G.Raut
Programming with Python(22616)

will assign 4**2 = 16


to a.
//= A//=b will be equal to a = a// b if a = 4, b = 3, a//=b
will assign 4//3 = 1 to
a

 Logical Operators :

Operator Meaning Example


x=5
and Logical
If both the operands are true then condition becomes true. print(x > 3 and x < 10)
AND
output : True

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

Operator Meaning Example


&(binary If both the bits at the same place in two operands are 1,
and) then 1 is copied to the result. Otherwise, 0 is copied. a & b = 0000
The resulting bit will be 0 if both the bits are zero
|(binary or) otherwise the resulting bit will be 1.. a or b = 1110
The resulting bit will be 1 if both the bits are different
^(binary otherwise the resulting bit will be 0. a ^ b = 1110
xor)
~(negation) It calculates the negation of each bit of the operand, i.e., ~ a = 0101
if the bit is 0, the resulting bit will be 1 and vice versa.

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)

 Python Operator Precedence :


 When more than one operator appears in an expression, the order of evaluation depends
on the rule of precedence.
 The acronym PEMDAS is a useful way to remember the order of operations.

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

2. E: Exponentiation has the next highest precedence, so 2**3 is 8.

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.

 The if statement is the simplest form of the conditional statement.

Mrs.K.G.Raut
Programming with Python(22616)

Syntax :

if condition:

statement

Example 1 :

n=int(input("Enter value of n"))


if n%2==0:
print ("Even Number")

O/p :

Enter value of n

10
Even Number

Example 2:

a = int (input ("Enter value of a "))


b = int(input ("Enter value of b "))
c = int(input ("Enter value of c "))
if a>b and a>c:
print ("a is largest")
if b>a and b>c:
print ("b is largest")
if c>a and c>b:
print ("c is largest")

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)

if a>b and a>c:


print ("a is largest")
if b>a and b>c:
print ("b is largest")
else:
print ("c is largest")'''

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 :

m=int(input("enter the marks"))


if m >=75:
print("Grade: Distinction")
elif m >=60:
print("Grade: First Class")
elif m >=50:
print("Grade: Second Class")
elif m >=40:
print("Grade: Pass Class")
else:
print("Grade: Fail")

O/p :

enter the marks


50
Grade: Second Class

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.

 The flow of execution is specified as follows :

1. Using the condition determine if the given expression is true or false.


2. If the expression is false then exit the while statement
3. If the expression is true then execute the body of the while and go back to step 1 in which
again condition is checked.
Example 1 :

i=1
while i<=10:
print(i)
i=i+1
O/p:
1 2 3 4 5 6 7 8 9 10

Example 2 :

a=int(input("enter table number"))


i=1
while i<=10:

Mrs.K.G.Raut
Programming with Python(22616)

print(a,"x",i,"=",a*i)
i=i+1
O/p:

enter table number

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 :

print("Enter the value of n")


n=int(input())
sum=0
for i in range(1,n+1):
sum=sum+i
print("The sum of ",n,"numbers is: ",sum)

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 :

Write a program to display the star pattern as

*
**
***
****
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:

The num has value: 1


The num has value: 2
Out of loop

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 :

for i in range(1, 11):


if i == 3:
continue
else:
# otherwise print the value
# of i
print(i, end=" ")

Mrs.K.G.Raut
Programming with Python(22616)

O/P :

1 2 4 5 6 7 8 9 10

3. Pass

 In Python, pass keyword is used to execute nothing;


 It means, when we don't want to execute code, the pass can be used to execute empty.
 It is same as the name refers to.

Example :

for i in [1,2,3,4,5]:
if i==3:
pass
print(" pass when value is",i)
print(i)

o/p:

pass when value is 1


1
pass when value is 2
2
pass when value is 3
3
pass when value is 4
4
pass when value is 5
5

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;

else:print("the while loop exhausted")

O/p:

1
2
3
4
5
the while loop exhausted

Mrs.K.G.Raut

You might also like