Input: "Enter String: "

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

NAME- AYUSH KUMAR ROLL NO.

-25 BCA(SEM-III) SEC A STUDEN ID-


20151034

PROBLEM STATEMENT -: WAP to count the frequency of words in a string the output should be displayed
in the dictionary format where each unique word of the string will be the key in the dictionary and its
frequency will be the value of dictionary.
Program: -
1. string=input("Enter string: ").split() #input string and convert to list
2. d={} #initialize an empty dictionary
3. l=[] #initialize an empty list
4. for i in string: #iterating through string
if i not in l:
l=[i]
d[i]=string.count(i) #assigning the frequency to the
else: pass word

5. print(d) #displaying the dictionary

PYTHON LAB
NAME- AYUSH KUMAR ROLL NO.-25 BCA(SEM-III) SEC A STUDEN ID-
20151034

PYTHON LAB
NAME- AYUSH KUMAR ROLL NO.-25 BCA(SEM-III) SEC A STUDEN ID-
20151034

PROBLEM STATEMENT -: WAP to find mean median and mode of a list.

Program: -

1. list_a=[] #creating an empty list


2. sum=0
3. count=countm=mode=0
4. size=int(input("Enter size of list:")) 5. for i in range(size):
mode+=str(list_a[i])+' '
if count>countm: #if it is greater the max_count
countm=count #replace it
mode+=list_a[i]+' ' #change value of mode
12.print("mode =",mode) #mode of list
a=int(input("enter element :")) #getting elements for list
list_a.append(a) #adding element to list
6. list_a.sort()

#to find mean of list


7. for i in list_a:
sum+=i #sum of all elements of list
8. print("mean =",sum/size) #mean of list

#to find median of list


9. if size%2==0: #if even number of elements
print("median =",(list_a[(size//2)-1]))
10.else: #if odd number of elements
print("median =",(list_a[((size-1)//2)-1]))

#to find mode of list


11.for i in range(size):
count=list_a.count(list_a[i]) #counting the occurance if
count==countm and str(list_a[i]) not in mode:

PYTHON LAB
NAME- AYUSH KUMAR ROLL NO.-25 BCA(SEM-III) SEC A STUDEN ID-
20151034

PYTHON LAB
NAME- AYUSH KUMAR ROLL NO.-25 BCA(SEM-III) SEC A STUDEN ID-
20151034

PROBLEM STATEMENT -: Write a python program to print all the prime numbers below 100.
Program: -
1. l = [] #list for storing prime numbers
2. for i in range(2,100):
t=1 #flag
for x in l: #traversing elements of list containing prime numbers
if i%x==0:
t=0
break
if t==1: #if flag is 1
l.append(i) #add to list
3. for i in l:
print(i,end=',')

PYTHON LAB
NAME- AYUSH KUMAR ROLL NO.-25 BCA(SEM-III) SEC A STUDEN ID-
20151034

PYTHON LAB
NAME- AYUSH KUMAR ROLL NO.-25 BCA(SEM-III) SEC A STUDEN ID-
20151034

PROBLEM STATEMENT -: Write a python script to create a list of n variables. Take a value from user which
will be replaced by another value given by the user. The list can be heterogeneous.
Program: -

1. list_in = [] #creating empty list to insert elements


2. size = int(input('enter size of list: ')) #getting size of list 3. for i in
range(size): a = eval(input('enter element: ')) #inserting elements
list_in.append(a)
4. ele_old = eval(input('enter element out want to replace: '))
5. ele_new = eval(input('enter element out want to replace with: '))
6. print('list before changes: ',list_in)
7. while ele_old in list_in: #replacing elements
index = list_in.index(ele_old) list_in[index]
= ele_new
8. print('list after changes: ',list_in)

PYTHON LAB
NAME- AYUSH KUMAR ROLL NO.-25 BCA(SEM-III) SEC A STUDEN ID- 20151034

Output: -

PYTHON LAB
NAME- AYUSH KUMAR ROLL NO.-25 BCA(SEM-III) SEC A STUDEN ID-
20151034

PROBLEM STATEMENT -: Write a recursive function to find the factorial of a given number.

Program: -

1. def fact(n): #defining function to find factorial if n<=1: return 1


else:
return n*fact(n-1)
2. num = int(input('enter the number: ')) #getting number for factorial
3. if num<0: #checking if number is valid or not
print('not valid number')
4. else:
print('factorial is',fact(num))

PYTHON LAB
NAME- AYUSH KUMAR ROLL NO.-25 BCA(SEM-III) SEC A STUDEN ID- 20151034

Output:

PYTHON LAB
NAME- AYUSH KUMAR ROLL NO.-25 BCA(SEM-III) SEC A STUDEN ID-
20151034

Problem Statement: - Write a function dups to find whether there are duplicates in the list.

Program: -

1. def dups(list_1):
for i in list_1:
if list_1.count(i)>1:
return True
else: return False

2. list_a = [1,2,3,4,5,1,2,4,5,123,34]

3. if dups(list_a): print("there are duplicates in the list")


4. else:
print("there are no duplicates in the list")

PYTHON LAB
NAME- AYUSH KUMAR ROLL NO.-25 BCA(SEM-III) SEC A STUDEN ID-
20151034

Output:

PYTHON LAB

You might also like