PWP unit 2 note
PWP unit 2 note
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”)
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.
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(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)
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
*
**
***
****
*****
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)
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