Mahit-Computer Record Final
Mahit-Computer Record Final
COMPUTER
SCIENCE
PROGRAM NO PROGRAM
1 Pattern
3 Character Count
4 Series Generator
5 String Number
6 String Reverse
7 Formula Checker
8 Duplicate Elements
9 Number Finder
10 Positive or Negative
11 List Manipulation
12 Smallest Fraction
13 Nested Tuple
15 Tuple Generator
16 Calendar Dictionary
17 Dictionary Manipulation
18 Letter Frequency
19 Phonebook
20 E-Mail's
OUTPUT:
EX. NO. 1
PATTERN PROGRAMS
AIM:
WAP to print following patterns.
SOURCE CODE:
Part a:
n = int(input("Enter the number of lines you want in the pattern: "))
for i in range(n):
for j in range(n-i-1):
print(' ', end='')
for j in range((2*i)+1):
print('*', end='')
print()
for i in range(n-1):
for j in range(i+1):
print(' ', end='')
for j in range(2*(n-i-1)-1):
print('*', end='')
print()
Part b:
n = int(input(“Enter the number of lines you want in your pattern: “))
For I in range(n-1):
For j in range(i):
Print(‘ ‘, end=’’)
For k in range(2*(n-i)-1):
Print(‘*’, end=’’)
Print()
For I in range(n):
For j in range(n-i-1):
Print(‘ ‘, end=’’)
For k in range((2*i)+1):
Print(‘*’, end=’’)
Print()
RESULT:
Thus, the program is executed successfully and the output is verified.
OUTPUT:
EX. NO. 2
PROGRAM TO PRINT 20 TWIN PRIME PAIRS
AIM:
WAP to print first 20 twin prime numbers. [Two prime numbers are called twin primes, if there is present
only one composite number between them.]
SOURCE CODE:
def prime(n):
if n<2:
return False
for I in range(2, int(n**0.5)+1):
if n%i==0:
return False
return True
def gentwinpairs(s,e):
twinprimes=[]
for n in range(s,e+1):
if prime(n) and prime(n+2):
twinprimes.append((n,n+2))
return twinprimes
sr=1
er=320
print(“Twin Prime Number Program in Python”)
print(”Generating twin prime numbers between {sr} and 20: “)
print()
twinprimes=gentwinpairs(sr,er)
for primepair in twinprimes:
print(primepair)
RESULT:
Thus, the program is executed successfully and the output is verified.
OUTPUT:
EX. NO. 3
PROGRAM Character Count
AIM: Write a program to input line(s) of text from the user until enter is pressed. Count the total number of
characters in the text (including white spaces), total number of alphabets, total number of digits, total
number of special symbols and total number of words in the given text.
SOURCE CODE:
Inp = input(“Write a sentence: “)
C = len(inp)
Print(“Total Characters: “,c)
Alpha=dig=spc=0
For a in inp:
If a.isalpha():
Alpha+=1
Elif a.isdigit():
Dig+=1
Else:
Spc += 1
Print(“Total number of alphabets found is: “,alpha)
Print(“Total number of digits found is: “,dig)
Print(“Total number of special characters found is: “,spc)
Space = 0
For b in inp:
If b.isspace():
Space+=1
Print(“Total Words in the Input :”,(space+1))
RESULT:
Thus, the program is executed successfully and the output is verified.
OUTPUT:
EX. NO. 4
PROGRAM Series Generator
SOURCE CODE:
Print(“Welcome To The Series Generator!”)
N = int(input(“””Choose one of the following options:
1) Fibonacci series:1
2) Number Factorial:2
3) Mersenne numbers:3”””))
If n==1:
Nu = int(input(“Enter the number of terms you want in the fibonacci series: “))
N1, n2 = 0, 1
Count = 0
If nu <= 0:
Print(“Please enter a positive integer”)
Elif nu == 1:
Print(“Fibonacci series of:”,n,”terms”)
Print(n1)
Else:
Print(“Fibonacci series:”)
While count < nu:
Print(n1)
Nt = n1 + n2
N1 = n2
N2 = nt
Count += 1
Elif n==2:
Nu = int(input(“Enter the number whose factorial you would like: “))
Fac = 1
For I in range(1,nu+1):
Fac = fac*i
Print(“The factorial of the number you have entered is”, fac)
Elif n==3:
Nu = int(input(“Enter the number of elements in Mersenne series: “))
Print(“Mersenne numbers are: “)
For I in range(nu):
Print((2**i)-1)
Else:
Print(“Enter a valid input!!!”)
RESULT:
Thus, the program is executed successfully and the output is verified.
OUTPUT:
EX. NO. 5
PROGRAM String Number
SOURCE CODE:
n = int(input("Enter an integer: "))
s = input("Enter a string: ")
n1 = ""
for i in s:
if i.isdigit() :
n1+=i
if n1=="":
n1=0
else:
n1 = int(n1)
print("The number you have entered is: ", n)
print("The string you have entered is: ", s)
print("The number extracted is: ",n1)
print("The final result is: ",n," + ",n1,end='')
print(" which is equal to: ",n+n1)
RESULT:
Thus, the program is executed successfully and the output is verified.
OUTPUT:
EX. NO. 6
PROGRAM String Reverse
AIM: Write a program to input a line of text and create a new line of text where each word of input line is
reversed.
SOURCE CODE:
s = input("Enter a sentence:")
def rev(sentence):
words = s.split()
rev = list(map(lambda x: x[::-1], words))
return ' '.join(rev)
print(rev(s))
RESULT:
Thus, the program is executed successfully and the output is verified.
OUTPUT:
EX. NO. 7
PROGRAM Formula Checker
AIM: Write a program to input a formula with some brackets and checks and prints out if the formula has
the same number of opening and closing parentheses.
SOURCE CODE:
Form = input(“Enter a formula: “)
Print(“The formula entered is:”, a)
C=0
For I in form:
If i==’(‘:
C+=1
Elif i==’)’:
c-=1
elif i==’[‘:
count+=1
elif i==’]’:
count-=1
elif i==’{‘:
count+=1
elif i==’}’:
count-=1
elif i==”<”:
count+=1
elif i==”>”:
count-=1
if count==0:
print(“The formula entered by you has the same number of opening and closing parentheses!!!”)
else :
print(“The formula entered by you has unequal number of opening and closing parentheses!!!”)
RESULT:
Thus, the program is executed successfully and the output is verified.
OUTPUT:
EX. NO. 8
PROGRAM Duplicate Elements
AIM: Program to find frequencies of all elements of a list. Also, print the list of unique elements in the list
and duplicate elements in the given list.
SOURCE CODE:
N = int(input(“Enter the number of elements for the list: “))
L = []
For I in range(n):
l.append(eval(input(“Enter the element: “)))
print()
print(“The list you have created is”,l)
print()
for I in l:
print(“The element “ ,I, “repeats” ,mainList.count(i), “times!!!”)
print()
l1 = l2 = []
#l1 is list for all duplicate elements
#l2 is list for all unique elements
For I in l:
If l.count(i) > 1:
If I not in l1:
L1.append(i)
Else:
L2.append(i)
Print(“Frequency of duplicated elements is:”,len(l1))
Print(“Frequence of unique elements is:”,len(l2))
If len(l1)==0:
Print(“The elements that do not have any duplicate’s in it is:”,l2)
Elif len(l2)==0:
Print(“The elements that have duplicate’s in it is:”,l1)
Else:
Print(“The elements that have duplicate’s in it is:”,l1)
Print(“The elements that do not have any duplicate’s in it is:”,l2)
RESULT:
Thus, the program is executed successfully and the output is verified.
OUTPUT:
EX. NO. 9
PROGRAM Number Finder
AIM: Write a program to check if a number is present in the list or not. If the number is present, return the
position of the number. Print an appropriate message if the number is not present in the list.
SOURCE CODE:
N = int(input(“Enter the number of elements for the list: “))
N1 = int(input(“Enter the number to find in the list: “))
L = []
L1 = []
C=0
For I in range(n):
l.append(int(input(“Enter a number: “)))
print(“The list is:”,l)
for I in range(n):
if l[i]==n1:
c+=1
l1.append(i)
if c!=0:
print(“The given number was found in the list”)
print(“The numbers position is:”,l1[0])
else:
print(“The given number was not found in the list!!!”)
RESULT:
Thus, the program is executed successfully and the output is verified.
OUTPUT:
EX. NO. 10
PROGRAM Positive or Negative
AIM: Write a program to read a list of n integers (positive as well as negative). Create two new lists, one
having all positive numbers and the other having all negative numbers from the given list. Print all three
lists.
SOURCE CODE:
N = int(input(“Enter the number of elements you want in your list: “))
L=[]
Pos=[]
Neg=[]
For I in range(n):
l.append(int(input(“Enter a number: “)))
for I in range(n):
if l[i]>=0:
pos.append(l[i])
else:
neg.append(l[i])
print(“The original list is: “,l)
print(“List of positive elements is: “,pos)
print(“List of negative elements is: “,neg)
RESULT:
Thus, the program is executed successfully and the output is verified.
OUTPUT:
EX. NO. 11
PROGRAM List Manipulation
AIM: 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
moves to the first index.
SOURCE CODE:
L=eval(input("Enter a list : "))
L1=[]
L1.append(L[-1])
for i in range(len(L)-1):
L1.append(L[i])
print(L1)
RESULT:
Thus, the program is executed successfully and the output is verified
OUTPUT:
EX. NO. 12
PROGRAM Smallest Fraction
AIM: Write a program to read two lists num and dnum which contain the numerators and
denominators of same fractions at the respective indexes. Then display the smallest fraction along
with its index.
SOURCE CODE:
num=[3,2,3,4,5,6,7,8,9]
dnum=[4,3,4,5,6,7,8,9,10]
n=len(num)
r=num[0]/dnum[0]
c=0
for i in range(n):
d=num[i]/dnum[i]
if r>d:
r=d
c=i
print("Smallest fraction is",r,"at index",c)
RESULT:
Thus, the program is executed successfully and the output is verified.
OUTPUT:
EX. NO. 13
PROGRAM Nested Tuple
AIM: Write a program to create a nested tuple to store roll number, name and 3 subject marks of 5
students and compute total marks and average marks obtained by each student.
SOURCE CODE:
L=[]
for i in range(5):
nam=input("enter name of student: ")
m1 =int(input("enter marks of subject 1: "))
m2 = int(input("enter marks of subject 2: "))
m3 = int(input("enter marks of subject 3: "))
tot = m1+m2+m3
avg = tot/3
T1 = (nam,(m1,m2,m3),("total = ",tot, "avg = ",avg))
L.append(T1)
print(L)
RESULT:
Thus, the program is executed successfully and the output is verified.
OUTPUT:
EX. NO. 14
PROGRAM Concatenation with Tuples
AIM: Write a program that inputs two tuples and creates a third, that contains all elements of the first
followed by all elements of the second.
SOURCE CODE:
T1=eval(input("Enter a tuple "))
T2=eval(input("Enter another tuple"))
T3 = T1+T2
print(T3)
RESULT:
Thus, the program is executed successfully and the output is verified.
OUTPUT:
EX. NO. 15
PROGRAM Tuple Generator
SOURCE CODE:
t=[]
for i in range(1,51):
t.append(i*i)
t= tuple(t)
print(t)
T=[]
x=97
for i in range(1,27):
T.append(chr(x)*i)
x+=1
T = tuple(T)
print(T)
RESULT:
Thus, the program is executed successfully and the output is verified.
OUTPUT:
EX. NO. 16
PROGRAM Calendar Dictionary
AIM: Create a dictionary whose keys are month names and whose values are the number of days in
the corresponding months.
a) Ask the user to enter a month name and use the dictionary to tell how many days are in the month.
b) Print out all the keys in alphabetical order.
c) Print out all the months with 31 days.
d) Print out the (key-value) pairs sorted by the number of days in each month.
SOURCE CODE:
d={"January":31,"February":28,"March":31,"April":30,"May":31,"June":30, "July":31,
"August":31,"September":30,"October":31,"November":30,"December":31}
n = input ( "Enter a name of a month: ")
print(d[n])
l=list(d.keys())
Ls=sorted(l)
L =[]
print(Ls)
for i in l:
if d[i]==31:
L.append(i)
print(L)
k=None
L2=[]
for i in l:
L2.append([i,d[i]])
for i in range(len(l)):
for p in range(len(l)-1):
if L2[p+1][1]<L2[p][1]:
k= L[p]
L2[p]=L2[p+1]
L2[p+1]=k
print(L2)
RESULT:
Thus, the program is executed successfully and the output is verified.
OUTPUT:
EX. NO. 17
PROGRAM Dictionary Manipulation
AIM: A dictionary D1 has values in the form of lists of numbers. Write a program to create a new
dictionary D2 having same keys as D1 but values as the sum of the list elements.
SOURCE CODE:
d={'A':[1,2,3],'B':[4,5,6]}
D={}
for i in d:
D[i]=sum(d[i])
print(D)
RESULT:
Thus, the program is executed successfully and the output is verified.
OUTPUT:
EX. NO. 18
PROGRAM Letter Frequency
SOURCE CODE:
s = input("Enter a string : ")
d={}
for i in s:
if i not in d:
d[i]=1
else:
d[i]+=1
print(d)
RESULT:
Thus, the program is executed successfully and the output is verified.
OUTPUT:
EX. NO. 19
PROGRAM Phonebook
AIM: Write a program to input your friends’ names and their phone numbers and store them in the
dictionary as the key-value pair. Perform the following operations on the dictionary:
a) Display the name and phone number of all your friends.
b) Ass a new key-value pair in this dictionary and display the modified dictionary.
c)Delete a particular friend from the dictionary.
d) Modify the phone number of an existing friend.
e) Check if a friend is present in the dictionary or not.
f) Display the dictionary in sorted order of name.
SOURCE CODE:
D={}
c=0
while c==0:
n = input("Enter Friend's Name : ")
n1 = int(input("Enter their number : "))
if n not in D:
D[n] = n1
else:
print("You have entered this name already!!!")
k = int(input("Enter 1 to enter another number or 2 to stop"))
if k == 2:
c=1
else:
continue
print(D)
n2 = input("Enter a new name : ")
n3=int(input("Enter their number: "))
if n2 not in D:
D[n2]=n3
else:
print("Name exists already!!!")
print(D)
nam= input("Enter name to check for : ")
if nam in D:
print("Name is in dictionary")
print(nam,D[nam])
m = input("Enter the name whose number to modify")
if m in D:
newN = int(input("Enter a new number"))
D[m] = newN
else:
print("Name not found in dictionary")
print(D)
print()
print("sorted list is" )
list(D.keys())
L.sort()
D2 ={}
for i in L:
D2[i] = D[i]
print(D2)
RESULT:
Thus, the program is executed successfully and the output is verified.
OUTPUT:
EX. NO. 20
PROGRAM E-Mail's
AIM: Write a program to read email ID’s of n number of students and store them in a tuple. Create
two new tuples, one to store only the username of all the email ID’s. Print all three tuples at the end
of the program.
SOURCE CODE:
n = int(input("Enter number of id's : "))
L = []
L = []
L2=[]
for i in range(n):
k=input("Enter the email id: ")
l.append(k)
for i in l:
x=""
z=""
print(i)
y = len(i)
for a in range(len(i)):
if a<y:
if i[a]!="@":
x+=i[a]
else:
y=a
L.append(x)
for b in range(y,len(i)):
z+=i[b]
L2.append(z)
l = tuple(l)
L=tuple(L)
L2=tuple(L2)
print(l)
print(L)
print(L2)
RESULT:
Thus, the program is executed successfully and the output is verified.