0% found this document useful (0 votes)
18 views51 pages

Naincy MOD 2 PYTHON

Uploaded by

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

Naincy MOD 2 PYTHON

Uploaded by

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

Name :- Naincy Jaiswal

Roll Number :- 22CS011107, Section :- B

Programming Practices II (YCS 4104)

MODULE 2 : Conditional Statements

1) Write a Pyhton program to find those numbers which are divisible


by 7 & multiples of 5, between 1500 & 2700 (both included).
Program :- print("Numbers between 1500 & 2700 which are divisible
by 7 & multiples of 5 are :-") for a in range(1500,2701): if(a%5==0
and a%7==0): print(a,end=" ")

Output :-

Numbers between 1500 & 2700 which are divisible by 7 & multiples of
5 are :-

1505 1540 1575 1610 1645 1680 1715 1750 1785 1820 1855 1890 1925
1960 1995 2030 2065 2100 2135 2170 2205 2240 2275 2310 2345 2380
2415 2450 2485 2520 2555 2590 2625 2660 2695

Explanation :-

• Firstly, a message relevant to the program is displayed.


• A for loop with index variable ‘a’ is iterated from 1500 to 2700
with an increment of 1.
• Within the body of the loop, it is checked whether the current
values of ‘a’ is divisible by both 5 & 7.

• If the above condition holds true, then the current value of ‘a’ is
displayed along with a whitespace at the end.

2) Write a Python program to convert temperatures to & from Celsius


& Fahrenheit. (Formula :- c/5=f-32/9 where c=temperature in Celsius &
f=temperature in Fahrenheit) Program :- c=float(input("Enter
temperature in Celsius :- "))

f=(9/5*c)+32 print(c,"°C =",f,"°F") f=float(input("Enter

temperature in Fahrenheit :- ")) c=f-32*5/9 print(f,"°F

=",c,"°C")

Output :-

Enter temperature in Celsius :- 37

37.0 °C = 98.60000000000001 °F

Enter temperature in Fahrenheit :- 150

150.0 °F = 132.22222222222223 °C
Explanation :-

• The temperature in Celsius is accepted from the user & stored in


‘c’.
• The value of ‘c’ is multiplied by 9, then divided by 5 and 32 is
added to the result obtained. This is the Fahrenheit equivalent of
the temperature entered in Celsius.
• The temperature in Fahrenheit is then displayed.

• The temperature in Fahrenheit is accepted from the user & stored


in ‘f’.
• 32 is subtracted from the value of ‘f’ and the result obtained is first
multiplied by 5, then it is divided by 9. This is the Celsius
equivalent of the temperature entered in Fahrenheit.
• The temperature in Celsius is then displayed.

3) Write a Python program to guess a number between 1 & 9.


Note :- User is prompted to enter a guess. If the user guesses wrong then
the prompt appears again until the guess is correct, on successful guess,
user will get a “Well guessed!” message, & the program will exit.
Program :- a=0 while(a<1 or a>9):
a=int(input("Enter a guess :- "))
if(a>=1 and a<=9):

print("Well guessed!")

break

Output :-

Enter a guess :- 12

Enter a guess :- 56

Enter a guess :- 0

Enter a guess :- -3

Enter a guess :- 6 Well

guessed!

Explanation :-

• Firstly a variable named ‘a’ is initialized with 0.


• A while loop is iterated with the condition a<1 or a>9.
• Within the body of the loop, a guess is accepted from the user in
the variable ‘a’.
• After that an if statement is executed with the condition a>=1 and
a<=9. If the condition is true, then “Well guessed!” message is
displayed and break statement is executed.
• If the condition is false, then the loop iterates again. This process
continues until the user enters a guess between 1 & 9.

4) Write a Python program to construct the following pattern, using a


nested for loop.

**

***

****

*****

****

***

**

Program :-

r=int(input("Enter the number of rows :- ")) if(r%2==0):


print("Even number of rows")

print("Pattern not possible!!!") else:

r=r//2 for a in
range(1,r+2): for b
in range(a):
print("*",end="")
print() for a in
range(1,r+1): for b
in range(r,a-1,-1):
print("*",end="") print()

Output :-

Enter the number of rows :- 9

**

***

****

*****

****

***
**

Explanation :-

• The number of rows is accepted from the user and stored in


variable ‘r’.

• If the number of rows is even, then pattern is not possible & a


relevant error message is displayed.
• Floor division is performed on ‘r’ and the value of the same is
updated.
• An outer for loop with index variable ‘a’ is iterated from 1 to r+2
with an increment of 1. • An inner for loop with index variable ‘b’
is iterated from 0 to a with an increment of 1.
• For every iteration of the inner loop, a ‘*’ along with a whitespace
is printed.
• A print statement without any argument is also provided within the
body of the outer for loop to shift the control to the next line. •
Another outer for loop with index variable ‘a’ is iterated from 1 to
r+1 with an increment of 1.
• An inner for loop with index variable ‘b’ is iterated from r to a-1
with a decrement of 1.
• For every iteration of the inner loop, a ‘*’ along with a whitespace
is printed.
• A print statement is provided within the body of the outer loop to
shift the control for the program to the next line.
5) Write a Python program that accepts a word from the user

and reverses it. Program :- a=input("Enter a word :- ") s='' for b

in a: s=b+s print("The reversed word is :-") print(s)

Output :-
Enter a word :- rishi The

reversed word is :- ihsir

Explanation :-

• A word is accepted from the user in variable ‘a’.

• A string ‘s’ is initialized with single quotes.


• A for loop with index variable ‘b’ is iterated on string.
• The string ‘s’ is concatenated with the stored in ‘b’ for every
iteration of the loop.
• After the termination of the loop, the resultant string is displayed.

6) Write a Python program to count the number of even and odd


numbers in a series of numbers.
Sample numbers : numbers=(1,2,3,4,5,6,7,8,9) Expected Output

Number of even numbers : 5


Number of odd numbers : 4 Program

:-
a=int(input("Enter the smallest term of the series :- "))

b=int(input("Enter the largest term of the series :- ")) e=o=0

for c in range(a,b+1): if(c%2==0):

e+=1 else: o+=1 print("Number of even


numbers =",e) print("Number

of odd numbers =",o)

Output :-

Enter the smallest term of the series :- 1

Enter the largest term of the series :- 9

Number of even numbers = 4


Number of odd numbers = 5 Explanation

:-

• The smallest & largest terms of the series is accepted from the user
in variables ‘a’ & ‘b’ respectively.
• Two variables ‘e’ & ‘o’ is initialized with 0.
• A for loop with index variable ‘c’ is iterated from a to b+1.
• In every iteration of the for loop, it is checked whether the value of
‘c’ is even or odd.
• If ‘c’ is odd, then the value of ‘o’ is increased by 1 else the value of
‘e’ is increased by 1.
• The values of ‘o’ & ‘e’ are then displayed.

7) Write a Python program that prints each item and its corresponding
type from the following list.

Sample List : datalist=[1452,11.23,1+2j,True,’w3resource’,(0,-1),


[5,12],{“class”:’V’,”section”:’A’}]

Program :-

datalist=[1452,11.23,1+2j,True,'w3resouce',(0,-
1),[5,12],{"class":'V',"Section":'A'}]
for a in datalist: print(a,type(a))

Output :- 1452 <class 'int'>

11.23 <class 'float'>


(1+2j) <class 'complex'> True
<class 'bool'> w3resouce
<class 'str'> (0,

-1) <class 'tuple'>

[5, 12] <class 'list'>

{'class': 'V', 'Section': 'A'} <class 'dict'>

Explanation :-

• A list named “datalist” is created with the same values as given in


the question. • A for loop with index variable ‘a’ is iterated on the
list.
• Every item of the list is displayed along with the type of the item
for every iteration of the for loop.

8) Write a Python program that prints all the number from 0 to 6 except
3 and 6.

Program :- print("Numbers from 0


to 6 are :-") for a in range(7):
if(a==3 or a==6): continue
print(a,end=" ")
Output :-

Numbers from 0 to 6 are :-

01245

Explanation :-

• First, a relevant message is displayed.


• A for loop with index variable ‘a’ is iterated form 0 to 7 with an
increment of 1. • Within the loop, it is checked whether the value
of ‘a’ is 3 or 6.
• If the above condition holds true, then continue statement is
executed and the control returns to the loop.
• The value of ‘a’ is displayed along with a whitespace.

9) Write a Python program to get the Fibonacci Series between 0 and 50.

Program :-

print("Fibonacci Series between 0 and 50 is :-") a,b=0,1

print(a) print(b) while(True): c=a+b if(c>50):

break

print(c) a=b

b=c
Output :-

Fibonacci Series between 0 and 50 is :-

23

13

21

34

Explanation :-

• Two variables ‘a’ & ‘b’ is initialized with 0 & 1 respectively.


• The values of ‘a’ & ‘b’ are displayed as the first 2 terms of the
series.

• A True while loop is iterated. • First the values of ‘a’ & ‘b’ are
added and the result is stored in ‘c’.
• It is now checked whether c>50 or not. If c>50, then break
statement is applied & the loop terminates.
• The value of ‘c’ is displayed.
• The values of ‘a’ & ‘b’ are swapped with the values of ‘b’ & ‘c’
respectively.

10) Write a Python program that iterates the integers from 1 to 50. For
multiples of three print “Fizz” instead of the number and for multiples of
five print “Buzz”. For numbers that are multiples of three and five, print
“FizzBuzz”. Program :-
print("Integers from 1 to 50 are :-")
for a in range(1,51): if(a%5==0 and
a%3==0):
print("FizzBuzz") elif(a%5==0 and
a%3!=0): print("Buzz") elif(a
%5!=0 and a%3==0):
print("Fizz") else:

print(a)

Output :- Integers from

1 to 50 are :- 1

Fizz
4

Buzz

Fizz

Fizz

Buzz

11

Fizz

13

14

FizzBuzz

16

17

Fizz

19

Buzz

Fizz
22

23

Fizz

Buzz

26

Fizz

28

29

FizzBuzz

31

32

Fizz

34

Buzz

Fizz

37

38

Fizz
Buzz

41

Fizz

43

44

FizzBuzz

46

47

Fizz

49

Buzz

Explanation :-

• A for loop with index variable ‘a’ is iterated from 1 to 51 with an


increment of 1.
• If the value of ‘a’ is divisible by both 3 & 5, then “FizzBuzz”
message is displayed.
• If the value of ‘a’ is divisible by 3 & not by 5, then “Fizz” message
is displayed.
• If the value of ‘a’ is divisible by 5 & not by 3, then “Buzz”
message is displayed.
• If all the above 3 conditions does not satisfy, then the value of ‘a’
is displayed.

11) Write a Python program that takes two digits m(row) and n(column)
as input and generates a two-dimensional array. The element value in the
i-th row & j-th column of the array should be i*j.

Note :

i=0,1,…,m-1 j=0,1,…,n-

1 Program

:-

m=int(input("Enter the number of rows :- "))


n=int(input("Enter the number of columns :- "))
a=b=[] for i in range(m): b=[] for j in range(n):
b.append(i*j)
a.append(b) print("The

2-D array is :-") print(a)

Output :-

Enter the number of rows :- 3


Enter the number of columns :- 4

The 2-D array is :-

[[0, 0, 0, 0], [0, 1, 2, 3], [0, 2, 4, 6]]

Explanation :-

• The number of rows and columns are accepted from the user in
variables ‘m’ & ‘n’.
• Two blank lists namely, ‘a’ & ‘b’ are also declared.
• An outer for loop is declared from 0 to m with an increment of 1.
• The list ‘b’ is made empty inside the body of this loop.
• An inner for loop is declared from 0 to n.
• Within this loop, the product of ‘i’ & ‘j’ is apended to the list ‘b’.
• Within the body of the outer loop, the list ‘b’ is appended to the list
‘a’.

12) Write a Python program that accepts a sequence of lines (blank line
to terminate) as input and prints the lines as output (all characters in
lowercase). Program :-

a,s=[],'string' while(s!=''):
s=input("Enter a line :- ")
a.append(s.lower()) for b in a:
print(b)

Output :-

Enter a line :- I am a boy.

Enter a line :- Kolkata the city of joy

Enter a line :- i am a boy. kolkata the

city of joy

Explanation :-

• A blank list ‘a’ & a string ‘s’ is initialized at the beginning of the
program.
• A while loop with the condition s!=0 is executed.
• Within the loop, a line is accepted from the user in ‘s’ and
appended into the list ‘a’ after converting them into lowercase. • A
for loop is iterated on list ‘a’. Through this loop, the contents of the
list are displayed.

13) Write a Python program that accepts a sequence of comma separated


4 digit binary numbers as its input. The program will print the numbers
that are divisible by 5 in a comma separated sequence. Program :-
a=input("Enter 4 digit binary numbers :- ") b=a.split(",");
print("Numbers divisible by 5 are :-") for a in b: c=int(a) f=e=0
while(c!=0): d=c%10 e+=d*(2**f) c//=10 f+=1
if(e%5==0):

print(a,end=",")

Output :-

Enter 4 digit binary numbers :- 0100,1010,1100,0101,1110,1111

Numbers divisible by 5 are :-


1010,0101,1111, Explanation

:-

• 4 digit binary numbers with a comma separated sequence is


accepted from the user as a string.
• Using split() function, the numbers are grouped in a list as strings.
• A for loop with index variable ‘a’ is iterated on list ‘b’.
• The string like binary number is converted to a number and stored
in ‘c’.

• Two variables ‘f’ & ‘e’ are initialized with 0.


• Now, using variables ‘f’, ‘e’, ‘d’, the decimal equivalent of the
binary number is obtained. If the decimal equivalent id divisible by
5, then the binary number is displayed.
14) Write a Python program that accepts a string and calculates
the number of digits and letters. Program :- a=input("Enter a
string :- ") d=l=0 for b in a: if(b.isdigit()):

d+=1

if(b.isalpha()):

l+=1
print("Number of letters =",l) print("Number

of digits =",d)

Output :-

Enter a string :- Python 3.2

Number of letters = 6

Number of digits = 2

Explanation :-

• A string is accepted from the user in variable ‘a’.


• Two variables ‘d’ & ‘l’ are initialized with 0.
• A for loop with index variable ‘b’ is iterated on string ‘a’.
• If the character in variable ‘b’ is alphanumeric, the value of ‘l’ is
increased by 1. If the character in variable ‘b’ is digit, the value of
‘d’ is increased by 1.
• After the termination of the for loop, the values of ‘l’ & ‘d’ are
displayed.

15) Write a Python program to check the validity of passwords input by


users.

Validation :-

• At least 1 letter between [a-z] and 1 letter between [A-


Z].
• At least 1 number between [0-9].
• At least 1 character from [$#@].
• Minimum length 6 characters.
• Maximum length 16 characters. Program :-

p=input("Enter a password :- ") if(len(p)<6 or len(p)>16):

print("Not a valid password!!!") else:

l=u=d=s=0 for
a in p: if(a.islower()):

l+=1

elif(a.isupper()):

u+=1
elif(a.isdigit()): d+=1
elif(a=='$' or a=='#' or a=='@'):
s+=1 if(l>0 and u>0 and d>0
and s>0): print("Valid
password")

else:

print("Not a valid password!!!")

Output :-

Enter a password :- Coal@123

Valid password

Explanation :-

• A password is accepted from the user in variable ‘p’ in the form of


a string.
• If the length of the password is less than 6 or greater than 16, then
password is invalid.

• Four variables l, u, d, s are initialized with 0.


• A for loop with index variable ‘a’ is iterated on the password ‘p’.
• If ‘a’ is a lowercase character, then the value of ‘l’ is increased by
1.
• If ‘a’ is an uppercase character, then the value of ‘u’ is increased by
1.
• If ‘a’ is a digit, then the value of ‘d’ is increased by 1.
• If ‘a’ is a $ or @ or #, then the value of ‘s’ is increased by 1.
• If the values of l, u, d, s are more than 0, then the password is valid
else not.

16) Write a Python program to find numbers between 100 and 400 (both
included) where each digit of a number is an even number. The numbers
obtained should be printed in a comma-separated sequence. Program :-
print("Even digit numbers between 100 & 400 are :-") for a in
range(100,401):

c,b=0,a while(b!
=0):
d=b%10 if(d%2==0):

c+=1
b//=10 if(c==3):

print(a,end=",")

Output :-

Even digit numbers between 100 & 400 are :-


200,202,204,206,208,220,222,224,226,228,240,242,244,246,248,260,26
2,264,266,268,280,282,284,286,288,400,
Explanation :-

• A for loop with index variable ‘a’ is iterated from 100 to 400 with
an increment of 1.
• Within the loop, the variable ‘c’ is initialized with 0 & the value of
‘a’ is copied on ‘b’.
• A while loop is executed with terminating condition b!=0. Within
the loop, the last digit of ‘b’ is extracted and if the last digit is
even, the value of ‘c’ is increased by 1. Floor division is performed
on ‘b’ for every iteration of the while loop.
• If the value of ‘c’ is 3, then the number ‘a’ is displayed followed by
a comma(,).

17) Write a Python program to check whether an alphabet is a vowel


or consonant. Program :- a=input("Enter an alphabet :- ")
if(a.isalpha()): if(a=='a' or a=='e' or a=='i' or a=='o' or a=='u' or
a=='A' or a=='E' or
a=='I' or a=='O' or a=='U'):
print(a,"is a vowel") else:
print(a,"is a

consonant")
Output :-
Enter an alphabet :- r
r is a consonant Enter
an alphabet :- a a is a
vowel

Explanation :-

• An alphabet is accepted from the user in variable ‘a’.


• The isalpha() function checks whether ‘a’ is alphabetic or not.
• If ‘a’ is alphabetic, then it is checked whether ‘a’ is equal to
a,e,i,o,u,A,E,I,O,U. If this condition is true, then the alphabet is a
vowel otherwise it is a consonant.

18) Write a Python program to convert a month name to a number of


days.

Program :- a=input("Enter a month name :- ")

b=a.lower()

if(b=='january' or b=='march' or b=='may' or b=='july' or b=='august' or


b=='october' or b=='december'): print("Number of days = 31")
elif(b=='april' or b=='june' or b=='september' or b=='november'):
print("Number of days = 30") elif(b=='february'):

print("Number of days = 28/29")

Output :-

Enter a month name :- OCTOBER

Number of days = 31

Explanation :-

• A month name is accepted from the user in variable ‘a’.


• The month name is converted to lowercase and stored in ‘b’.
• If the month name is January, March, May, July, August, October,
December then the message “Number of days = 31” is displayed.
• If the month name is April, June, September, November then the
message “Number of days = 30” is displayed.
• If the month name is February, then "Number of days = 28/29"
message is displayed.

19) Write a Python program to sum two integers. However, if the sum is
between 15 & 20 it will return 2.
Program :- a=int(input("Enter first

integer :- ")) b=int(input("Enter second

integer :- ")) if(a+b>14 and a+b<21):

print("The sum is = 2"); else:

print("The sum is =",a+b)

Output :-

Enter first integer :- 7

Enter second integer :- 10

The sum is = 2

Explanation :- • Two integers are accepted from the user in

variables ‘a’ & ‘b’.

• If the sum of both the integers is greater than 14 and less than 21,
then 2 is displayed as the sum. Otherwise, the actual sum is
displayed.

20) Write a Python program that checks whether a string represents an


integer or not. Program :-
a=input("Enter a string :- ") f=0

for b in a:

if(b.isdigit()==False):

f=1
break if(f==0): print("String is

an integer") else: print("String

is not an integer")

Output :-

Enter a string :- 15376

String is an integer

Explanation :-

• A string is accepted from the user in variable ‘a’.


• A variable ‘f’ is initialized with 0.
• A for loop with index variable ‘b’ is iterated on the given string.
• In every iteration, it is checked whether ‘b’ is a digit or not. If ‘b’ is
not a digit, then the value of ‘f’ is changed to 1 and break
statement is executed.
• If ‘f’ is 0, the string is an integer. If ‘f’ is 1, the string is not an
integer.
21) Write a Python program to find the median of three values. Program

:-
a=int(input("Enter first number :- "))

b=int(input("Enter second number :- "))

c=int(input("Enter third number :- ")) d=[] d.append(a)

d.append(b)

d.append(c)

d.sort()

print("The median is =",d[1])

Output :-

Enter first number :- 56

Enter second number :- 23

Enter third number :- 77

The median is = 56

Explanation :-

• Three numbers are accepted from the user and all of them are
added to a list named ‘d’.
• The list ‘d’ is then sorted in ascending order using the sort()
function.
• The element at the 1st index of the list is the median and is then
displayed.

22) Write a Python program to calculate the sum & average of n


integer numbers (input from user). Input 0 to finish. Program :- s=c=0
while(True): a=int(input("Enter an integer number :- ")) if(a==0):
break s+=a c+=1 print("Sum is =",s) print("Average is =",s/c)

Output :-

Enter an integer number :- 5

Enter an integer number :- 3

Enter an integer number :- 7

Enter an integer number :- 4 Enter

an integer number :- 0

Sum is = 19

Average is = 4.75

Explanation :-

• Two variables ‘s’ & ‘c’ are initialized with 0 at the beginning of the
program.
• A true while loop is iterated.

• Within the loop, an integer number is accepted in variable ‘a’. If


the value of ‘a’ is 0, the break statement is executed and the loop
terminates.
• In every iteration of the loop, the present value of ‘s’ added with
the number entered by the user. Also, the value of ‘c’ is increased
by 1.
• After the loop terminates, the Sum ‘s’ & Average (obtained when
‘s’ is divided by ‘c’) is displayed.

23) Write a Python program to create the multiplication table (from 1 to


10) of a number. Program :-

a=int(input("Enter a number :- ")) print("The

multiplication table of",a,"is :-") for b in

range(1,11): print(a,"*",b,"=",a*b)

Output :-

Enter a number :- 7

The multiplication table of 7 is :-

7*1=7
7 * 2 = 14

7 * 3 = 21

7 * 4 = 28

7 * 5 = 35

7 * 6 = 42

7 * 7 = 49

7 * 8 = 56

7 * 9 = 63

7 * 10 = 70

Explanation :-

• A number is accepted from the user in variable ‘a’.


• A for loop with index variable ‘b’ is iterated from 1 to 10 with an
increment of 1.
• Within the loop, the value of ‘a’,’b’, product of ‘a’ & ‘b’ is
displayed along with a ‘*’ & ‘=’ symbol in such a way that it
represents that of a multiplication table.

24) Demonstrate the following Conditional statements in Python with


suitable examples.
(i) if statement

Program :- a=int(input("Enter a

number :- ")) if(a<0): a*=-1

print("Absolute Value =",a)

Output :-

Enter a number :- 5

Absolute Value = 5

Enter a number :- -8
Absolute Value = 8 Explanation

:-

• An integer number is accepted from the user and stored in variable


‘a’.
• Then, it is checked whether the number entered is less than 0 or
not.
• If the number is less than 0, the number is multiplied by -1.

• The current value of ‘a’ is the absolute value of the given number.
• The absolute value of the number is then displayed.

(ii) if else statement Program


:-

a=int(input("Enter a number :- ")) if(a%2==0):

print("Even Number") else:


print("Odd Number") Output

:-

Enter a number :- 5

Odd Number

Enter a number :- 8

Even Number

Explanation :-

• An integer number is accepted from the user and stored in variable


‘a’.
• It is checked whether the given number is divisible by 2 or not.
• If the number is divisible by 2, then the message “Even Number”
is displayed.

• If the number is not divisible by 2, then the message “Odd


Number” is displayed.
25) Demonstrate the following Iterative statements in Python with
suitable examples.
(i) while loop Program

:-

a=int(input("Enter a number :- ")) b=1 print("All the natural

numbers from 1 to",a,"are :-") while(b<=a):

print(b)

b+=1

Output :- Enter a

number :- 5

All the natural numbers from 1 to 5 are :-

2 3

Explanation :-

• An integer number is accepted from the user & stored in variable


‘a’. • A variable ‘b’ is initialized
with 1.
• A while loop is iterated whose body will execute only when the
value of b<=a.
• Within the body of the while loop, the current value of ‘b’ is
displayed & the value of ‘b’ is increased by 1.

(ii) for loop Program

:-

a=int(input("Enter a number :- ")) print("All the


natural numbers from 1 to",a,"are :-") for

b in range(1,a+1):

print(b)

Output :-

Enter a number :- 6

All the natural numbers from 1 to 6 are :-

23

6
Explanation :-

• A integer number is accepted from the user & stored in variable


‘a’.
• A relevant message about displaying the natural numbers upto the
value of ‘a’ is displayed.
• A for loop with index variable ‘b’ is treated from 1 to a+1. The
loop has an increment of 1 as its updating condition. • Within the
body of the for loop, the current value of ‘b’ is displayed.

26) Demonstrate the following control transfer statements in Python


with suitable examples.
(i) break Program

:-
a=int(input("Enter a number :- ")) print("Natural nos.

from 1 to",a,"are :-") for b in range(1,a+1):

print(b)

if(b==5): break

Output :-

Enter a number :- 7
Natural nos. from 1 to 7 are :-

Explanation :- • An integer number is accepted from the user in

variable ‘a’.

• A for loop is iterated from 1 to a+1 with an increment of 1 as its


updating condition.
• Within the body of the for loop, first the current value of ‘b’ is
displayed & then it is checked whether the value of ‘b’ is equal to
5.
• If the value of b is equal to 5, break statement is applied & the loop
terminates immediately. That’s why, when the input is 7, only
numbers from 1 to 5 are displayed.

(ii) continue Program


:-

a=int(input("Enter a number :- ")) print("All the


odd natural nos. from 1 to",a,"are :-") for b in
range(1,a+1): if(b%2==0): continue
print(b)

Output :-

Enter a number :- 10

All the odd natural nos. from 1 to 10 are :-

Explanation :-

• An integer number is accepted from the user and stored in variable


‘a’.

• A relevant message regarding odd natural nos. is displayed.


• A for loop with index variable ‘b’ is iterated from 1 to a+1. It has
an increment of 1 as its updating condition.
• Within the body of the loop, first it is checked whether the current
value of ‘b’ is divisible by 2 or not. If ‘b’ is divisible by 2, then
continue statement is executed.
• The current value of ‘b’ is displayed.

27) Write Python programs to print the following Patterns :-

A B

A B C

A B C D

A B C D E

Program :- r=int(input("Enter the number


of rows :- ")) print("The pattern is :-") for b
in range
(1,r+1): for c in range(r,b,-1):

print(" ",end="") for

c in range(1,b+1):
print(chr(c+64),end=" ")

print()

Output :-

Enter the number of rows :- 5

The pattern is :-

AB

ABC

ABCD

ABCDE

Explanation :-

• The number of rows is accepted from the user & stored in variable
‘r’.
• A for loop with index variable ‘b’ & with an increment of 1 is
iterated from 1 to r+1.

• Within the above loop, an inner for loop is iterated from ‘r’ to ‘b’
with a decrement of 1.
• In the body of this for loop, a space is printed for each iteration &
it ends in the same line.
• Another inner for loop with index variable ‘c’ is iterated from 1 to
b+1.
• This third loop prints the letters of the required pattern using
ASCII code of the respective letters.

• After the execution of the above 2 inner for loops, print() statement
is executed within the body of the outer for loop to shift the control
to the next line.

28) Write Python programs to print the following Patterns :-

4 3

4 3 2

4 3 2 1

4 3 2 1 0

4 3 2 1

4 3 2

4 3

Program :- r=int(input("Enter odd number of rows :- "))

if(r%2==0): print("Number of rows is even")

print("Pattern not possible!!!") else:


r=r//2 for a in
range(1,r+2): for b
in range(r,a-1,-1):
print(" ",end="")
for c in range(a):
print(r-c,end=" ")
print() for a in
range(r): for b in
range(a+1):
print(" ",end="")
for c in range(r,a,-1):
print(c,end=" ") print()

Output :- Enter odd number of

rows :- 9

43

432

4321

43210
4321

432

43

Explanation :- • An odd number of rows is accepted from the user in

variable ‘r’.

• If the number of rows in even, a relevant error message is


displayed & the pattern will not be displayed.
• Floor Division is performed on ‘r’ & the value of the same is
updated. • An outer for loop with index variable ‘a’ is iterated
from 1 to r+2.
• Within the above for loop, an inner for loop is iterated from r to a1
with a decrement of 1. This loop is for the purpose of printing
whitespaces.
• Another inner for loop is executed from 0 to ‘a’ for the purpose of
printing the numbers.
• A print() statement is also executed within the body of the outer for
loop to shift the control to the next line for every iteration of the
outer for loop
• Another outer for loop with index variable ‘a’ is iterated from 0 to
r.
• An inner for loop is iterated from 0 to a+1 to print the necessary
whitespaces.
• Another inner for loop is also iterated from ‘r’ to ‘a’ to print the
necessary numbers.

• Another print() statement is executed within the body of the outer


for loop to shift the flow of control to the next line for every
iteration of the outer for loop.

29) Write Python programs to print the following Patterns :-

* *

* * * *

* * * * * *

* * * * * * * *

* * * * * * * * * * Program :- r=int(input("Enter the number of


rows :- ")) for a in range(r): for b in range(r,a+1,-1):

print(" ",end="")
for b in range(a+1):
print("*",end=" ") for b
in range(r,a+1,-1):
print(" "*2,end="") for b
in range(a+1):
print("*",end=" ")
print()

Output :-

Enter the number of rows :- 5

* *

* * **

* ** ***

* *** ****

* *********

Explanation :-

• The number of rows is accepted from the user in variable ‘r’.


• An outer for loop with index variable ‘a’ is iterated from 0 to r with
an increment of 1.
• The first inner for loop is executed from r to a+1 with a decrement
of 1. This loop is used for printing whitespaces.
• The second inner for loop is executed from 0 to a+1 with an
increment of 1. This loop is used to print * in a triangular pattern. •
The third inner for loop is executed from ‘r’ to a+1 with a
decrement of 1. This loop prints the whitespaces in a V shaped
pattern necessary to print the required pattern.
• The fourth inner for loop is executed from 0 to a+1 with an
increment of 1. This loop also prints * in a triangular pattern. •
The index variables of all the inner for loops’ is ‘b’.
• The print() statement is executed in the body of the outer for loop
to shift the control to the next line for every iteration of the outer
for loop.

30) Write Python programs to print the following Patterns :-

34

234

1234

01234

1234

234

34

4
Program :- r=int(input("Enter an odd number of rows :- "))

if(r%2==0):

print("Even number of rows")


print("Pattern not possible!!!") else:
r=r//2 for a in range(1,r+2):
c=r+1-a for
b in range(a):
print(c,end="")
c+=1 print() for
a in range(1,r+1):
c=a for b in range(r,a-1,-

1):

print(c,end="")

c+=1 print()

Output :-

Enter an odd number of rows :- 7

23

123
0123

123

23

Explanation :-

• The number of rows is accepted from the user in variable ‘r’.


• If the no. of rows is even, then the required pattern is not possible.
• Floor division is performed on ‘r’ by 2 & the value of the same is
updated.

• An outer for loop is iterated from 1 to r+2. Within it, the value of
‘c’ is set as r+1-a.
• An inner for loop prints the value of ‘c’ & ‘c’ is increased by 1 in
every iteration.
• A print() statement within the body of the outer loop shifts the
control to the next line.
• Another outer for loop is iterated from 1 to r+1. The value of ‘a’ is
copied onto ‘c’. • The value of ‘c’ is printed within an inner for
loop which iterates from r to a-1. • The value of ‘c’ is increased by
1 for each & every iteration of the inner for loop.
• A print() statement within the body of the outer loop shifts the
control to the next line.

You might also like