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

PWP unit 2 note

The document covers Python operators and control flow statements, focusing on selection/conditional branching statements such as if, if-else, nested if, and if-elif-else. It also discusses basic loop structures including while and for loops, along with break, continue, and pass statements. Additionally, it explains nested loops and the use of else statements with loops.

Uploaded by

gangurdekasturi
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 note

The document covers Python operators and control flow statements, focusing on selection/conditional branching statements such as if, if-else, nested if, and if-elif-else. It also discusses basic loop structures including while and for loops, along with break, continue, and pass statements. Additionally, it explains nested loops and the use of else statements with loops.

Uploaded by

gangurdekasturi
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/ 17

Unit-II

Python Operators and Control Flow Statements

Selection/Conditional Branching Statements:


The Selection or conditional branching statements are used as decision making statements.
These are: 1.if
2. if else
3. Nested if
4.if-elif-else
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. It is simplest form of the
conditional statement.
Syntax:
if condition:
Statement

Example:
if a<10:
print(“The number is less than 10”)
2.if-else statement: The if-else statement is used to test particular condition. If the condition is
true then it executes if block statements and if the conditions is false then it executes else
block statements.
Syntax:
if condition:
Statement
else:
Statement

Example:
print(“Enter the number:”)
n=int(input())
if n%2==0:
print(“Even number”
else:
print(“odd number”)

3. Nested if: When one if condition is present inside another if then it is called nested if
conditions.
Syntax:
if condition:
statement
else:
if condition:
statement
else:
statement

Example: Write a python program to compare two numbers using nested conditions.
print(“Enter the value of a:”)
a=int(input())
print(“Enter the value of b:”)
b=int(input())
if a==b:
print(“Both the numbers are equal”)
else:
if a<b:
print(“a is less than b”)
else:
print(“a is greater than b”)
4.if-elif-else: sometimes there are more than two possibilities. These possibilities can be
expressed using chained conditions. The chained conditional execution will be such that each
condition is checked in order. The elif is basically abbreviation of else if. If there is else clause
then it should be at the end.
Syntax:
if condition:
statement
elif condition:
statement
elif condition:
statement
else:
statement

Example:
Write a python program to display the result such as distinction, first class, second class,
pass or fail based on the marks entered by the user.
print(“Enter your marks:”)
m=int(input())
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”)

Example: Write a python program to find the largest among the three numbers.
print( “Enter First number:”)
x=int(input())
print( “Enter second number:”)
y=int(input())
print( “Enter Third number:”)
z=int(input())
if (x>y) and (x>z):
print(“First Number is largest”)
elif (y>x) and (y>z):
print(“Second Number is largest”)
elif(z>x) and (z>y):
print(“Third number is largest”)
else:
print(“All numbers are equal”)

Basic Loop Structures/Iterative Statements:


Iteration is a technique that allows to execute a block of statements repeatedly.
Definition: Repeated execution of a set of statements is called iteration.
The iterative statements are: 1.While
2.For
3.Break
4.Continue

1. While Loop: It is popularly used for representing iteration.


Syntax:
while test_condition:
body of while
1. Using condition we check expression is true or false
2. If the expression is true then execute the body of while and goback to step1 in which
again the condition is checked.
3. If the expression is false then exit the while statement.

Example:
1. Write a program to print numbers from 1 to 10.
i=0
while i<10:
i=i+1;
print(i)
2. Write a python program for computing the sum of n natural numbers and finding
out the average of it.

print(“ Enter the value of n:”)


n=int(input())
sum=0
avg=0
i=1
while i<=n:
sum=sum+i
i=i+1
print(“sum is:”,sum)
avg=sum/n
print(“Average is:”,avg)
3. Write a python program to display square of n numbers using while loop.
print("Enter the value of n:")
n=int(input())
i=1
print("The squre table is as given below:")
while i<=n:
print(i,i*i)
i=i+1

4. Write a python program for displaying even or odd numbers between 1 to n.


print("Enter the value of n:")
n=int(input())
i=1
j=1
while j<=n:
if i%2==0:
print(i,"is even")
else:
print(i,"is odd")
j=j+1
i=j
5. Write a python program to display Fibonacci numbers for the sequence of length n.
print("Enter the value of n:")
n=int(input())
a=0
b=1
i=0
print("Fibonacci Sequence is:")
while i<n:
print(a)
c=a+b
a=b
b=c
i=i+1
2. For Loop: The for loop is another popular way of using iteration.
Syntax:
1.for variable in sequence:
Body of for loop

Here <variable> is a variable that is used for iterating over a <sequence>. On every
iteration it takes the next value from <sequence> until the end of sequence is reached.
Function range()

However we can also use a range() function in for loop to iterate over numbers defined by
range().

range(n): generates a set of whole numbers starting from 0 to (n-1).


For example:
range(8) is equivalent to [0, 1, 2, 3, 4, 5, 6, 7]

range(start, stop): generates a set of whole numbers starting from start to stop-1.
For example:
range(5, 9) is equivalent to [5, 6, 7, 8]

Example:
1. Write a program to print numbers from 1 to 10.
for i in range(1,11):
print(i)

2. Write a python program to find the sum of 1 to n numbers

print("Enter the value of n:")


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

3. Write a python program to display the multiplication table

print("Enter the number for its multiplication table:")


n=int(input())
for i in range(1,n+1):
print(n, "x" ,i,n*i)

4. Write a python program to check whether given number is prime or not.


print("Enter the number:")
n=int(input())
if n>1:
for i in range(2,n):
if n%i==0:
print("number is not prime")
break
else:
print("number is prime")
else:
print("Number is not prime")

Q. Compare Pre-test and Post-test loop


Sr.no Pre-test loop Post-test loop
1 The condition is specified at the beginning The condition is specified at the bottom
of the loop of the loop.
2 If the condition is false, then the loop does Even-if the condition is false, the loop
not get executed at all. executes at least single time.
3 There are n+1 number of tests There are n number of tests.
4 Minimum number of iterations can be Minimum number of iterations are one.
zero
5 The loop is initialized only once. The loop gets initialized twice.

Q. Compare Condition controlled loop and Counter Controlled loop.

Sr.No Condition-controlled loop Counter controlled loop


1 This type of loop is used when we do not This type of loop is used when the number of
know for how many number of times the times loop could be executed is already
loop should be executed. known.
2 There is no counter used The counter is used
3 It is called indefinite loop It is called definite loop.
4 Generally while loop is used to implement Generally for loop is used to implement
condition controlled loop. counter controlled loop.
5 i=1 For i in range(1,10):
while (i<=10): Print(i)
print(i)
i=i+1

Nested Loop:
Nested loops are the loop structure in which one loop is present inside the other loop.
There can be nested for loops. That means one for loop is present inside the other for loop. 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:
1. Write a python program to display the star pattern as
*
**
***
****
*****

print("Enter the number:")


n=int(input())
for i in range(1,n+1):
for j in range(1,i+1):
print("*",end=" ")
print("")

2.Write a python program to display the star pattern as


*****
****
***
**
*

print("Enter the number:")


n=int(input())
for i in range(n+1,1,-1):
for j in range(1,i):
print("*",end=" ")
print("")
3.Write a python program to display the star pattern as
*
**
***
****
*****
****
***
**
*
print("Enter the number:")
n=int(input())
for i in range(1,n+1):
for j in range(1,i+1):
print("*",end=" ")
print("")
for i in range(n,1,-1):
for j in range(1,i):
print("*",end=" ")
print("")

4. Write a python program to display the number pattern as


12345
2345
345
45
5
print("Enter the number:")
n=int(input())
for i in range(1,n+1):
for j in range(i,n+1):
print(j,end=" ")
print("")

5. Write a python program to print the pattern as given below.


A
AB
ABC
ABCD
ABCDE
print("Enter the number:")
n=int(input())
for i in range(1,n+1):
for j in range(65,65+i):
a=chr(j)
print(a,end=" ")
print("")

6. Write a python program to print the pattern as given below.


AAAAA
BBBB
CCC
DD
E
n=65
for i in range(0,5):
for j in range(i,5):
a=chr(n+i)
print(a,end=" ")
print("")

Break Statement:
The break statement is used to transfer the control to the end of the loop.
When break statement is applied then loop gets terminates and the control goes to the next
pointing after loop body.
Syntax:
Break
Example:
for i in range(0,5): i=0
if i==3: while i<5:
print("Number found") i=i+1
break if i==3:
print(i) print("Number found")
break
print(i)
2. Continue
The Continue statement is used to skip some statements inside the loop.
The continue statement forces to execute the next iteration of the loop to execute.
Syntax:
Continue

Example:
for i in range(0,5): i=0
if i==3: while i<5:
continue i=i+1
print(i) if i==3:
continue
print(i)

3. Pass
The pass statement is used when we do not want to execute any statement. Thus the desired
statements can be bypassed.
Syntax:
pass

Example:
for i in range(0,5): i=0
if i==3: while i<5:
pass i=i+1
print(i) if i==3:
pass
print(i)

4. Else statement with loops:


The else block is placed just after the for or while loop. It is executed only when the loop is not
terminated by a break statement.
Else statement with for loop
Syntax:
for variable in sequence:
body of for loop
else:
statement
for i in range(1,10): for i in range(1,10):
print(i) print(i)
break
else: else:
print("good Bye") print("good Bye")

Output:
1 output:
2 1
3
4
5
6
7
8
9
good Bye
In above case, in first case, else part is executed when for loop condition false. but in second
column, we given break statement in for loop and there by for loop terminates but else part does
not get executed.
Else statement with while loop
Syntax:
while condition:
body of while
else:
Statements
Example:
i=0 i=0
while i<5: while i<5:
i=i+1 i=i+1
print(i) print(i)
break
else: else:
print("good Bye") print("good Bye")

output: output:
1 1
2
3
4
5
good Bye

You might also like