0% found this document useful (0 votes)
17 views

G 11 C.Sc Lab Manual

The document contains a comprehensive index of programming exercises and their corresponding code implementations, covering various topics such as leap years, temperature conversion, patterns, series summation, and string manipulations. Each program includes a question prompt and the relevant Python code to solve it, demonstrating fundamental programming concepts and techniques. Additionally, it features menu-driven programs for user interaction and nested data structures like lists and tuples.

Uploaded by

suganya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

G 11 C.Sc Lab Manual

The document contains a comprehensive index of programming exercises and their corresponding code implementations, covering various topics such as leap years, temperature conversion, patterns, series summation, and string manipulations. Each program includes a question prompt and the relevant Python code to solve it, demonstrating fundamental programming concepts and techniques. Additionally, it features menu-driven programs for user interaction and nested data structures like lists and tuples.

Uploaded by

suganya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 30

INDEX

S Page Teacher’s
No Date Program No sign
1 Leap year
2 Temperature – Celsius and Farenheit
3 Patterns
4 Sum of series
5 Menu Driven – Perfect number, Armstrong
6 Menu driven – Fibonacci, GCD
7 Menu driven – Palindrome, Prime Number
8 String - 1
9 String - 2
10 String - 3
11 String - 4
12 List –Methods

13 List - Largest & second largest

14 List – Left Shift , Right Shift

15 Nested List - Employee

16 Tuple Manipulations

17 Tuple – Creation
Dictionary – Creation and Search
18

19 Dictionary – Frequency of character and word

20 Dictionary – Creation and Updation

Science
Program 1 : Leap Year Date :

Question :

Write a program to input a year and print if it is a leap year or not.

Code:

year = int(input("Enter year"))


if (year % 400 == 0) and (year % 100 == 0):
print(year," is a leap year")
elif (year % 4 ==0) and (year % 100 != 0):
print(year," is a leap year")
else:
print(year," is not a leap year")
Program 2 : Temperature Conversion

Question :

Write a program to input a temperature value (in float) and option (1-Celsius to
Farenheit 2 – Farenheit to Celsius) and convert it into the required temperature.

Code :

temp = float(input("Input the temperature you like to convert? (e.g., 45, 102
etc.) : "))
temp_code=int(input("Enter the option 1-Celsius to Farenheit 2 - Farenheit to
Celsius 3 "))
if temp_code == 1:
result = (9/5 * temp) + 32
elif temp_code==2:
result = (temp - 32) * 5/9
else:
print("Invalid code")

print("The temperature ", "is", result, "degrees.")


Program 3: Display pattern
Question:
Write a program to display the following patterns on the screen:
a) b)
A 12345

BC 1234

DEF 123

GHIJ 12

Code :
a) ch='A'
n=int(input("Enter n"))
for i in range(n):
for j in range(i):
print(ch,end=' ')
x=ord(ch)+1
ch=chr(x)
print()

b) n=int(input("Enter n"))
for i in range(n,0,-1):
for j in range(1,i+1):
print(j,end=' ')
Program 4 : SUM OF SERIES

Question:

Write a menu driven program to calculate and display the sum of the
following series
i. 1 + x1/1! + x2/2! + x3/3! +...........+ xn/n!
ii. x/1 – x2/2 + x3/3 – x4/4 +.............xn/n

Series - i
x=float(input("Enter x:"))
n=int(input("Enter n(the number of
terms):")) s=1

for i in range(1,n+1):
f=1
for j in range(1,i+1):
f*=j
s+=x**i/f
print("Sum=",s)

Series - ii
x=float(input("Enter x:"))
n=int(input("Enter n(the number of
terms):")) s=0
for i in range(1,n+1):
if i%2==0:
s-=x**i/i
else:
s+=x**i/i
Program 5 : Menu Driven – Perfect number and Armstrong
number
Question :
Write a menu driven program to determine whether the given number is a
palindrome, perfect number or an armstrong number .
(Armstrong number is a number that is equal to the sum of cubes of its
digits.)
Code:
def perfect():
h=int(input('Enter the number :'))
s=0
for x in range(1,h):
if h%x==0:
s=s+x
if s==h:
print(h,'is a perfect number ')
else:
print(h,'is not a perfect number ')

def armstrong():
m=int(input('Enter the number :'))
n=m
s=0
while
m:
r=m%10
s=s+r**3
m=m//10
if s==n:
print('Armstrong number ')
else:
print('Not an armstrong number ')
while True:
print('1 - Perfect number ')
print('2 - armstrong')
p
r
i
n
t
(
'
3

E
x
i
t

'
)
x=int(inpu
t('Enter
your
choice :'))
if x==1:
perfect()
elif x==2:
armstrong()
elif x==3:
break
else:
print('Invalid choice ')
Program 6: Fibonacci, Greatest Common Divisor

Question :
Write a menu driven program to display fibonacci, check if a number
is prime or not, and the gcd of two numbers
Code:
def fibo():
a,b=-1,1
n=int(input('How many terms
??')) print('Fibonacci series')
for i in
range(1,n+1):
c=a+b
print(c,end=' ')
a=b
b=c
def gcd():
a=int(input('Enter the first number :'))
b=int(input('Enter the second number :'))
while(b != 0):
c=b b=a
%b a=c
print('GCD is',a)

while True:
print('\n1 - Fibonacci ')
print('2 - Prime number')
print('3 - Exit ')
x=int(input('Enter your choice :'))
if x==1:
fibo()
elif
x==2:
gcd()
elif x==3:
break
else:
print('Invalid choice ')
Program 7: Prime and Number Palindrome
def palindrome():
num=int(input("Enter number"))
s=0
tnum=num
while tnum:
x=tnum%10
s=s*10+x
tnum//=10
if s==num:
print("It is a palindrome")
else:
print("It is not a palindrome")

def prime():
n=int(input('Enter the number to be checked '))
if n==1:
print(n," neither prime nor composite number")
else:
for i in range(2,n):
if n%i==0:
print(n," is a composite number")
break
else:
print(n," is a prime number")

while True:
print('\n1 - Palindrome
') print('2 - Prime
number') print('3 - Exit')
x=int(input('Enter your choice
:')) if x==1:
palindrome()
elif x==2:
prime()
elif x==3:
break
else:
print('Invalid choice ')
Program 8: String - 1

Question :

Input a string and check if it is palindrome, also change case, count the number
of characters in the given string, use title() and capitalize() functions
Code:
s=input("Enter a string")
while True:
opt=int(input("1.Palindrome 2.Change Case 3.Total
characters 4. title() 5. capitalize()
6.Exit"))
if opt==1:
if s==s[::-1]:
print(s,"is a Palindrome")
else:
print(s,"is not a Palindrome")
elif opt==2:
print("Changed case is:",s.swapcase())
elif opt==3:
print("Total characters are:",len(s))
elif opt==4:
print('title():',s.title())
elif opt==5:
print('Capitalize():',s.capitalize())
else:
break
Program 9 : String – 2

Question :

Write a Program to read a line and count how many times a specific word or a
character appears in the line and also the index positions where the word
appears in the given line and perform string built-in functions like i)startswith()
ii)endswith(),iii)find() and iv)replace() function.

def
Index_count(s,s2):
start=0
for i in s:
Index=s.find(s2,start)
if Index!=-1:
print(Index)
start+=Index+1
print("Count of ",s2," in ",s," is ",
s.count(s2)) def Startswith_Endswith(s1,s2):
if s.startswith(s1) and s.endswith(s2):
print(s," startswith ",s1," and ends with
",s2) else:
print(s," doesn't startswith ",s1," or ends
with ",s2)
def Find_Replace(s1,s2):
Index=s.find(s1)
if Index!=-1:
st=s.replace(s1,s2)
print(st)
while
True:
x=int(input("Enter option 1 - Input 2 - Indices
and count 3 - Startswith_Endswith 4-
Find_Replace 5 - Exit"))
if x==1:
s=input("Enter string")
s2=input("Enter substring")
elif x==2:
Index_count(s,s2)
elif x==3:
s1=input("Enter starts
with") s2=input("Enter ends
with ")
Startswith_Endswith(s1,s2)
elif x==4:
s1=input("Enter Find string")
s2=input("Enter Replace with string ")
Find_Replace(s1,s2)
elif x==5:
break
Program 10: String - 3

Question :
Write a program that takes a sentence as an input parameter where each
word in the sentence is separated by a space. The function should replace
each character by its next character, number by # and spaces by @.
Eg : If the given sentence is
Tokyo 2024
Then the output should be
Uplzp@####

Code:
def Change():
str2=''
for i in str1:
if i.isspace():
str2+='@'
elif i.isnumeric():
str2+='#'
else:
str2+=chr(ord(i)+1)

print('Original string :',str1)


print('Updated string
:',str2)

while True:
x=int(input("Enter 1 - Input string 2-Change
3-Exit")) if x==1:
str1=input("Enter
string") elif x==2:
Change()
elif x==3:
break
else:
print("Invalid code")
Program 12 : LIST – Methods
Question :
Input a list perform following methods of list.
Append, Extend, Insert,del,pop and remove

Code:
def Append_Extend():
#Appending
x=eval(input("Enter an element to append"))
lst.append(x)
print(lst)
#Extending
x=eval(input("Enter a list to extend"))
lst.extend(x)
print(lst)
#Inserting
def Insert():
y=input("Enter an element to insert")
x=int(input("Enter a position to insert"))
lst.insert(x,y)
print(lst)
#Different ways of deleting element from list
def Delete_Element():
i=int(input("Enter index to pop"))
print(lst.pop(i))
print("Popping last element",lst.pop())
x=input("Enter element to remove")
if x in lst:
lst.remove(x)
else:
print("Element not found")
start=int(input("Enter starting index"))
end=int(input("Enter end index"))
del lst[start:end]
print("Final List ",lst)
while True:
x=int(input("Enter
option 1 - Enter list 2 -
Append_Extend 3 -
Insert 4 - Delete Elemet
5 - Exit"))
if x==1:
lst=eval(input("Enter list"))
elif x==2:
Append_Extend()
elif x==3:
Insert()
elif x==4:
Delete_Element()
elif x==5:
break
else:
print("Invalid code")
Program 13 : LIST - LARGEST & SECOND LARGEST
Question : Accept a list of number and display the largest and second largest
numbers (do not use built-in function)

Code :

n=eval(input("Enter elements in the list "))


max1=0
max2=0
for i in range(len(n)):
if n[i]>max1:
max1=n[i]
for j in range(len(l)):
if n[j]>max2 and n[j]!=max1:
max2=n[j]
print("Second largest element is:",max2)
Program 14 - LIST – Left Shift , Right Shift

Write a menu driven program to perform the following manipulations on the list
 left shift the elements. Eg) INPUT-[1,2,3,4] OUTPUT-[2,3,4,1]
 right shift the elements. Eg) INPUT-[1,2,3,4] OUTPUT-[4,1,2,3]

def left_shift():
l=eval(input("Enter elements in the list "))
k=l[0] #store the first element
for i in range(0,len(l)-1):
l[i]=l[i+1]
l[len(l)-1]=k
print(l)

def right_shift():
l=eval(input("Enter elements in the list
")) k=l[-1] #store the last element
for i in range(len(l)-1,0,-
1): l[i]=l[i-1]
l[0]=k
print(l)

while True:
print("1.Left Shift\n2.Right Shift\n3.Exit")
ch=int(input("Enter menu choice:"))
if ch==1:
left_shift()
elif ch==2:
right_shift()
elif ch==3:
break
else:
print("Enter a valid choice")
Program 15 : EMPLOYEE - NESTED LIST

Question: Write a Python Program that creates a nested list with the
details of N employees such as (Empno, Empname, BasicPay] .
Calculate the following:
HRA = 40% of BasicPay #House Rent Allowance
DA=20% of BasicPay#Dearness Allowance
PF=15% of BasicPay#Provident Fund
N should be input from the user.
Finally it should print the payslip as follows:
EmpNo EmpName BasicPay HRA DA PF NetPay(BasicPay+ HRA +DA- PF)
For all employees.

Code :
def Create():
n = int(input("Enter the no. of employees"))
for i in range(n):
empno =int(input("Enter the employee number "))
empname = input("Enter the employee name:")
BasicPay = float(input("Enter the basic pay "))
HRA=BasicPay*.40
DA=BasicPay*.20
PF=BasicPay*.15
Net=BasicPay+HRA+DA-PF
lst.append([empno,empname,BasicPay,HRA,DA,PF,Net])
def Display():
print("Empno Empname BasicPay HRA DA PF Net")
for i in lst:
for j in i:
print(j,end=' ')
print()
lst=[]
Create()
Display()
Program 16: Tuple Manipulations

Question : Write a Python program to


- Create 2 tuples and swap them
- Compare the tuples using all relational operators
- Slice the tuple when start and end location is specified
- Search for a given element in the tuple and display the index
Code:
t1=()
t2=()
#store elements in 1st tuple
limit1=int(input("Enter limit for 1st tuple:"))
for i in range(limit1):
e_t1=eval(input("Enter elements in tuple1:"))
t1+=(e_t1,)
#store elements in 2nd tuple
limit2=int(input("Enter limit for 2nd tuple:"))
for i in range(limit2):
e_t2=eval(input("Enter elements in tuple2:"))
t2+=(e_t2,)

print("Before swapping")
print("1st tuple:",t1,"2nd tuple:",t2)
t1,t2=t2,t1 #swap
print("After swapping")
print("1st tuple:",t1,"2nd tuple:",t2)

#compare tuples using all relational operators


print("Equality",t1==t2)
print("Not Equal",t1!=t2)
print("Greater than or equal to",t1>=t2)
print("Less than",t1<t2)
#Slicing the tuple
n1=int(input('Enter the starting location to start slicing '))
n2=int(input('Enter the ending location to end slicing'))
print(t1[n1:n2])
#searching the element

n=eval(input('Enter the element to be searched in


tuple1:'))
f=0
for i in range(len(t1)):
if n==t1[i]:
print('Element ',n,'found at ',i,'index')
f=1
break
if f==0:
print('Element is not found ')
Program 17: Tuple – Creation
Question : Write a Python program to create a tuple t = (x,x**2,x**3,x**4,x**5)
where x is entered by the user. Also display the positive as well as negative
indices of the tuple
Code:

x=int(input("Enter x:"))
t=()
for i in
range(1,6):
t+=(x**i,)
print("Tuple is: ", t )
print("+ve INDEX\tELEMENT\t-ve INDEX")
for i in range(0,len(t)):
print(i,"\t\t",t[i],"\t\t",i-len(t))
Program 18: Dictionary – Creation and Search
Question : Write a Python program to create a dictionary by taking the values
as input from the user.Also display the record by record. Search for the student
details based on admission number and display the appropriate message.

Sample Dictionary: {101:['Gayathri','xi-c',85.5] , 102:['Jisha','xi-a',65], 103:['Hemali','xi-


c',52] }

Code:

d={}

n=int(input("Enter no.of entries:"))

def Create():

i=1

while i<=n:

admno=int(input("Enter admno:"))

nm=input("Enter name:")

clsec=input("Enter class & section:")

per=float(input("Enter percentage:"))

val=[nm,clsec,per]

d[admno]=val

i=i+1

print(d)

#to display the contents in a tabular form

def Display():
print("ADMNO\tNAME\tCLASS\tPERCENTAGE")

for i in d:
print(i,end="\t\t")

for j in d[i]:

print(j,end="\t\t")

print()

#to search based on admno

def Search():

ad=int(input("Enter admno to be searched for:"))

found=False

for i in d:

if

ad==i:

for j in d[i]:

print(j,end=" ")

found=True

if not found:

print("not found")

Create()

Display()

Search()
Program 19 : Dictionary – Frequency of character and word
Date :
Question : Write a Program to display the frequency of every character and
word in a sentence and
i) display the character as a key and the frequency as the value of a dictionary
ii) display the word as a key and the frequency as the value of a dictionary.

Code:
def Char():
for i in s:
chd[i]=s.count(i)
def Word():
l=s.split()
for i in l:
wd[i]=l.count(i)

s=input("Enter the sentence:\n")


wd={}
chd={}
Char()
Word()
print("Frequency of every character:",chd)
print("Frequency of every word:",wd)
Program 20 : Dictionary - Creation and Updation

Question : Write a program to create a dictionary with roll number, name and
marks of n students in a class and update the marks in such a way , if the marks
is greater than 75 , add 3 marks and print the names , otherwise add 2 marks.

Code:
dic={}
n=int(input('Enter the total number of students :'))
def Create():
for i in range(n):
rollno=int(input('Enter rollnumber
:')) name=input('Enter name :')
mark=float(input('Enter marks :'))
dic[rollno]=[name,mark]
def Print():
print('Students who had scored marks above 75')
for i in dic:
if dic[i][1]>75:
dic[i][1]+=3 print(dic[i]
[0])
else:
dic[i][1]+=2
print("Dictionary")
print(dic)

Create()
Print()

You might also like