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

Cs Practical File

The document contains a list of Python programming exercises, each with a brief description of the task to be accomplished. It includes various topics such as input/output operations, mathematical calculations, string manipulations, and data structures. Each exercise is followed by example code and expected output.

Uploaded by

asrajput1268
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)
18 views22 pages

Cs Practical File

The document contains a list of Python programming exercises, each with a brief description of the task to be accomplished. It includes various topics such as input/output operations, mathematical calculations, string manipulations, and data structures. Each exercise is followed by example code and expected output.

Uploaded by

asrajput1268
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/ 22

INDEX

S.No Program Pag Signature


e
No.
1 Write a python program to input a welcome message and display it.
2 Write a python program to input two numbers and display the larger/
smaller number.
3 Write a python program to input three numbers and display the largest/
smallest number.
4 Generate patterns using a nested loop.
5 Write a program to input the value of x and n and print the sum of the
following series:
I) I + x' + x' + ... + x"
2) x - x2/2! + x3/3! - x'/4! + x'/5! - x6/6!
6 Write a program to determine whether a number is a perfect number, an
Armstrong number or a palindrome.
7 Write a program to input a number and check if the number is a prime or
comoosite number.
8 Write a program to display the n terms of a Fibonacci series.
9 Write a python program to compute the greatest common divisor and
least common multiple of two integers.
10 Write a program to count and display the number of vowels, consonants,
uooercase, lowercase characters in the strim!.
11 Find the largest/smallest number in a list/tuple.
12 Input a list of numbers and swap elements at the even location with the
elements at the odd location.
13 Input a list/tuple of elements, search for a given element in the
list/tuple.
14 Create a dictionary with the roll number, name and marks of n students
in a class and display the names of students who have scored marks
above 75.
15 WAP to remove all odd numbers from the given
Index list.
16 WAP to display second largest element of a given
list.
17 WAP to disolay cumulative sum of elements of a l!iven list.
18 WAP to frequencies of all the elements of
a list using a dictionary.
19 WAP in Python to find and display the sum of all the values which are
ending with 3 from a list.
20 WAP to shift the negative number to left and the positive numbers to
right.
21 WAP to swap the content of a list with next value
divisible by 7.
22 WAP to accept values from user and create a
tuple.
23 Write a Python program to input names of 'n'
countries and their capital and currency, store it in a dictionary and
display in tabular form. Also search and display for a particular country.
Ql) WRITE A PYTHON PROGRAM TO INPUT A WELCOME MESSAGE AND
DISPLAY IT.

y=str (input ("enter name:"))

x="welcome"

print (x, y)

OUTPUT:-

..... -. ...
"' -..
Python 3.8.3 (t.tg1/,J.8.3:6!llc832, lily 13 2020, 22:31:02) (MSC v.1124 64 bit (All
)64)) OD ruJ2
?ypt "btlp", •copyright", 'crtd!tl" or •u.,..,. () • for aoro illfonutioo.
»>
- - - - - - lttfflllT: C:/Pythooll/dcb.py - - - - - -
tnttr IliM:Anili ihltticbuy1
iMlccal!: Anika Bhattichuya
»> I

Q2) WRITE A PYTHON PROGRAM TO INPUT TWO NUMBERS AND DISPLAY THE
LARGER/SMALLER NUMBER.

CODE:-

x=int (input ("enter first number:"))

y=int (input ("enter second number:"))

if X>y:

print (x, "is greater than ", y)

else:

print (y, "is greater than", x)


OUTPUT:-

Python 3,8.3 (tagz/v3,8,J;6t8c832, May 13 2020, 22:37:02) (MSC v •.


Type "help", "copyright", "credits" or "licen.se()" for more inforr

··•-•=••-··---· RESTART; C;/PythonJO/PRACTlCAL XlC R.py --~
enter fir5t nuaber: 10
ontor 5ocond number: 100
100 is greater than 10
>» I

Q3) WRITE A PYTHON PROGRAM TO INPUT THREE NUMBERS AND DISPLAY


THE LARGEST/SMALLEST NUMBER.

CODE:-

y=int (input ("enter the number"))

l=list ()

for i in range(y):

x=int (input ("enter number:"))

l.append(x)

print(I)

I.sort()

print ("largest number:", max(I))

print ("smallest number:", min(I))

OUTPUT:-
... ,.. ,... ______
:CJ :l;~fn ~-=i.~ Jtag:s/vJ.o.J:6t8e8J2, May 13 2020, 22:37:02) [MSC
0 3
~-:- Type "help", .. copy1.·ight.", •credi ts 00 or "license<) .. tor 11\ore ir
< >>>
RESTART: C:/Pyt.honl8/q3.py - - - - -
enter the number ?
Qntg,r numOOr:23
enter number: 45
enter number: 6?
enter nur!'.ber: 94
on tor numbor: 12
ontor nurnbor: 68
enter nmr.bor: 90
(23, 45, 67, 9!1, l2, 68, 901
l.;ir:qost nurnbor: 94
~ma11ont nu111.bor: l2
»>

Q4) GENERATE PATTERNS USING A NESTED LOOP.

string="#"

pattern=''

for i in range (5):

pattern+=string

print(pattern)

OUTPUT:-

»>
------------------------- RESTART: C:/Python
t
u
m
HH
HfH
»> I

QS) WRITE A PROGRAM TO INPUT THE VALUE OF X AND N AND PRINT THE
SUM OF THE FOLLOWING SERIES:

x=int (input ("enter a number:"))

n=int (input ("enter the maximum power:"))


s=O
for i in range (0, n+l):
s=x**i
print(s)
m=(x* *(n+1)-1)/(x-1)

print (m)
OUTPUT:-

= = = = = = FlSTART: C:Jfytnonl8/qi.py = = = =
enter a nu:rlierl
enter the 1L1Xi~111 po~r4
1
3
9
27
81
111.0
:» I

(ii) x-x2/2! + x3/3! - x4/4! + xS/5!-xA/6!

import math
a= int(input("Enter value of a of series:"))
b = int(input("Enter value of b of series:"))
C =0
for i in range(l,b+l):
if i % 2 == 0:
c -= ((a**i)/math.factorial(i))
else:
c += ((a**i)/math.factorial(i))
print("The sum is", c)

OUTPUT;-
Shell
Python 3.7.9 (bundled)
>>> %Run python.py
Enter value of a of series:4
Enter value of b of series:5
The sum is 4.533333333333333
>>>

Q6) WRITE A PROGRAM TO DETERMINE WHETHER A NUMBER IS THE


PERFECT NUMBER, AN ARMSTRONG NUMBER OR PALINDROME.

def palindroae(n):
te11p =- n
rev • 0
while n > 0:
dig • n ll 10
rev = rev • 18 + di&
n • n II 10
if te■p == rev:
prlnt(tomp, "The numbtr Is a pallndr..,.!")
else:
prlnt(tomp, "The number lsn"t a pal1ndroae!")

def anntrong(n):
count = 0
teap • n
while te■p > 0:
digit • temp ll 10
count ♦= digit ** 3
t•"'P //• 10
if n =-• count:
print(n, "is an Armstrong number")
else:
print(n, "is not an Armstrong number•)
def perfect(n):
count = 0
for i in range(l, n):
if n :I: i == 0:
count = count + i
if count == n:
print(n, "The number is a Perfect number!")
else:
print(n, "The number is not a Perfect number!")

if _name '_main_':
n = int(input("Enter number:"))
palindr011e(n)
armstrong(n)
perfect(n)

OUTPUT:-

>>> %Run python.py


Enter number:1221
1221 The number is a palindrome!
1221 is not an Armstrong number
1221 The number is not a Perfect number!
>>> %Run python.py
Enter number:496
496 The number isn't a palindrome!
496 is not an Armstronq number
496 The number is a Perfect number!

Q7) WRITE A PROGRAM TO INPUT A NUMBER AND CHECK IF THE NUMBER IS


A PRIME NUMBER OR COMPOSITE NUMBER.

CODE:-

x=int (input ("enter any number:"))


for i in range (2, x):

ifx%i==O:

print ("it is a composite number")

break

elif x==l:

print ("it is neither prime number nor a composite number")

elif x==O:

print ("it is neither prime number nor a composite number")

else:

print ("it is a prime number")

OUTPUT:-

enter any number4


it is a composite number
»>
enter any number2
it is a prime number
»>
enter any number:56
it is a composite number
»>

QB) WRITE A PROGRAM TO DISPLAY THEN TERMS OF A FIBONACCI SERIES.

CODE:-

x=int (input ("enter any number:"))


n=int (input ("enter limit:"))

a=O

b=l
print(a)

print(b)

c=O

while c<=n-b:

c=a+b

print(c)

a=b

b=c

OUTPUT:-
- - - ru:,JJMI, <.,1r1c11u11Jo/yJ.py - - - - - - - - - -
enter any number: 6
enter limit: 6
0

1
2
3
5

Q9) WRITE A PROGRAM TO COMPUTE THE GREATEST COMMON DIVISOR
AND LEAST COMMON MULTIPLE OF TWO INTEGERS.

a=int (input ("enter 1st number:"))

b=int (input ("enter 2nd number:"))


import math

print ("the gcd is:", math.gcd (a, bl)

if(a>b):

minl=a

else:

minl=b

while (1):

if min1%a==O and min1%b==O:

print ("the 1cm is:", minl)

break

minl=minl+l

OUTPUT:-

>>>

enter 1st nurnber:4


enter 2nd nurnber:5
the gcd is: 1
the 1cm is: 20
>>>
enter 1st nurnber:7
enter 2nd nurnber:8
the gcd is: 1
the 1cm is: 56
>>>_I_

Ql0) WRITE A PROGRAM TO COUNT AND DISPLAY THE NUMBER OF VOWELS,


CONSONANTS, UPPERCASE, LOWERCASE CHARACTERS IN THE STRING.

CODE: -

x=str(input("Enter a string:"))

vowel=uppercase=lowercase=constant=O
l=len(x)

for i in x:

if(i=='a',i=='e 1,i=='i 1,i=='o',i=='u'):

vowel+=l

if i.isupper():

uppercase+=l

print ("number of vowels", vowel)

print ("number of constant",1-vowel)

print ("number of uppercase", uppercase)

print ("number of lowercase",1-uppercase)

OUTPUT:-

fik (cl,t ,_ P"l>R Opt>ort1 Wftdo# Hd,p

x;str(input("Enter .. string:"))
vowel;uppercase;lowercase;constant;O
lalen(x)
tcr i n x:
1f(i--'a' or i --'e' or i - ' i ' o~ i--'o' er i--'u'):
vowel+;l
if i. is upper():
uppercase+;l
print("numbcr of vowol", vowel)
print("numbet o( t:on~Ldnl",1-vowel)
print("numbor of upporc.lso",upporcase)
print("nwnlJeL o( lowtHt:dse",1-uppercase)

Q11) FIND THE LARGEST/SMALLEST NUMBER IN A LIST/TUPLE.

CODE:-

x=eval (input ("enter the list of number to be computed:"))

x.sort ()
print ("largest number:", max(x))

print ("smallest number:", min(x))

OUTPUT:-

===================== REST;
enter the list of number to be computed: [214,354,346,56]
largest number: 354
smallest number: 56

Q12) INPUT A LIST OF NUMBERS AND SWAP ELEMENTS AT THE EVEN
LOCATION WITH THE ELEMENTS AT THE ODD LOCATION.

l=eval (input ("list of numbers:"))

for i in range (0, len{l)-2,2):

l[i+l], l[i)=l[i), l[i+l)

print(I)

OUTPUT:-

...Python
,.-~°""""" -
3.8.3 (tag:,/v3.8.3:6f8c832, May 13 2020, 22:JJ:02) (MSC v.1924 f
Type '"help", "copyright", •credit:,• or "licen!le C) • tor more in!or:natior.

-------------------------- RESTART: C:/Python38/st:r .py ---------------·
Lbt ct number.5 [232,345,564,JS,43]
(345, 232, 35, 564, 43]
>» I

Q13) INPUT A LIST/TUPLE OF ELEMENTS, SEARCH FOR A GIVEN ELEMENT IN


THE LIST/TUPLE.
f,.c ·- Jc,,o,« -.... °"""'"~-=~""'~----------------
x-eval (input ( "Enter a li!lt: ") )
y-oval (input. ("f~ntor element to bo round in tho • i5t"))
.:_(. 't l.l X:
print("y i~ in the p-::i:'5ition",x.index(y), "in t'lR list"1>
,_L,c:
print ( "element not tound")

OUTPUT:-

Python 3.8.3 (tag~/v3.8.3:6C8c832, May 13 2020, 22:37:02) (M


Typo '"hol.p'", ••copyright"', "crodit!:I .. o r "'licont,o()" tor rnoro

- - - - - - - - - - - - - - - - - - - - - - RESTART: C: /Python38/au::signroont. PY - -
En t o r a l.i.ot:134,45,34~,64,56~,3431
Entt!r olcmont to bo .round in tho li.!tt 34
y L~ in the po.oit.ion O in tt,,,. .Li:,;t
>>> I

Q14) CREATE A DICTIONARY WITH THE ROLL NUMBER, NAME AND MARKS OF
N STUDENTS IN A CLASS AND DISPLAY THE NAMES OF STUDENTS WHO HAVE
SCORED MARKS ABOVE 75.

CODE:-

x=int (input ("enter the total no. of students:"))

result={}

for i in range (x):

roll number=int (input ("enter roll number:"))

name=str (input ("enter the name:"))

marks=int (input ("enter marks:"))

result[roll number]= [name, marks]

for marks in result:

if result [marks)[1)>75:
print ('students who scored above 75:', result[marks][OI)

OUTPUT:-

0."1' o....,.... w,..,._ ..._


'M 1611 s,,..

Python 3.8.3 (tags/v3.8.3:6f8c832, M.ay 13 2020, 22:37:02)


0154)} on win32
rype "help" 1 "copyright", "credits" or "1icenaeC) 11
for mo
>>>
- - - - - - = - - - - - - - - - - - = REST.ART: C:\Python38\jhd5fjhlc5.py
enter the total no. of students:3
enter roll nwaber:25
enter the name:DI:SHA
enter marlts:36
enter roll number:34
enter the nam.e:JANV'I
enter m.arks:79
enter rol.l number:27
enter the name:ASHMJ:TA
enter m.arlcs:46
student• who scored above 75: JANVI
>>>

QlS) WAP TO REMOVE All ODD NUMBERS FROM THE GIVEN INDEX.

l=eval (input ("enter list of numbers:"))

for i in I:

if (i%2! =O):

1.remove(i)

print ("list after removal of odd numbers is",I)

OUTPUT:-
====== B!START: C:/Python38/buiefr.py =====
enter list of numbers:[24,46,57,68,68,68,46,325,582]
list after removal of odd numbers is [24, 46, 68 1 68 1 68 1 46, 582]

Q16) WAP TO DISPLAY SECOND LARGEST ELEMENT OF A GIVEN LIST.

CODE:-

l=eval (input ("enter list of numbers:"))

l.sort(reverse=True)

print(I)

print (I [1])

OUTPUT:-
Enter a 11st of numbers [ 34,546,34434,656,345,)
[34434, 656, 546, 345, 34)
656
>>>

Q17) WAP TO DISPLAY CUMULATIVE SUM OF ELEMENTS OF A GIVEN LIST.

Code:-
imoor numpy a, np
x=eval(input("Enter a list"))
arr=np.array(x)
arrl=np.cumsum(arr)
print(arrl)

Output:

Enter a list [23,5,54,57)


[ 23 28 82 139)
>>>

Q18) WAP TO FREQUENCIES ALL THE ELEMENTS OF A LIST USING A


DICTIONARY.

x-eval(input("Enter a list"))
freq={ J
tnr item in x:
if(item in freq):
freq[item]+=l
else:
freq[item]-1
for key, value in freq.items():
print("%d : %d"%(key,value))

output;-
Enter a list [23,53,5467,6743,23,546,53,90,67,90]
23 : 2
SJ : 2
54 67 : 1
6743 : l
546: 1
90 : 2
67 : 1

Q19) WAP IN PYTHON TO FIND AND DISPLAY THE SUM OF ALL THE VALUES
WHICH ARE ENDING WITH 3 FROM A LIST.

1st• eval(input("Enter elements of a list: "))

S a 0

for a in 1st:
if int(a)ll:10aa3:
s +• a
print {"Sum of numbers ending with 3 in the list",s)

Output:

>>> %Run python.py


Enter elements of a list : [13,34,33,45,93)
Sum of numbers ending with 3 in the list 139

Q20) WAP TO SHIFT THE NEGATIVE NUMBER TO LEFT AND THE POSITIVE
NUMBER TO RIGHT.
l•eval(input( •enter eleinnts of • lis't: "))

n■ len(l)

print("Oricinal list:"~l)

for i in rance(n-1):

for j in range(n-i-1):

if 1 I i J ,0:

1 I i J. 1 I i + 1J • 1 I i + 11, 1 I i J

print("After shiftinc list is:", l)

Output:-

>>> ¾Run python.py


Enter elements of a. list: (-23, 54, 43, -31, -1, 21)
OriQina.l list: t-23, S4, 43, -31, -1, 21]
After shifting list is: (S4, 43,. 21, -1, -31, -23)

Q21) WAP TO SWAP THE CONTENT OF A LIST WITH NEXT VALUE DIVISIBLE BY
7.

num=eval(input("f.nter .:, ... i:i;t"))


num. reverse ()
tnr i n rangc{len(num)-1):
it num[iJ%7--0:
num[ i-1 J, num[i J~num(i J, num(i-11
num. reverse()
print (num)

Output:•

Enter a list[3,21,56,43,63,28)
[28, 43, 21, 56, 3, 63)
»>
Q22) WAP TO ACCEPT VALUES FROM USER AND CREATE A TUPLE.

t~tuple ()
n-int.(inp,1t("~:nt.<'r n11mb<"r nlr-n,.nt:; lo t>0. lillec1:"))
~vL i ... u rd.nge (n) :
a:ci.nput. ("1:-:nrC'r nntthf'r: ")
t•t+ (a,)
prlnt("cutpul l•")
print (tl

Output:-

Enter number of clements to be fillcd:5


Enter number:45
Enter number:54
Enter number:2
Enter number:34
Enter number:78
output is
('45', '54', '2', '34', '78')
Q23) WRITE A PROGRAM TO INPUT NAMES OF 'N' COUNTRIES AND THEIR
CAPITAL AND CURRENCY, STORE IT IN A DICTIONARY AND DISPLAY IN
TABULAR FORM. ALSO SEARCH AND DISPLAY FOR A PARTICULAR COUNTRY.

CODE:-

n = int(input("Enter the number of countries to be be ent•

print()

cnt_dict = diet()
for i in range(n):

x = input("Enter the country name: ")

y = input("Enter the capital: ")

z = input("Enter the currency: ")


print()

cnt_dict[x] = y, z

print()

print("This is your given dictionary: ")

print("Country", "\t\t", "Capital", "\t\t'", "Currency")

for i in cnt_dict:

print(i, "\t\t\t", cnt_dict(i][0], "\t\t\t", cnt_dict[i][l])

print()

re • input("Enter the country whose details you'd like view: ")

if re in cnt_dict:

print(rc, ": ", cnt_dict(rc])

else:

print{"No such country in the given dictionary.")


Output: -

Enter the number or countries to be be entered: 2

Enter the country name: India


Enter the c.apital: Ni!W ~lhi
Enter the currency: Rupee

Enter the country name: United States


Enter the capital: Washington D.C
Enter the currency: Dollar

This is your given dictionary:


country capital currency
India New Delhi Rupee
Oni ted States Washington O.C Dollar

Ent~c- the country who.,e: details you'd like view: tndia


rndia : ('New Delhi', 'Rupee')

»>

You might also like