We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 67
GNiT
thon Programming Lab (MCA20-191
NAME: ARKA DAS
REGISTRATION NO.: — 211430571010043
COLLEGE ROLLNO.: — 504121011005
STREAM: MCA
SEMESTER: rARKA DAS ROLL NO. : 504121011005
Sl.No. | jestion
[ I
[2 | Create a list of 5 items 1,2,3,4,5. 5
2. implement a queue and remove first 2 items. &
3. Create a ist of 5 tems 1,2,3,4,5 implementa stack and remove fist2 | 7
items.
4 ‘Create @ paragraph like ths 3
am _, my hobby is __,| don't have__, | lke to
| have__books, Today's weather is_,/ have walked _miles to reach
the college.
AND FILLIN THE BLANKS WITH USER CHOICE.
[5 [Wit a program to print the ASC value ofa character. 3
[Write a program to swap two numbers using a temporary variable. | 10
LA ‘Write a program to swap two numbers without using a temporary a
| variable.
8. Momentum is calculated as, e=mc*2, where m is the mass of the object | 12
and cis its velocity. Write a program that accepts an object's mass (in
kilograms) and velocity (in meters per second) and displays its
momentum.
To develop an automatic systern that accepts the marks of student | 13,
and generates his/ her grade.
Write a program to enter any character, IFthe entered characterisin | 14
lowercase then convert it into uppercase and if it is an uppercase
character, then convert itinto lowercase.
it ‘Write a program to determine whether the character enteredisa vowel | 15
| | ornot.
jm Write a program to find whether the given number san Armstrong | 36
number or not
[3,T Write a program to find the use given number is odd or even. 7 |
a ‘Write a program find the use given nammber isa pros wuniber oF nob_[ 16 |
15. Write 2 program to count the ne. of vowels present inthe usergiven | 29
string.
i ti 20 |
v7. Write a program that uses docstrings and variable-length arguments to_| 21
add the values passed to the function.
i [Write a program to sum the series 2/21 + 4/21 + 27/31 z
Os. [Write @ program to swap two numbers using function, 23 |
20. Star Pattern : 28
a ‘Star Pattern 5ARKA DAS ROLL NO. : 504121011005
7
23
456
78910
1112131415
‘Character Pattern @
A
88
cee
ooo
eee
Print Fist 10 natural numbers using wile toop.
Print the following pattern,
32345,
1234
123
ity
$/8
1
‘Gilculate the sum ofall numbers from 1 to a given number.
table of ‘number,
B)S)
Display numbers from a list using loo
‘Count the total number of digits in a number.
Print the following pattern
°
n
22
3333
nana
Print list in reverse order using aloop. 3%
Display numbers from -10 to -1 using for loop. 3
Use else block to display a message “Done” after successful execution of
for loop.
z ‘Write a program to display all prime numbers within a range:
Display Fibonacci series up to 10 terms.
Find the factorial of a given number.
Reverse a given integer number.
Use a loop to display elements from a given Ist present at odd index | 43
positions.
‘Calculate the cube ofall numbers from 1 to agiven number. a
Find the sum of the series upto n terms: 45
24224222+2222+22222¢....4n terms
Print the following pattern %
elsis
alee
Behe)
BIS)
Ie
slelsle
sis
eB)ARKA DAS ROLL NO. : 504121011005
ae Python program to find square root of a given number. aT }
[aa Python program to check Buzz Number. 8
Tas. Python program to find HEF or GCD. 0
46. | Python program to find LCM 50 |
47.____| Write a program to sort character ina word in alphabetic order 1
[48 T Python program to count the number of each vowel in a word 32
43. ‘Write a program in python to check whether a Number is palindrome or | 53
ot.
30, Write a program to determine the Pythagorean theorem between three | 54
points.
on Find the number of words and character present in the user given 5
statement.
[52.1 Write a program to validate password entered by the user 36,57
33. Write a function that accepts a number n as input and returns he 38
average of numbers from 1to n.
34 ‘Write 2 menu driven program using functions to perform calculator | 59
‘operations such as adding, subtracting, multiplying, and dividing two
integers.
cs ‘Write 2 menu driven program using Objects to perform calculator Cy
‘operations such as adding, subtracting, multiplying, and dividing two
integers.
35 ‘Write a oop of person dass. Person will have name age and there will | 61, 62
bea display method which will show name and age.
Person subclasses:
1 teacher
2. student
7 ‘Write a program fo make a class take number variable andintalze | €3
with the help ofan object which belangs to anather class,
38 ‘Write a program to create a nested class and print anything to show the | 64
access to outer class from the inner cass using their corresponding
objects.
(5a, ‘Write a program to overicad the + - Operator of any Class. S
60. Write 8 program to create any text fle and give some user input. co
Display the no. of letters and words present in the file.
a ‘Write a program to utilize Try, catch, and finally block to handle division | €7
by zero and file not found error.
iARKA DAS ROLL NO. : 504121011005
Question 1: Create a list of 5 items 1,2,3,4,5,ARKA DAS ROLL NO. : 504121011005
Question 2: implement a queue and remove first 2 items.
=[)
= int(input("Enter The Limit: *))
print("Enter {} items in the queue". (1im))
for i in range(@, lim)
=60
print("Grade: A")
elif and perc>=50
print("Grade: 8")
elif @ and
print("Grade: ¢")
else:
print("Grade: D")
Output:ARKA DAS ROLL NO. : 504121011005
Question 10: Write a program to enter any character. If the entered character is in lowercase then
convert it into uppercase and f itis an uppercase character, then convert it into lowercase.
if asc>=97 and asce=42;
The Entered Character is in Lowercase! “)
pri converting into Uppercase )
elif asco965 and asce=90:
print("The Entered Character is in Uppercase! “)
print(“After converting inte Lowercase: “,x)
print(“Entered character does not support case change!
l=)ARKA DAS ROLL NO. : 504121011005
Question 11: Write a program to determine whether the character entered is a vowel or not.
Solution:ARKA DAS ROLL NO. : 504121011005
‘Question 12: Write a program to find whether the given number is an Armstrong number or not.
Hint: n Armstrong number of three digits isan integer such that the sum of the cubes of its digits Is
‘equal to the number itself. For example, 371 is an Armstrong number since 3*3 + 7**3 + 1**3 = 371
‘Solution:
int (input ("Enter A number: “))
print ("ARMSTRONG NUMBER!"
else:
print("Not An ARMSTRONG NUMBER!")
Output:
16ARKA DAS ROLL NO. : 504121011005
Question 13: Write a program to find the use given number is odd or even.
int “Enter Any Numbe
if ak2==0
print ("odd NumbeARKA DAS ROLL NO. : 504121011005
Question 14: Write a program to find the use given number is a prime number or not.
Solution:
=int(input (“Enter A Number to Check Prime Number or not: "))
“0
for i ine
if ax:
if cnt==1
print("Prime Number! !")
else
print ("Composite Number! !")
Output:ARKA DAS ROLL NO. : 504121011005
Question 15: Write a program to count the no. of vowels present in the user given string.
print(‘Total Number of Vowels Present in the String: *,cnt)
19°ARKA DAS ROLL NO. : 504121011005
Question 16: Write a program to find the user given string is palindrome or not.
Solution:
=input (“Enter A String: ")
= 0
r-2]
if
int("Palidrome String! !")
print(*Non Palindrome String! !")
Output:ARKA DAS ROLL NO. : 504121011005
‘Question 17; Write a program that uses docstrings and variable-length arguments to add the values,
passed to the function,
Solution:
def add(s)
0
for i in
return
=int(input(“Enter The Limit: “))
print("enter {) Numbers in the List". (n))
in range(@,n)
(input ("Enter Value: "))
(x)
print(a c_)
wadd(1i)
print("The Sum is: “,ss)
Output:
21ARKA DAS ROLL NO. : 504121011005
Question 18: Write a program to sum the series: 1/11 + 4/2! + 27/31 +...
Solution:
n range(1,n+1)
in range(1,i+1)ARKA DAS ROLL NO, : 504121011005
Question 19: Write a program to swap two numbers using function.ARKA DAS ROLL NO. : 504121011005
Question 20:
Star Pattern,ARKA DAS ROLL NO. : 504121011005ARKA DAS ROLL NO. : 504121011005
‘Question 22:
Star Pattern
for 1)
for k in range(i,n)
rint(" ",end=
for j in ranj
arint ("* )
Output:ARKA DAS ROLL NO. : 504121011005
‘Question 23:
Number Pattern:
ET
23
456
78910
121213 1415
Solution:ARKA DAS
‘Question 24:
Character Pattern:
A
BB
ccc
poop
EEEEE
Solution:
for j in range(1
int (
ROLL NO.
). : §04121011005ARKA DAS ROLL NO. : 504121011005
‘Question 25: Print First 10 natural numbers using while loop.
Soluth
64
for i in range(1,6)
29ARKA DAS ROLL NO. : 504121011005ARKA DAS ROLL NO. : 504121011005
Question 27: Calculate the sum of all numbers from 1 to a given number.ARKA DAS ROLL NO, : 504121011005
Question 28: Write a program to print multiplication table of a given number.ARKA DAS ROLL NO. : 504121011005
Question 29: Display numbers from a lst using loop
Solution:
maint(
tie[]
t("Enter Limit of the List: *))
print(*Enter {} Data in the List!! mat (1im))
for { tn range(@,
sinput("Enter Data for list “)
list (
print(*Showing Data From the List Using Loop
for i in range(@, lim)
print('Dsta {} in the list: () (441, Listi[i})ARKA DAS ROLL NO. : 504121011005
‘Question 30: Count the total number of digits in a number
while are
3X10
tel
2-2//10
print(*Tatal no of Sigits of the tunber: "ent
Output:ARKA DAS ROLL NO, : 5044121011005
222
3333
4aaaa
Solution:ARKA DAS ROLL NO. : 504121011005
‘Question 32: Print list in reverse order using a loop
Solution:
(input (‘Enter T mit of the List: '))
pr ter {} Data in the List! (1im))
fo nge(@, Lim)
“enter Data: “)
(x)
print(‘Showing the list in reverse order using a loop: ')
for ange(1in-1,-1,-1)
int(Ist[j],end=", *)ARKA DAS ROLL NO. : 504121011005
‘Question 33: Display numbers from -10 to -1 using for loop
Solution:ARKA DAS ROLL NO. : 504121011005
‘Question 34: Use else block to display a message “Done” after successful execution of for loop
Soluti
38ARKA DAS ROLL NO. : 504121011005
‘Question 35: Write a program to display all prime numbers within a range
Solution:
(input(‘Enter The Limit of
for i in range(3, Limel)
for j in range(1,i)
if ixj==0
Prime Number: {} at(pr,i))ARKA DAS
‘Question 36: Display Fibonacci series up to 10 terms
print(‘** FIBONACCI SERIES UPTO 10 TERMS
ROLL NO.
). : §04121011005
40°ARKA DAS ROLL NO.
‘Question 37: Find the factorial of a given number
Solution:
). : §04421011005
le|
&ARKA DAS ROLL NO. : 504121011005
‘Question 38: Reverse a given integer number
Soluti
put ("En Numbe )
while tempre
=temp%1e
=n*10+
t 118
42ARKA DAS ROLL NO. : 5044121011005
‘Question 39: Use a loop to display elements from a given list present at odd index positions.
for nge(@, Lim)
("Enter Data: “)
A(x)
pr owing the list items at odd pc using a loop: ‘)
for j in range(@,lin,2)
print(*PSITION {}, ITEM: { Gea, 1»)
Output:
43ARKA DAS ROLL NO. : 504121011005
‘Question 40: Calculate the cube of all numbers from 1 to a given number
Soluti
»)
Numbers from 1 t
} (4,2%3))
44ARKA DAS
ROLL NO.
1604121011005
Find the sum of the series up to n terms:
2+224222422224222224 ens $0 tS
‘Solution:
=int(input ("Enter The Limit: "))
=2
a
for i in range(1,a+1)
oxt1042
print("Total sum: “,s)ARKA DAS ROLL NO. : 504121011005
Print the following pattern
rint(‘*',end=" ')
Output:
46ARKA DAS ROLL NO. : 504121011005
‘Question 43: Python program to find square root of a given number.
Soluti
int( Enter The Number To Find The
if ace
47ARKA DAS ROLL NO. : 504121011005
‘Question 44: Python program to check Buzz Number.
t("Enter A number To Check Buzz Number or Not: "))
if or c==0.@
print("Buzz Number")
else
peint("Not A Buzz Numbe
Output:
48ARKA DAS ROLL NO. : 504121011005
Question 45: Python program to find HCF or GCD.
Soluti
sint(input("Enter The Limit: "))
("Enter {} Values! st (Lim) )
nge(@, Lim)
t(‘Enter Value: *))
ange(1, (min(val)+1)
49°ARKA DAS ROLL NO. : 504121011005
‘Question 46: Python program to find LCM
Solution:
Cinput ("Enter The Limit: "))
("Enter {} Values! (1im))
n range(@, lim)
eint(input(‘Enter Value: *))
A(x)
8
for 4 in range(1,(min(val)+1)ARKA DAS ROLL NO. : 504121011005
‘Question 47: Python program to sort character in a word in alphabetic order.
Soluti
=input(
tC
nt( “Output
Output:
51ARKA DAS ROLL NO. : 504121011005
‘Question 48: Python program to count the number of each vowel in a word.
Solution:
for i in range(o,5
for j in range(@,1)
+21
print(‘Total Nunber of Times, {} is present in the string
{+f [il,a,ent))ARKA DAS ROLL NO, : 504121011005
‘Question 49: Write a program in python to check whether @ Number is palindrame or not.
Solution:
aint Enter a Number: '))
“0
while tempre
x10
=n 10+
t 118
wax
print(*Palindrome Number! |‘)
rint( ‘Not A Palindrome Number!!!*)
Output:
53ARKA DAS ROLL NO. : 504121011005
‘Question 50: Write a program to determine the Pythagorean Theorem between three points.
Solution:ARKA DAS ROLL NO. ; 504121011005
‘Question 51: Find the number of words and character present in the user given statement.
Solution:
input(‘Enter Any String: ')
print(‘No. of Words: *,wrd)
print(‘No. of Characters: *,ch)
Output
renvami, Brave +e 1 & ox
Copyright (C) Microsoft Corporation. All rights reserved.
Tey the new cross-platform PowerShell https://aka.ns/pscores
FS D:\PYTHON PROGMS AD NOTES? & C:/Usera/are/Nponta/Local/MicrosoPt/WMiéowsAges/ python’. 30.0n8 “8: /P¥
ter Any String! arifcet rey
No. oF Characters: 21
PE DL\PYTOON PROGRAMS AND NOTES»
we
aARKA DAS
ROLL NO. : 504121011005
‘Question 52: Write a program to validate password entered by the user.
‘Solution:
def passvaliditiy(uname,passw):
dconditions declared here
conditionse[ "first letter af the password should be in uppercase’, “password should not
contain username",
‘should contain atleast 2 special character’, "password length @ characters to 16
characters",
‘password should contain atleast 2 number’, ‘password should not contain any space’]
nocelen( conditions)
Astislist(passw)
staf]
first Letter of the passward should be tn uppercase
4ff passw[@] -supper()==True:
1st2.append(2)
else:
AF passw{@]-tsnuneric():
Ist2.append(5)
‘Ist2-appendt
password should not contain username
‘fF unaze.lower() in passw,lower():
1st2.append(@)
else:
Lst2,append(t)
Ashould contain atteost 2 special character
sco" igeseae
tenpie2
for b to ista:
Afb in 3
tenpi=t
break
else:
tompise
1st2-append( tempt)
password Length @ characters to 16 characters
4ff len(passw)>=8 and len(passw)<=16:
Ist2,append(1)
else:
1st2,append(a)
should contetn a number
dge'0123456789"
for b in Ist:
Af b An
toopiet,
break
else:
‘temp2=0
\s)ARKA DAS ROLL NO. : 504121011005
o nstx))
w « input("Enter User Name: “)
password input("Enter Password: *)
Password Accepted! \n*)
print(“\n Password cen not be accepted! ! \n*)
Output:
ws ‘ Bree +e DB Ax
ebyrtene CC) MerosoFE Corporation, ALL rights reserved.
Try the new crosspatform PowerShell Mttps//aka.e/pcores
75 DMO Mac 0 OFS ers acl Merson 8s “H/F
Enter User Name: ariiect
Inter Pessword fanart feet
Error 2. password should not contain usernane
Error 3; should contain atleast 1 spectal character
Error 4: password should contain atleast a number
Paesuond can not be accepted!ARKA DAS ROLL NO. : 504121011005
‘Question 53: Write a function that accepts a number n as input and returns the average of numbers
from 1ton.
Solutlon:
def avg(n)
6
for i in range(i,n¢1)
aves/
=int (input (‘Enter the Limit: *))
ageeave(a)
print(*Average from 1 to {) 4s: {}") co »
Output:
TERMINAL Drytron + a x
Windows Powershell
Copyright (€) Microsoft Corporation, All rights reserved.
Try the new cross-platform Powershell https://aka.ns/pscores
THON PROGRAMS AND NOTES> & C: /sers/aeJe/AppHAta/Local/McrN
viowsAops /python}. 10.ene “4: /PY
Enter The Linit: 18
Average fron 1 to 18 Ss: 5.5
Ps Oc\PYTnON Poans AND WOTES>
g
&ARKA DAS ROLL NO. : 504121011005
{Question 54: Write a menu driven program using functions to perfarm calculator operations such as
‘adding, subtracting, multiplying, and dividing two integers.
‘Solution:
def add(a,b)
return a+t
def sub(a,b)
return
def mul(a,b)
return
def div(a,b)
return a//b
print("enter 2 No.s")
nt(input(“*Enter ist Num: "))
nt (input ("Enter 2nd Num: “))
print(*1-Addition \n2.Subtraction \n3.Multiplication \n4.Division")
cesint(input(*Enter your choice: “))
tf che
print("Addition result is: ",add(a,b))
elif choices=2
print("Subtraction result is: ",sub(a,b))
elif 3
print(*Multiplication result is: ",mul(a,b))
elif choicensd:
print("Division result is: “,div(a,b))
else:
print(“krong Choice! !")
Output:
MAA Blotnon +» 0
Fs D:\PYTION PROGRAKS MD NOTES» & ¢: /Users/erje/tppsts/Locel/Microsoft/nsowstgpn/pthen).2®.ene "d:/PYT
Fon exocras AnD NOTES/FINaL ASSTCAMENTS/Q54 py
inter 2 Nos
Enter Ist hums 45
tnter and How: 3
1xsastion
2 subtraction
s.mltiplication
‘.oivision
Enter your chotee: 1
Acostson result Se 18
fs D:\PrTIOn PaoGtass m0 woTEs>ARKA DAS ROLL NO. : 5044121011005
‘Question 55: Write a menu driven program using objects to perform calculator operations such as
adding, subtracting, multiplying, and dividing two integers,
‘Solution:
self. unas!
et add( self
turn self.nunieself
det sun(selF
“eturn $86f. nunl-self
det mal (sel
return setf.nuni*setf.
eturn sekf.nuni/ fself.
prim on \n3.Multipticati son)
. ection r ni
Output:
rea Drm +s 8
Copyright (€) MlerosoFt Corporation, All rights reserved
Try the nee cross:platforn PowerShell httes://aka.ns/pscores
fer ast huni a5
Enter and fas 3
Galeulator 1s Accessed
Scnultiplicstion
‘Addition result 8: 38
PS D:\PYTIEN PROGRAMS AND NOTESARKA DAS ROLL NO, : 504121011005
‘Question 56: Write a 00p of a person class. Person will have name age and there will be a display
‘method which will show name and age.
Person subclasses:
1. teacher, (which will show name and age from parent class and its salary and experience in itself)
2. student (which will show name and age from parent class and its course and marks in itself)
‘Solution:
class person:
def
def
_init__(self,a,b):
print(" am a Person! !*)
self.name=a
self.age=b
disp(self):
print("My Name Ts: “,self.name)
print("My Age is: ",self.age)
class teacher(person):
def
def
init_(self,¢,d,x,5):
person, init_(self,c,d)
print("I Am a Teacher!!!")
self .experax
self.salaryes
dispteacher(self):
print("\nAccessing Function Within Teacher Class! !\n")
super().disp()
print( ‘Experience: *,self.exper)
print('Salary: *,self.salary)
class student(person):
def
def
_init_(self.e,f,cr,mr):
person.__init__(self,e,f)
print(*T Am a Student !!!")
self.coursescr
self.marksane
dispstudent (self):
print("\nAccessing Function Within Student Class! !\n")
person.disp(self)
print( ‘Course: *,self.course)
print('Marks: *,setf.marks)
print("\nCalling Object for PARENTclass PERSON: “)
‘objper=person( "PERSON NAME", 100)
[e|
gARKA DAS ROLL NO, : 504121011005
print("\n Calling Object for subclass TACHER: \n")
tches! (‘Teacher Name’ ,35,16, 20000)
t 0
print(*\n Ca 1° subclass STUDENT: \n‘)
Name" ,22,'MCA' ,5@)ARKA DAS ROLL NO. : 504121011005
‘Question 57: Write a program to make a class take a number variable and initialize with the help of an
‘object which belongs to another class.
Solution:
class »
nume1@
class b
def disp(self, ob)
=12008
=3()
0
(ebj1)
print(*Again Printing From Object *)
Output:
63ARKA DAS ROLL NO. : 504121011005
‘Question 58: Write a program to create a nested class and print anything to show the access to outer
class from the inner class using their corresponding objects.
Solution:
class »
class
def disp(self,ob)
print(‘This is display for class b*)
(‘Hello Again! T an From class b!!")
def displ(self,b):
self-n=!
print('This is display for class a: ',self.n)
5 hello! I am from object of class a‘)
[2ARKA DAS ROLL NO. : 504121011005
‘Question 59: Write a program to overload the +,- Operator of any Class,
Soluth
class operatoroverload
def __init_(self,num)
self.val=
def _add_(self,ob
int ("Addition using operatc ading: *,self.val+
def __sub_(self,ob
print("Subtraction using operator overoading: “,self.val- )
~ 10
a 20
65ARKA DAS ROLL NO. : 504121011005
‘Question 60: Write a program to create any text file and give some user input. Display the no. of letters
‘and words present in the file.
abc. txt
input(‘Enter Your
open( ‘abe. txt",
0
[@
4 0
print('No of ( )
print('No of »)
0
Output:
66ARKA DAS ROLL NO. : 504121011005
‘Question 62: Write a program to utilize Try, catch, and finally block to handle division by zero and type
Solution:
try
(input (‘Enter ast number: *)
orint (a/b)
try
print(a/c)
Output:
67