# WRITE A PYTHON PROGRAM TO CHECK
THE GIVEN NUMBER IS PRIME OR NOT
a=int(input("Enter a number : "))
half=a/2
i=2
ctr=0
print ("The factors of ",a,"are :")
while i<=half:
if a%i==0:
print ("It's Not a prime number")
break
i=i+1
if i>half:
print ("\nIt is a prime number")
Run Part :-
Enter a number : 7
The factors of 7 are :
It is a prime number
# WRITE A PYTHON PROGRAM TO
COUNT THE OCCURRENCES OF A
WORD (WHOLE WORD) IN A STRING
USING USER defined function.
def countword(string,s):
k=0
words = f.split()
print(f)
print(words)
for i in words:
if(i==s):
k=k+1
print("Occurrences of the word:")
print(k)
#__main__
f="this is is a book"
search ="is"
countword(f,search)
Run Part:-
this is is a book
['this', 'is', 'is', 'a', 'book']
Occurrences of the word:2
# Write a Python program to show the
frequency of each element in list.
'''
L1=[]
n=int(input("enter the number of elements ="))
for i in range(n):
item = int(input('Enter No. ='))
L1.append(item)
'''
L1=[11,2,2,1,3,11,4]
print("the elementsin List L1",L1)
L2=L1
L2.sort()
print("the elementsin List L2",L2)
d=dict()
for ch in L2:
if ch in d:
d[ch]+=1
else:
d[ch]=1
for i in d:
print (i, d[i])
Run Part :-
the elementsin List L1 [11, 2, 2, 1, 3, 11, 4]
the elementsin List L2 [1, 2, 2, 3, 4, 11, 11]
11
22
31
41
11 2
# Write a Python program to
search an element in a list and
display the frequency of element
present in
list and their location.
'''
L1=[]
n=int(input("enter the number of elements ="))
for i in range(n):
item = int(input('Enter No. ='))
L1.append(item)
'''
L1=[3,11,2,2,1,3,11,3,3]
print("the elements in List L1",L1)
# searched element
x=3
#count the frequency of x
d=dict()
d[x]=0
for ch in L1:
if x==int(ch):
d[x]+=1
print("Frequency of x in list:=",d.values())
v=d[x]
print(v)
#find the location of x in
list.
L2=[]
d=dict()
d[x]=0
count=1
for ch in L1:
if x==int(ch):
L2.append(count)
count+=1
print("Location of x in List :=", L2)
dict1=dict()
dict1.update({x:[v, L2]})
print("Location of x in List :=", dict1)
Run part:-
the elements in List L1 [3, 11, 2, 2, 1, 3, 11, 3, 3]
Frequency of x in list:= dict_values([4])
4
Location of x in List := [1, 6, 8, 9]
Location of x in List := {3: [4, [1, 6, 8, 9]]}