Computer Science Lab Manual-1
Computer Science Lab Manual-1
AMBROSE VIDYALAYA
(CBSE)
Prepared by,
M. George Fernandez,
PGT-CS,
BDAV.
1. Input a welcome message and display it.
Aim:
To write a python program to get input as a “Welcome Message” and display it.
Program:
print(“Hello,”, welcome_message)
Output:
Result:
Thus the above python program was executed and output verified.
2. Input two numbers and display the larger/smaller number.
Aim:
To write a python program to get two numbers as a input and display the larger/smaller
number.
Program:
num1 = input("Enter the first number: ")
else:
Output:
96 is greater than 89
Result:
Thus the above python program was executed and output verified.
3. Input three numbers and display the largest/smallest number.
Aim:
To write a python program to get three numbers as a input and display the largest/smallest
number.
Program:
number1 = int(input('Enter First number : '))
largest_number = number1
largest_number = number2
else:
largest_number = number3
Output:
Enter First number : 893562
Result:
Thus the above python program was executed and output verified.
4. Generate the pyramid patterns using nested loop.
Aim:
To write a python program to generate the pyramid patterns using nested loop.
Program:
for i in range(size):
Output:
***
*****
*******
*********
Result:
Thus the above python program was executed and output verified.
5. Determine whether a number is a perfect number, an armstrong number or a palindrome.
Aim:
To write a python program to determine whether a number is a perfect number, an
armstrong number or a palindrome.
Program:
#palindrome
reverse = 0
temp = number
Reminder = temp % 10
if(number == reverse):
print(number,"is a Palindrome" )
else:
#perfect number
sum1=0
for i in range(1,number):
if(number%i==0):
sum1=sum1+i
if (sum1==number):
else:
print(number,"is not a perfect number")
#armstrong number
sum=0
temp=number
while temp>0:
digit=temp%10
sum+=digit**3
temp//=10
if number==sum:
else:
Output:
Reverse of it is 351
Result:
Thus the above python program was executed and output verified.
6. write a program to input two strings. If strings1 is contained in string2, then create a third
string with first four characters of strings2’ added with word ‘Restore’.
Aim:
Program:
if s1 in s2:
s3=s2[0:4] +” Restore”
output :
result:
Thus the above python program was executed and output verified.
7. program that reads a line and prints its statistics like.
Aim:
To write a python program that reads a line and prints its statistics like.
Program:
Line=input(“enter a line”)
Lowercount=upper count=0
Digitcount=alphacount=symcount=0
for a in line:
if a.is lower():
lowercount+=1
elif a.isupper():
uppercount+=1
elif a.isdigit():
digitcount+=1
elif a.isalpha():
alphacount+=1
elif aisalnum()!=true and a!=’ ‘ :
symcount+=1
print(“number of symbols:”,symcount)
Output:
Number of digits:3
Number of symbols:1
Result:
Thus the above python program was executed and output verified.
8. write a program that inputs individual words of your school motto and joins them to make
a sting . it should also input day, month and year of your schools foundation date and print
the complete day
Aim:
To write a python program that inputs individual words of your school motto and joins
them to make a sting . it should also input day, month and year of your schools foundation
date and print the complete day.
Program :
dd=input(“enter dd:”)
mm=input(“enter mm:”)
yyyy=input("enter yyyy:”)
dt=”/”.join ([dd,mm,yyyy])
print(“school motto:”,motto)
Output:
Enter mm:10
Enter yyyy:1975
Result:
Thus the above python program was executed and output verified.
9. write a program that inputs a list , replicates it twice and then prints the sorted list in
ascending and descending orders.
Aim:
To write a python a program that inputs a list , replicates it twice and then prints the
sorted list in ascending and descending orders.
Program:
val=eval(input(“enter a list:”))
val=val*2
val.sort()
val.sort(reverse =true)
print(“sorted in descending order:”, val)
Output :
Replicated list:[23,11,29,23,11,29]
Result:
Thus the above python program was executed and output verified.
10. program to count the frequency of the given element in a list of numbers.
Aim:
To write a python a program to count the frequency of the given element in a list of
numbers.
Program:
Ist=eval(input(“enter list :”))
length =len(lst)
count=0
if element==lst[i]:
count+=1
if count==0
else:
Output:
Enter element:2
Result:
Thus the above python program was executed and output verified.
11.write a program to input two lists and display the maximum element from the elements of
both the lists combined , along with its index in its list.
Aim:
To write a python program to input two lists and display the maximum element from the
elements of both the lists combined , along with its index in its list.
Program:
lst1=eval(input(“enter list1:”))
lst2=eval(input(“enter list2:”))
mx1=max(lst1)
mx2=max(lst2)
if mx1>=mx2:
else:
Output:
Enter list1:[6,8,11,6,12,7,16]
Enter list2:[34,11,23,11]
Result:
Thus the above python program was executed and output verified.
12.writ a program to input a tuple and check if it contains the all elements as same.
Aim:
To write a python program to input a tuple and check if it contains the all elements as
same.
Program:
tup=eval(input(“enter a tuple:”))
ln=len(tup)
num=tup.count(tup[0])
if num==ln:
else:
Output:
Result:
Thus the above python program was executed and output verified.
13. writ a program to check if the elements in the first half of an tuple are sorted in ascending
order or not.
Aim:
To write a python program to check if the elements in the first half of an tuple are
sorted in ascending order or not.
Program:
tup=eval(input(“enter a tuple:”))
ln =len(tup)
mid=ln//2
if ln%2==1:
mid=mid+1
half=tup[:mid]
if sorted (half)==list(tup[:mid]):
Output:
Enter a tuple:11,22,13,14,10
Enter a tuple:11,22,13,14,10
Result:
Thus the above python program was executed and output verified.
14. write a program a to check if the minimum elements of a tuple lies at the middle position
of the tuple .
Aim:
To write a python program a to check if the minimum elements of a tuple lies at the
middle position of the tuple .
Program:
tup=eval(input(“enter a tuple”))
ln=len(tup)
lies=false
mn=min(tup)
if ln%2==0:
half=ln//2
if mn==tup[half] or mn==tup[half-1]:
lies=true
else:
half=ln//2
if mn==tup[half]:
lies=true
if lies==true:
else:
Output:
Enter a tuple:6,7,12,13,5,11,13,8,13
Result:
Thus the above python program was executed and output verified.
15. write a program to input names of n students and store them in a tuple .also, input a name
from the user and find if this students is present in the tuple or not .
Aim:
To write a python program to input names of n students and store them in a tuple
.also, input a name from the user and find if this students is present in the tuple or not .
Program:
lst=[]
lst.append(name)
ntuple=tuple(lst)
if nm in tuple:
else:
print(nm, “does not exist in the tuple”)
Output:
Result:
Thus the above python program was executed and output verified.
16.write a program to delete the keys of a dictionary, one of in LIFO order. Make sure that
there is no error generated after the last item delete.
Aim:
To write a python program to delete the keys of a dictionary, one of in LIFO order. Make
sure that there is no error generated after the last item delete.
Program:
Output:
Result:
Thus the above python program was executed and output verified.
17. write a program to print the maximum, minimum, sum of keys of numbers dictionary as
given below
Numbers ={1:111,2:222,3:333,4:444}
also individually find minimum, maximum, sum of values
Aim:
To write a python program to print the maximum, minimum, sum of keys of numbers
dictionary as given below
Numbers ={1:111,2:222,3:333,4:444}
also individually find minimum, maximum, sum of values.
Program:
numbers ={1:111,2:222,3:333,4:444}
max_key_val=max(numbers)
min_key_val=min(numbers)
print(“maximum and minimum keys:”,ma_key_val, min_)key_val)
max_value=max(numbers.values())
min_value=min(numbers.values())
key_sum=sum(numbers)
values_sum=sum(number.values())
Output:
Aim:
To write a python program to create a dictionary name the Dct with 10 keys 0..9, each
having values has 200. Update the first and the last values by adding 200 to each of them.
Program:
dct=dict.fromkeys(range(10),200)
dct[0]+=200
dct[9]+=200
print(dct)
Output:
{0: 400, 1: 200, 2: 200, 3: 200, 4: 200, 5: 200, 6: 200, 7: 200, 8: 200, 9: 400}
Result:
Thus the above python program was executed and output verified.
19. write a program to read a sentence and then createa dictionary contains the frequency of
letters and digits in the sentence.ignore other symbols,if any.
Aim:
To write a python program to read a sentence and then create dictionary contains the
frequency of letters and digits in the sentence.ignore other symbols,if any.
Program:
alphabet_digits=”abcdefdghijklmnopqrstuvwxyz0123456789”
char_count={}
if char in alphabet_digits:
if char in char_count:
char_count[char]=char_count[char]+1
else:
char_count[char]=1
print(char_count)
Output:
Enter a sentence : hello there! Class 10 is done and now you are in class 11
Total characters in the sentence are:58
{‘h’: 2, ‘e’: 5, ‘l’: 4, ‘o’: 4 ‘t’:1 ‘r’: 2, ‘c’:2, ‘a’: 4 ‘s’: 5 ‘1’:3, ‘0’: 1, ‘i’: 2, ‘d’:2, ‘n’:4, ‘w’:1
,’y’:1,’u’:1}
Result:
Thus the above python program was executed and output verified.
20.write a python program to find the highest 2 values in a dictionary
Aim:
Program:
s=sorted (numbers.values())
Output:
Given dictionary is {31:111, 22:222, 43:333,14:444, 25:555}
Result:
Thus the above python program was executed and output verified.