ARMY PUBLIC SCHOOL
DHAULA KUAN
Session 2020-21
PYTHON
PROGRAMMING FILE
NAME :UTKRISHT KUMAR
CLASS : XII-F
ROLL NO :39
LIST OF PRACTICALS :
[Link] PROGRAM
.
1 WAP to counts the number of alphabets ,digits, uppercase,
lowercase, # spaces and other characters(status of a string).
2 WAP to remove all odd numbers from the given list.
3 WAP to display those string which starts with ‘A’ from the
given list.
4 WAP to find and display the sum of all the values which are
ending with 3 from a list.”’
5 Write a program to accept values from a user and create a
tuple.
6 WAP to input total number of sections and stream name in 11th
class and display all information on the output screen.”
7 WAP to input name 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.
8 Write a program to find factorial of entered number using
library function fact()
9 Write a Program to call great function to find greater out of
entered 2 numbers, using import command
10 Write a program to show and count the number of words in a
text file ‘[Link]’ which is starting/ended with an word
‘The’, ‘the’, ‘my’, ‘he’, ‘they’.
11 WAP to read data from a text file [Link], and display word
which have maximum/minimum characters
12 Consider a binary file “[Link]” containing details such as
empno: ename:salary(separator‘:’).Write a python function to
display
details of those employees who are earning between 20000
and 40000.
13 Write a program that rotates the elements of a list so that the
element at the first index moves to the second index, the
element in the second index moves to the third
index, etc., and the element in the last index moves to the first
index.
14 Write a program to show push and pop operation using stack.
#Program1 : WAP to counts the number of
alphabets ,digits, uppercase, lowercase, # spaces and
other characters(status of a string).
str1 =input("enter a string")
n=c=d=s=u=l=o=0
for ch in str1:
if [Link]():
n+=1
if [Link]():
u=u+1
elif [Link]():
l=l+1
elif [Link]():
c=c+1
elif [Link]():
d=d+1
elif [Link]():
s=s+1
else:
o=o+1
print("[Link] alpha and digit",n)
print("[Link] capital alpha",u)
print("[Link] smallalphabet",l)
print("[Link] digit",d)
print("no. of spaces",s)
print("no of other character",o)
OUTPUT
enter a stringarmy public school DHAULA kuan 2020!!
[Link] alpha and digit 30
[Link] capital alpha 6
[Link] smallalphabet 20
[Link] digit 4
no. of spaces 0
no of other character 0
#Program 2:WAP to remove all odd numbers from the
given list.
L=[2,7,12,5,10,15,23]
for i in L:
if i%2==0:
[Link](i)
print(L)
OUTPUT
[7, 5, 15, 23]
#Program 3: WAP to display those string which starts with
‘A’ from the given list.
L=['AUSHIM','LEENA','AKHTAR','HIBA','NISHANT','AMAR']
count=0
for i in L:
if i[0] in ('aA'):
count=count+1
print(i)
print("appearing",count,"times")
OUTPUT
AUSHIM
AKHTAR
AMAR
appearing 3 times
‘“Program 4:WAP to find and display the sum of all the
values which are ending with 3 from a list.”’
L=[33,13,92,99,3,12]
sum=0
x=len(L)
for i in range(0,x):
if type(L[i])==int:
if L[i]%10==3:
sum+=L[i]
print(sum)
OUTPUT
49
#Program5: Write a program to accept values from a user
and create a tuple.
t=tuple()
n=int(input("enter limit:"))
for i in range(n):
a=input("enter number:")
t=t+(a,)
print("output is")
print(t)
output
enter limit:3
enter number:2
enter number:3
enter number:4
output is
('2', '3', '4')
‘“Program6:WAP to input total number of sections and
stream name in 11th class and display all information on
the output screen.”’
classxi=dict()
n=int(input("enter total number of section in xi class"))
i=1
while i<=n:
a=input("enter section:")
b=input("enter stream name:")
classxi[a]=b
i=i+1
print("class",'\t',"section",'\t',"stream name")
for i in classxi:
print("xi",'\t',i,'\t',classxi[i])
output
enter total number of section in xi class3
enter section:a
enter stream name:science
enter section:b
enter stream name:science
enter section:c
enter stream name:humanities
class section stream name
xi a science
xi b science
xi c humanities
‘“Program7:WAP to input name 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.’”
d1=dict()
i=1
n=int(input("enter number of entries"))
while i<=n:
c=input("enter country:")
cap=input("enter capital:")
curr=input("enter currency of country")
d1[c]=[cap,curr]
i=i+1
l=[Link]()
print("\ncountry\t\t","capital\t\t","currency")
for i in l:
z=d1[i]
print(i,'\t\t',end=" ")
for j in z:
print(j,'\t\t',end='\t\t')
x=input("\nenter country to be searched:")
#searching
for i in l:
if i==x:
print("\ncountry\t\t","capital\t\t","currency\t\t")
z=d1[i]
print(i,'\t\t',end=" ")
output
enter number of entries2
enter country:india
enter capital:delhi
enter currency of countryrupee
enter country:nepal
enter capital:kathmandu
enter currency of countryrupee
country capital currency
india delhi rupee
nepal kathmandu rupee
enter country to be searched:india
country capital currency
india
“““Program8 : Write a program to find factorial of entered
number using library function fact().”””
def fact(n):
if n<2:
return 1
else :
return n*fact(n-1)
import factfunc
x=int(input("Enter value for factorial : "))
ans=[Link](x)
print (ans)
#Program9 : Write a Program to call great function to find
greater out of entered 2 numbers, using import command.
def chknos(a, b):
if a>b:
print("the first number ",a,"is greater")
return a
else:
print("the second number ",b,"is greater")
return b
import greatfunc
a=int(input("Enter First Number : "))
b=int(input("Enter Second Number : "))
ans=[Link](a, b)
print("GREATEST NUMBER = ",ans)
output
#Program10 : Write a program to show and count the
number of words in a text file ‘[Link]’ which is
starting/ended with an word ‘The’, ‘the’, ‘my’, ‘he’, ‘they’.
f1=open("[Link]","r")
s=[Link]()
print("All Data of file in string : \n",s)
print("="*30)
count=0
words=[Link]()
print("All Words: ",words,", length is ",len(words))
for word in words:
if [Link]("the")==True: # [Link]
with(“the”)
count+=1
print("Words start with 'the' is ",count)
output
#PROGRAM 11 : WAP to read data from a text file
[Link], and display word which have
maximum/minimum characters.
f1=open("[Link]","r")
s=[Link]()
print(s)
words=[Link]()
print(words,", ",len(words))
maxC=len(words[0])
minC=len(words[0])
minfinal=""
maxfinal=""
for word in words[1:]:
length=len(word)
if maxC<length:
maxC=length
maxfinal=word
if minC>length:
minC=length
minfinal=word
print("Max word : ",maxfinal,", maxC: ",maxC)
print("Min word : ",minfinal,", minC: ",minC)
output
#Program12: Consider a binary file “[Link]” containing
details such as empno: ename:salary(separator‘:’).Write a
python function to display
details of those employees who are earning between
20000 and 40000.
fin=open("[Link]", 'rb')
x=[Link]()
while x:
data=[Link](":")
if float(data[0])>20000 and float(data[0])<30000:
print(data)
x=[Link]()
[Link]()
#Program 13: Write a program that rotates the elements
of a list so that the element at the first index moves to the
second index, the element in the second index moves to
the third
index, etc., and the element in the last index moves to the
first index.
n=int(input("Enter Number of items in List: "))
DATA=[]
for i in range(n):
item=int(input("Item :%d: "%(i+1)))
[Link](item)
print("Now List Items are :",DATA)
lg=len(DATA)
print("Now Number of items before update are :",lg)
b=["undef"]*lg
for x in (range(lg-1)):
if (x)>lg:
break
b[x+1]=DATA[x]
b[0]=DATA[-1]
print("RESULT OF NEW LIST IS " , b)
output
Enter Number of items in List: 2
Item :1: 1
Item :2: 2
Now List Items are : [1, 2]
Now Number of items before update are : 2
RESULT OF NEW LIST IS [2, 1]
#PROGRAM 14: Write a program to show push and pop
operation using stack.
#[Link]
def push(stack,x): #function to add element at the end
of list
[Link](x)
def pop(stack): #function to remove last element
from list
n = len(stack)
if(n<=0):
print("Stack empty....Pop not possible")
else:
[Link]()
def display(stack): #function to display stack entry
if len(stack)<=0:
print("Stack empty...........Nothing to display")
for i in stack:
print(i,end=" ")
#main program starts from here
x=[]
choice=0
while (choice!=4):
print("********Stack Menu***********")
print("1. push(INSERT)")
print("2. pop(DELETE)")
print("3. Display ")
print("4. Exit")
choice = int(input("Enter your choice :"))
if(choice==1):
value = int(input("Enter value "))
push(x,value)
if(choice==2):
pop(x)
if(choice==3):
display(x)
if(choice==4):
print("You selected to close this program")