Write a Python Program to Check the Given Number is Prime or Not
Write a Python Program to Check the Given Number is Prime or Not
VIDYALAYA NO 1 AFS
GURUGRAM HARYANA
COM
PUTER SCIENCE
PRACTICAL FILE
SUBMITTED
BY : ARYAN
ROLL NO : 07
CLASS:12A
# WRITE A PYTHON PROGRAM
TO CHECK THE GIVEN
NUMBER IS PRIME OR
NOT
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]]}
# Write a python program to pass a list to
a function and double the odd values and
half even values of a list and display list
element after changing.
def listprocess(L1):
length=len(L1)
print(length)
for i in range(0,length):
if(i%2==0):
L1[i]=L1[i]*2
else:
L1[i]=L1[i]/2
return(L1)
#__main__
'''
L1=[]
n=int(input("enter the number of elements ="))
for i in range(n):
item = int(input('Enter No. ='))
L1.append(item)
'''
L1=[1,2,3,4,5,6]
#__main__
# creation of tuple using list
L1=[]
n=int(input('ENTER THE NUMBER OF ELEMENTS :'))
for i in range(n):
print('Enter ', i+1 , ' element= ',end=' ')
element = int(input())
L1.append(element)
Enter 1 element= 1
Enter 2 element= 2
Enter 3 element= 3
Enter 4 element= 7
Enter 5 element= 8
The list 1 is : [1, 2, 3, 7, 8]
studentmark[rollno]=[name,age]
'''
studentmark={100:['ram','10'], 200:
['laxman,20']}
changevalue(studentmark)
print('PRINTING WHOLE DICTIONARY\
n',studentmark)
Run Part:-
PRINTING WHOLE DICTIONARY
{100: ['ram', '10'], 200: ['laxman,20']}
#__main__
#s=input("Enter the string")
s="this is a book"
this is a book
The total vowels are : 5
The total vowels are : 5
# Write a program in python to
generate random numbers
between 1 and 6.
import random
Run Part:-
random number between 1 and 6
x=pow(2,3) #1
print(x)
a=123.45
STR=str(a)
print(STR)
b=float(STR) #2
print(b)
c=int('15') #3
print(c)
print(range(5))
print(type(c))
d=-15
print("absolute value :=", abs(d)) #4
print("quotient and
remainder:=" ,divmod(9,5)) # return
quotient and remainder #5
print(sum([1,2,3]))
print(sum((1,2,3)))
print(sum([1,2,3],5))
Run part:-
the length of st is := 4
8
123.45
123.45
15
range(0, 5)
<class 'int'>
absolute value := 15
quotient and remainder:= (1, 4)
6
6
11
# Write a program in python to
explain mathematical functions-max-
min-oct-hex-round.
print("max")
print(max([5,7,6],[2,3,4])) # match first
element only of both lists
print(max([5,7,6],[7,4,3]))
print(max((5,7,6),(2,3,4))) # match first
element only of both tuples
print("min")
print(min([5,7,6],[2,3,4])) # match first
element only of both lists
print(min([5,7,6],[7,4,3]))
print(min((5,7,6),(2,3,4))) # match first
element only of both tuples
print(oct(10))
print(hex(10))
print(hex(15))
x=7.5550
y=int(x)
print(y)
z=round(x)
print(z)
p=round(x,2)
print(p)
Run part:-
Max
[5, 7, 6]
[7, 4, 3]
(5, 7, 6)
min
[2, 3, 4]
[5, 7, 6]
(2, 3, 4)
0o12
0xa
0xf
7
8
7.55
# Write a program in python to explain
string functions-join-split-replace.
#join only joins strings
st1="#"
print(st1.join("Student")) # joind string
st2="##"
print(st2.join("Student"))
st3="##"
print(st3.join(["good", "morning", "all"])) # join
with list
st4="##"
print(st3.join(("good", "morning"))) # join with
tuple
#st4="##"
#print(st3.join((777, "good", "morning"))) # show
errors- only string allowed in tuple
#replace
st7="this is a book"
print(st7.replace('is','are')) #find and replace
#capitalize
st8="this is a book"
print(st8.capitalize()) # capital first letter of a
string
Run part:-
S#t#u#d#e#n#t
S##t##u##d##e##n##t
good##morning##all
good##morning
['this', 'is', 'a', 'book']
['th', 's ', 's a ', 'ce-cream']
thare are a book
This is a book
# Write a program in python to
explain string constant function.
import string
Run part:-
print all alphabet in lowercase and uppercase
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLM
NOPQRSTUVWXYZ
def calculator(x,y,ch):
if(ch=='+'):
c=a+b
elif(ch=='-'):
c=a-b
elif(ch=='*'):
c=a*b
elif(ch=='/'): # float quotient
c=a/b
elif(ch=='//'): #integer quotient
c=a//b
elif(ch=='%'): # remainder
c=a%b
return c
our main file :-
import cal
#__main__
a=9
b=2
x=calculator(a,b,'%')
print(x)
Run part:-
1
# Write a program in python to read a
text line by line and display each word
separated by #.
while str:
str= myfilein.readline()
words = str.split()
for i in words:
print (i,"#",end='')
print()
myfilein.close()
Run part:-
hello #students #
myfilein = open('article.txt','r')
myfileout = open('copy.txt','w')
myfilein.close()
myfileout.close()
Run part:-
hello students
this is mobile
this is mobile
# Write a python program to read
characters from keyboard one by one,
then store all lowercase letters
inside a file "LOWER", all uppercase
letters gets inside a file "UPPER" and
other characters inside file
"OTHERS".
myfile1 = open('LOWER.txt','w')
myfile2 = open('UPPER.txt','w')
myfile3 = open('OTHERS.txt','w')
myfile1.writelines(Llower)
myfile2.writelines(Lupper)
myfile3.writelines(Lothers)
myfile1.close()
myfile2.close()
myfile3.close()
Run part:-
import pickle
#class declaration
class student:
def __init__(self, rno = 0 , nm = "" ):
self.rollno=rno
self.name=nm
def getdetails(self):
print("Enter the details\n")
self.rollno=input("Enter the rollno.:=")
self.name=input("Enter the name:=")
def show(self):
print("\n\nReport of students are:\n")
print("The Roll Number of student is: ",
self.rollno)
print("The name of student is : ",
self.name)
def getrollno(self):
return int(self.rollno)
# object creation
object1= student(1,"ram")
object1.show()
object2= student()
object2.getdetails()
object2.show()