Naincy MOD 2 PYTHON
Naincy MOD 2 PYTHON
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 :-
• If the above condition holds true, then the current value of ‘a’ is
displayed along with a whitespace at the end.
=",c,"°C")
Output :-
37.0 °C = 98.60000000000001 °F
150.0 °F = 132.22222222222223 °C
Explanation :-
print("Well guessed!")
break
Output :-
Enter a guess :- 12
Enter a guess :- 56
Enter a guess :- 0
Enter a guess :- -3
guessed!
Explanation :-
**
***
****
*****
****
***
**
Program :-
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 :-
**
***
****
*****
****
***
**
Explanation :-
Output :-
Enter a word :- rishi The
Explanation :-
:-
a=int(input("Enter the smallest term of the series :- "))
Output :-
:-
• 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.
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))
Explanation :-
8) Write a Python program that prints all the number from 0 to 6 except
3 and 6.
01245
Explanation :-
9) Write a Python program to get the Fibonacci Series between 0 and 50.
Program :-
break
print(c) a=b
b=c
Output :-
23
13
21
34
Explanation :-
• 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)
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 :-
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
:-
Output :-
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 :-
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.
print(a,end=",")
Output :-
:-
d+=1
if(b.isalpha()):
l+=1
print("Number of letters =",l) print("Number
of digits =",d)
Output :-
Number of letters = 6
Number of digits = 2
Explanation :-
Validation :-
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:
Output :-
Valid password
Explanation :-
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 :-
• 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(,).
consonant")
Output :-
Enter an alphabet :- r
r is a consonant Enter
an alphabet :- a a is a
vowel
Explanation :-
b=a.lower()
Output :-
Number of days = 31
Explanation :-
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
Output :-
The sum is = 2
• 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.
for b in a:
if(b.isdigit()==False):
f=1
break if(f==0): print("String is
is not an integer")
Output :-
String is an integer
Explanation :-
:-
a=int(input("Enter first number :- "))
d.append(b)
d.append(c)
d.sort()
Output :-
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.
Output :-
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.
range(1,11): print(a,"*",b,"=",a*b)
Output :-
Enter a number :- 7
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 :-
Program :- a=int(input("Enter a
Output :-
Enter a number :- 5
Absolute Value = 5
Enter a number :- -8
Absolute Value = 8 Explanation
:-
• The current value of ‘a’ is the absolute value of the given number.
• The absolute value of the number is then displayed.
:-
Enter a number :- 5
Odd Number
Enter a number :- 8
Even Number
Explanation :-
:-
print(b)
b+=1
Output :- Enter a
number :- 5
2 3
Explanation :-
:-
b in range(1,a+1):
print(b)
Output :-
Enter a number :- 6
23
6
Explanation :-
:-
a=int(input("Enter a number :- ")) print("Natural nos.
print(b)
if(b==5): break
Output :-
Enter a number :- 7
Natural nos. from 1 to 7 are :-
variable ‘a’.
Output :-
Enter a number :- 10
Explanation :-
A B
A B C
A B C D
A B C D E
c in range(1,b+1):
print(chr(c+64),end=" ")
print()
Output :-
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.
4 3
4 3 2
4 3 2 1
4 3 2 1 0
4 3 2 1
4 3 2
4 3
rows :- 9
43
432
4321
43210
4321
432
43
variable ‘r’.
* *
* * * *
* * * * * *
* * * * * * * *
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 :-
* *
* * **
* ** ***
* *** ****
* *********
Explanation :-
34
234
1234
01234
1234
234
34
4
Program :- r=int(input("Enter an odd number of rows :- "))
if(r%2==0):
1):
print(c,end="")
c+=1 print()
Output :-
23
123
0123
123
23
Explanation :-
• 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.