Utkrisht - program file
Utkrisht - program file
DHAULA KUAN
Session 2020-21
PYTHON
PROGRAMMING FILE
S.NO PROGRAM
.
1 WAP to counts the number of alphabets ,digits, uppercase,
lowercase, # spaces and other characters(status of a string).
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.
n=c=d=s=u=l=o=0
for ch in str1:
if ch.isalnum():
n+=1
if ch.isupper():
u=u+1
elif ch.islower():
l=l+1
elif ch.isalpha():
c=c+1
elif ch.isdigit():
d=d+1
elif ch.isspace():
s=s+1
else:
o=o+1
print("no.of smallalphabet",l)
print("no.of digit",d)
print("no. of spaces",s)
OUTPUT
no.of smallalphabet 20
no.of 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:
L.remove(i)
print(L)
OUTPUT
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
classxi=dict()
i=1
while i<=n:
a=input("enter section:")
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 section:a
enter section:c
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
while i<=n:
c=input("enter country:")
cap=input("enter capital:")
d1[c]=[cap,curr]
i=i+1
l=d1.keys()
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 country:india
enter capital:delhi
enter country:nepal
enter capital:kathmandu
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=factfunc.fact(x)
print (ans)
#Program9 : Write a Program to call great function to find
greater out of entered 2 numbers, using import command.
import greatfunc
a=int(input("Enter First Number : "))
b=int(input("Enter Second Number : "))
ans=greatfunc.chknos(a, b)
print("GREATEST NUMBER = ",ans)
output
#Program10 : Write a program to show and count the
number of words in a text file ‘DATA.TXT’ which is
starting/ended with an word ‘The’, ‘the’, ‘my’, ‘he’, ‘they’.
f1=open("data.txt","r")
s=f1.read()
print("="*30)
count=0
words=s.split()
if word.startswith("the")==True: # word.ends
with(“the”)
count+=1
output
#PROGRAM 11 : WAP to read data from a text file
DATA.TXT, and display word which have
maximum/minimum characters.
f1=open("data.txt","r")
s=f1.read()
print(s)
words=s.split()
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 “Emp.dat” 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("emp.dat", 'rb')
x=fin.readline()
while x:
data=x.split(":")
if float(data[0])>20000 and float(data[0])<30000:
print(data)
x=fin.readline()
fin.close()
#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.
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]
#stack.py
def push(stack,x): #function to add element at the end
of list
stack.append(x)
def pop(stack): #function to remove last element
from list
n = len(stack)
if(n<=0):
print("Stack empty....Pop not possible")
else:
stack.pop()
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")