PYTHON 1 Merged
PYTHON 1 Merged
Write a program to be
29. provided code stub reads
two strings
Write a python program to
30. display elements from a
list, present at odd index
positions.
Write a program to be
31. provided code stub reads
and integer,n , from STDIN.
For all non- negative
integers i > n, print i
Develop a python program
32. to count the number of
vowels and consonants
from the given string
Write a python program to
33. print the matrix of given
inputs
RESULT:
Thus, the output has been successfully executed!
MODULE-1
EX: 1.2
Program:
a=int(input())
b=int(input())
print(a+b)
print(a-b)
print(a*b)
Ouput:
Module-1
Ex: 1.3
Correct the python program to print 'S' and 'k' using character
literal.
Aim:
To correct the python program to print 'S' and 'k' using character
literal.
Algorithm:
1. Declare ‘S’ and ‘k’ in different variables.
2. Print those
variables. Program:
v = "S"
w = "k"
print(v)
print(w)
Output:
Module –1
EX:1.4
Write a python program for the following problem statement.
Aim:
To write a python program for the following problem statement.
Algorithm:
1.Read two integers from STDIN and print three lines where:
2.The first line contains the sum of the two numbers.
3. The second line contains the difference of the two numbers (first - second).
4. The third line contains the product of the two
numbers. 5.Print the result.
Program:
a=int (input ())
b=int (input ())
c=a+b
d=a-b
e=a*b
print(c)
print(d)
print(e)
OUTPUT:
RESULT:
Thus, the output has been successfully executed!
Module –2
Ex:2.1
Construct a Python Program to find Factorial of the number '5' AIM: To construct
a Python Program to find Factorial of the number '5'
ALGORITHM:
Step1:Get two input from the user
Step2: if the input is equal to a%2==0 print EVEN
Step3: else print ODD
Step4: Execute the program.
PROGRAM:
fact=1
n=int(input())
for i in range (1,n+1):
fact=fact*i
print("Factorial of the given number",n,"is", fact)
OUTPUT:
RESULT:
RESULT:
Thus,the program has been successfully executed!
Module-2
EX:2.3
Write a lambda function which takes z as a parameter and returns z*11 using python.
AIM:
To write a lambda function which takes z as a parameter and returns z*11 using python.
ALGORITHM:
1. Declare a parameter as z.
2. Use lambda function by product of 11.
3. Print the
result. PROGRAM:
z=int(input())
mul=lambda z:z*11
print(mul(z))
OUTPUT:
RESULT:
Thus ,the output has been successfully executed!
Module –2
Ex:2.4
Python program to print alternate number pattern. Get the number of rows as input.
AIM:
To print alternate number pattern. Get the number of rows as input.
ALGORITHM:
1. Get the number of rows as input.
2. Analyze the no. of rows and columns to be executed!
3. Print the
result. Program:
n=int(input())
k=1
for i in range(1,n+1):
for j in range(1,i+1):
print(k,end=" ")
k+=2
print()
Output:
Result:
Thus, the output has been successfully executed!
Module –3
Ex: 3.1
Write a python function that accepts a string and converts it into upper case, lower case and title case.
AIM:
To write a python function that accepts a string and converts it into upper case, lower case and title case.
ALGORITHM:
TEST:
PROGRAM:
def modify(a):
b=a.upper()
c=a.lower()
d=a.title()
print(a)
print(b)
print(c)
print(d)
OUTPUT:
RESULT:
Thus,the output has been successfully executed.
MODULE –3
EX: 3(B)
Write a Python program to check that a string contains only a certain set of characters (in this case a-z, A-Z
and 0-9).
AIM:
To write a Python program to check that a string contains only a certain set of characters (in this case a-z, A-
Z and 0-9).
ALGORITHM:
RESULT:
Thus, the output has been successfully executed.
Module –3
EX.NO: 3C
Write a Python program to count the number of strings where the string length is 2 or more and the first and last
character are same from a given list of strings.
AIM:
To count the number of strings where the string length is 2 or more and the first and last character are same from a
given list of strings.
ALGORITHM:
1. Creating list for the following strings.
PROGRAM:
list1=['abd', '1221', 'abx',
'1221'] count=0
for i in list1:
if len(i) >= 2 and i[0]==i[-1]:
count += 1
print(count)
Output:
Result:
Thus, the output has been verified suucessfully.
Module –3
Ex: 3(D)
Write a python program to create the tuple by the multiples of 5 up to N. Get the N value
from the user.
AIM:
To write a python program and create the tuple by the multiple of 5 upto N and get the N
value from the user.
ALGORITHM:
1. Start the program.
2. Create a tuple by the multiple of 5 upto N.
3. Print the
result. PROGRAM:
a=int(input())
b=[]
for i in range(1,a):
if i%5==0:
b.append(i)
print(tuple(b))
OUTPUT:
RESULT:
Thus, the output has been successfully executed.
Module –4
Ex: 4(A)
Write a Python script to check whether a given key 5,9 already exists in a dictionary.
RESULT:
Thus , the output has been successfully executed.
MODULE-4
EX: 4(B)
Place result="You can't divide with 0" to the right place so that program avoids
ZeroDivisionError.
AIM:
To place the result ="You can't divide with 0" to the right place so that program avoids
ZeroDivisionError.
ALGORITHM:
1. Start the program.
2. Get the input from the user.
3. Use try method in such a way that it avoids ZeroDivisionError.
4. Use except method to print if ZeroDivisionError occurs.
5. Print the
result. PROGRAM:
a=int(input())
b=int(input())
try:
result=a/b
print(result)
except ZeroDivisionError:
print("You can't divide with 0")
OUTPUT:
RESULT:
Thus , the output has been successfully executed.
Module –4
Ex No. : 4(C)
Write a python program to Place msg="You're out of list range" to avoid IndexError
AIM:
Place msg="You're out of list range" to avoid IndexError
ALGORITHM:
Step1: put a list
Step2: try print(list(5))
Step3:except print that you are out of range
Step4:Execute the program.
PROGRAM:
lst=[5, 10, 20]
try:
print(lst[5])
except:
print("You're out of list range”)
OUTPUT:
RESULT:
Thus, the program has been successfully executed.
MODULE-4
EX.NO: 4(D)
Write Python Program to take the radius from the user and find the area of the circle using
class name 'cse' and function name 'mech'
AIM:
To write Python Program to take the radius from the user and find the area of the circle
using class name 'cse' and function name 'mech'.
ALGORITHM:
1. Start the program.
2. Create classes named as “cse” and “mech”
3. Give inputs from the user.
4. Print the
result. Program:
class cse:
radius=int(input())
area=3.1416*radius*radius
def mech():
cse.area
area=cse()
print("Area of circle:",cse.area)
area.mech
OUTPUT:
RESULT:
Thus, the output has been successfully executed.
Module-5
EX. NO:5(A)
Write a python code to create a student class with default constructor and a user defined function to
display the text "welcome" with student name given to the function.
AIM:
To write a python code to create a student class with default constructor and a user defined function to
display the text "welcome" with student name given to the function.
ALGORITHM:
1. Start the program.
2. Create a class named “Student”
3. Display the text “welcome”
4. Print the result.
PROGRAM:
class Student:
def init (self,name):
self.name=name
def display(self):
print("This is non parametrized constructor")
print("Hello",self.name)
name=input()
s=Student(name)
s.display()
OUTPUT:
RESULT:
Thus, the output has been successfully executed.
MODULE-5
EX. NO: 5(B)
Create a Python Class Student with a destructor.
AIM:
To Create a Python Class Student with a destructor.
ALGORITHM:
1. Start the program.
2. Create a constructor.
3. Create a destructor.
4. Stop the program.
PROGRAM:
class Student:
def init (self):
print("Inside Constructor")
print("Object initialized")
def name(self):
print("Hello, my name is Emma")
def del (self):
print("Inside destructor")
print("Object destroyed")
obj=Student()
obj.name()
del obj
OUTPUT:
RESULT:
Thus, the output has been successfully
executed. MODULE –5
EX. NO:5(C)
Write a Python Program to Display the Employee Details
EmpId , Emp Name.,
AIM:
PROGRAM:
class Det:
def init (self,name,id):
self.name=name
self.id=id
class Display(Det):
def view(self):
print((self.id,self.name))
name="Employee1"
id=198754
d=Display(name,id)
d.view()
OUTPUT:
RESULT:
Thus , the output has been successfully executed.
MODULE-5
EX. NO:5(D)
Write a Python program to Get the name, age and salary of a person and display using Multilevel inheritance.
AIM:
To write a Python program to Get the name, age and salary of a person and display using Multilevel inheritance.
ALGORITHM:
PROGRAM:
class Person:
self.personname=personname
class Age(Person):
self.age=age
class Salary(Age):
self.salary=salary
def print_name(self):
print(self.personname,self.age,self.salary)
personname="srinivas"
age=24
salary=23456
obj=Salary(personname,age,salary)
obj.print_name()
OUTPUT:
RESULT:
MODULE-6
EX. NO:6(A)
Create a parent class vehicle with the following methods show(),max_speed,change_gear()
inherit the car class from the vehicle class,where the car class methods
max_speed(),change_gear() override the same methods of base class using method
overriding
AIM:
To create a parent class inherit the car class from vehicle class with the following methods.
ALGORITHM:
1. Start the program
2. Create a parent class that inherit from vehicle class with the following
RESULT:
Thus the output has been succesfully executed.
MODULE –6
EX NO: 6(B)
Create two classes Employee and Admin. These two different classes have the same method name info(). This method contains
information(name, department) about employee in Employee class and admin in Admin class.after initializing classes, Create two objects for
respective classes. Then the method info() is called. Once by the object of Employee class and once by the object of Admin class.
AIM:
To Create two classes Employee and Admin. These two different classes have the same method name info(). This method contains
information(name, department) about employee in Employee class and admin in Admin class.after initializing classes, Create two objects for
respective classes.
ALGORITHM:
MODULE-6
EX.NO:6(C)
Create two classes “Cat” and “Cow”. They’re different animals and make different sounds.
So, the make_sound() function should produce two different outputs based on the objects
we pass through them. In this case, we have created two objects “cat1” and “cow1”.
AIM:
To Create two classes “Cat” and “Cow”. They’re different animals and make different
sounds. So, the make_sound() function should produce two different outputs based on the
objects we pass through them. In this case, we have created two objects “cat1” and “cow1”.
ALGORITHM:
1. Start the program
2. Creating classes CAT and COW with make_sound() function
3. Print the result
4. Stop the program
PROGRAM:
class Cat:
def init (self, name,
age): self.name = name
self.age = age
def info(self):
print(f"I am a cat. My name is {self.name}. I am {self.age} years old.")
def make_sound(self):
print("Meow")
class Cow:
def init (self, name,
age): self.name = name
self.age = age
def info(self):
print(f"I am a Cow. My name is {self.name}. I am {self.age} years old.")
def make_sound(self):
print("Moo")
cat1 = Cat("Kitty", 2.5)
cow1 = Cow("Fluffy", 4)
for animal in (cat1, cow1):
animal.make_sound()
animal.info()
animal.make_sound()
OUTPUT:
RESULT:
Thus, the output has been successfully executed.
MODULE –6
EX. NO: 6(D)
Create a Class Student with the private members name and age, Add getter and setter to initialize the age variable.
AIM:
To create a Class Student with the private members name and age, Add getter and setter to initialize the age variable.
ALGORITHM:
1. Start the program
2. Creating a class Student .
3. Include private members name,age.
4. Add getter and setter to initialize the age variable.
5. Print the result
6. Stop the program
PROGRAM:
class Student:
def init (self, name, age):
# private member
self.name = name
self. age = age
def get_name(self):
return self.name
def set_name(self,n):
self.name=n
def get_age(self):
return self. age
def set_age(self,a):
self. age=a
stud = Student('Jessa', 14)
print('Name:', stud.get_name(), stud.get_age())
stud.set_age(16)
print('Name:', stud.get_name(), stud.get_age())
OUTPUT:
RESULT:
Thus the output has been successfully executed
MODULE-7
EX. NO:7(A)
Write a Python program to find the sum of first n Natural Numbers
AIM:
To Write a Python program to find the sum of first n Natural Numbers
ALGORITHM:
1. Strat the program
2. Use recursive function to find sum of n natural numbers
3. Print the
result PROGRAM:
def Add(n):
if n==0:
return 0
else:
return n+Add(n-1)
n=int(input())
print("Result is",Add(n))
OUTPUT:
RESULT:
Thus the output has been successfully executed.
MODULE –7
EX. NO:7(B)
Write a Python program to print the sum of digits of a positive number using tail recursion
AIM:
To Write a Python program to print the sum of digits of a positive number using tail recursion
ALGORITHM:
1. Start the program.
2. Use tail recursion to get the sum of the digits of a positive number
3. Print the result
4. End the program.
PROGRAM:
def sum_(n):
if n==0:
return 0
elif n<0:
return "Not defined"
else:
return (n%10)+sum_(n//10)
n=int(input())
print(sum_(n))
OUTPUT:
RESULT:
Thus the output has been successfully executed.
MODULE-7
EX. NO:7(C)
Define the abstract base class named Polygon and also define the abstract method. This
base class inherited by the various subclasses. Implement the abstract method in each
subclass. Create the object of the subclasses and invoke the sides() method.
AIM:
To Define the abstract base class named Polygon and also define the abstract method. This
base class inherited by the various subclasses. Implement the abstract method in each
subclass. Create the object of the subclasses and invoke the sides() method.
ALGORITHM:
1. Start the program
2. Create a abstract class name Polygon
3. Define abstract method
4. Create classes and methods given in the question
5. Print the result
6. End the program
PROGRAM:
OUTPUT:
from abc import ABC
class Polygon(ABC):
def sides(self):
pass
class Triangle(Polygon):
def sides(self):
print("Triangle has 3 sides")
class Pentagon(Polygon):
def sides(self):
print("Pentagon has 5 sides")
class Hexagon(Polygon):
def sides(self):
print("Hexagon has 6 sides")
class square(Polygon):
def sides(self):
print("I have 4 sides")
t = Triangle()
t.sides()
s = square()
s.sides()
p = Pentagon()
p.sides()
k = Hexagon()
k.sides()
OUTPUT:
RESULT:
Thus the output has been successfully executed.
MODULE-7
EX. NO:7(D)
Create a Class Student with the private members name and age ,Add getter and
setter to initialize the age variable.
AIM:
To Create a Class Student with the private members name and age ,Add getter
and setter to initialize the age variable.
ALGORITHM:
1. Start the program.
2. Create a class name Student with private numbers name and age
3. Add get and setter to initialize the age variable
4. Print the result
5. End the
program PROGRAM:
class Student:
def init (self, name,
age): # private member
self.name = name
self. age = age
def
get_name(self):
return self.name
def set_name(self,n):
self.name=n
def get_age(self):
return self. age
def set_age(self,a):
self. age=a
stud = Student('Jessa', 14)
print('Name:', stud.get_name(), stud.get_age())
stud.set_age(16)
print('Name:', stud.get_name(),
stud.get_age()) OUTPUT:
RESULT:
Thus the output has been successfully executed.
MODULE- 8
EX : 8(A)
QUESTION :
The provided code stub reads two strings from STDIN, a and b
AIM:
To provided code stub reads two strings from STDIN, a and b. Add code to print
three lines where:
1. The first line contains the concatenation of the two strings.
2. The second line contains the repetition of the first string 3 times
ALGORITHM:
PROGRAM:
a=input()
b=input()
print(a+b)
print(a*3)
OUTPUT:
RESULT:
EX : 8(B)
QUESTION :
Write a python program to display elements from a list, present at odd index
positions.
AIM:
To construct a python program to display elements from a list, present at odd index
positions
ALGORITHM:
Step1 : Get two input from the user & append to an list
Step2 : a loop can be used to iterate over the elements Step3
: print only the odd numbers
Step4 : Execute the program.
PROGRAM:
size=int(input())
l=[]
for i in range(size):
a=int(input())
l.append(a)
for i in range(0,size):
if i%2!=0:
print(l[i],end=" ")
OUTPUT:
RESULT:
Thus the program has been successfully executed.
MODULE- 8
EX : 8(C)
QUESTION :
The provided code stub reads and integer,n , from STDIN. For all non-
negative integers i > n, print i3
AIM:
To construct python code stub reads and integer,n , from STDIN. For all non-
negative integers i > n, print i3
ALGORITHM:
PROGRAM:
n=int(input())
l=[]
for i in range(n):
l.append(i)
for i in l:
print(i**3)
OUTPUT:
RESULT:
EX : 8(D)
QUESTION :
Develop a python program to count the number of vowels and consonants from
the given string
AIM:
To construct a python program to count the number of vowels and consonants
from the given string
ALGORITHM:
PROGRAM:
def fun(s):
v,c=0,0
for i in s:
if i in ['A','E','I','O','U','a','e','i','o','u']:
v+=1
else:
c+=1
print("Number of Vowels:",v)
print("Number of Consonants:",c)
s=input()
OUTPUT:
RESULT:
EX : 9(A)
QUESTION :
Develop a python program to count the number of vowels and consonants from
the given string
AIM:
To construct a python program to count the number of vowels and consonants
from the given string
ALGORITHM:
PROGRAM:
def create_matrix(n,m):
M=[]
for i in range(n):
row=[]
for j in range(m):
x=int(input())
row.append(x)
M.append(row)
return M
r,c=input().split()
A=create_matrix(int(r),int(c))
B=create_matrix(int(r),int(c))
C=A+B
print(A)
print(B)
Print(c)
OUTPUT:
RESULT:
QUESTION :
Write a Python class program to generate all even numbers between 200 and 300 and store
in a list using list comprehension.
AIM:
To develop a Python program to generate all even numbers between 200 and 300 and store
in a list using list comprehension.
ALGORITHM:
PROGRAM:
class Generate:
def init__(self, first,d,last):
self.first = first
self.d = d
self.last=last
def Ap_generate(self):
L=[i for i in range(self.first,self.last+1,self.d)]
return L
Series = Generate(200,2,301)
print(Series.Ap_generate())
OUTPUT:
RESULT:
EX : 9(C)
QUESTION :
AIM:
ALGORITHM:
PROGRAM:
def create_matrix(n,m):
M=[]
for i in range(n):
row=[]
for j in range(m):
x=int(input())
row.append(x)
M.append(row)
return M
r,c=input().split()
r=int(r)
c=int(c)
A=create_matrix(r,c)
print(A)
T = [[r[i] for r in A] for i in range(len(A[0]))]
print(T)
OUTPUT:
RESULT:
EX : 9(D)
QUESTION :
AIM:
To develop a Python class to perform Insertion sort algorithm
ALGORITHM:
PROGRAM:
class Numbers:
def init__(self, N=0):
self.N = int(input())
def create_list(self):
self.L=[]
for i in range(self.N):
x=int(input())
self.L.append(x)
def sorting(self):
for i in range(1,len(self.L)):
key=self.L[i]
j=i-1
while j>=0 and key < self.L[j]:
self.L[j+1]=self.L[j]
j=j-1
self.L[j+1]=key
def print_List(self):
for i in range(self.N):
print(self.L[i])
L1=Numbers()
L1. create_list()
print('Before Sorting')
L1.print_List()
L1.sorting()
print('After Sorting')
L1.print_List()
OUTPUT:
RESULT:
EX : 10(A)
QUESTION :
Write a python program to push only vowels into the stack from the given
string
AIM:
To develop a python program to push only vowels into the stack from the given
string
ALGORITHM:
PROGRAM:
l=[ S]
s=input()
for i in s:
if i in ['a','A','e','i','o','u','E','I','O','U']:
l.append(i)
print("".join(l))
OUTPUT:
RESULT:
EX : 10(B)
QUESTION :
ALGORITHM:
PROGRAM:
def ispalidrone(s):
return s==s[::-1]
s=input()
ans= ispalidrone(s) if
ans:
print("Yes")
else:
print("No")
OUTPUT:
RESULT:
MODULE- 10
EX : 10(C)
QUESTION :
Write a python program to add only the even unique numbers using appendleft()
from n given numbers
AIM:
To develop a python program to add only the even unique numbers using
appendleft() from n given numbers
ALGORITHM:
PROGRAM:
OUTPUT:
RESULT:
EX : 10(D)
QUESTION :
Write a python program to get the 4 integer values from user and display the values
using multiprocessing library
AIM:
To develop a python program to get the 4 integer values from user and display
the values using multiprocessing library
ALGORITHM:
PROGRAM:
OUTPUT:
RESULT:
Thus the program has been successfully executed.
MODULE- 11
EX : 11(A)
QUESTION :
Write a function to traverse the linked list and display it in the following format.
AIM:
To develop a python program to traverse the linked list and display it in the
following format.
ALGORITHM:
PROGRAM:
class Node:
def init (self, data=None):
self.data = data
self.next = None
class SLinkedList:
def init (self):
self.head = None
def listprint(self):
printval = self.head
while printval is not None:
print (printval.data)
printval = printval.next
list = SLinkedList()
list.head = Node("Mon")
e2 = Node("Tue")
e3 = Node("Wed")
list.head.next = e2
e2.next = e3
list.listprint()
OUTPUT:
RESULT:
EX : 11(B)
QUESTION :
Write a function to traverse the linked list and display it in the following format.
AIM:
To develop a python program to traverse the linked list and display it in the
following format.
ALGORITHM:
PROGRAM:
class Node:
def init (self, data=None):
self.data = data
self.next = None
class SLinkedList:
def init (self):
self.head = None
def listprint(self):
printval = self.head
while printval is not None:
print (printval.data)
printval = printval.next
list = SLinkedList()
list.head = Node("Mon")
e2 = Node("Tue")
e3 = Node("Wed")
list.head.next = e2
e2.next = e3
list.listprint()
OUTPUT:
RESULT:
EX : 11(C)
QUESTION :
AIM:
ALGORITHM:
PROGRAM:
class Nodeq:
def init (self, data):
self.data = data
self.next = None
self.prev = None
class DoublyLinkedList:
def init (self):
self.head = None
def insert_beginning(self,data):
new_node = Nodeq(data)
if(self.head == None):
self.head = new_node
return
self.head.prev = new_node
new_node.next = self.head
self.head = new_node
def insert_end(self, new_data):
new_node = Nodeq(new_data) if
self.head is None:
new_node.prev = None
self.head = new_node
return
last = self.head
while last.next:
last = last.next
last.next = new_node
new_node.prev = last
def search(self,data):
temp = self.head
while temp:
if temp.data==data:
break
temp = temp.next
if temp==None:
print("The given data doesnot exist:")
return False
return True
Dllist = DoublyLinkedList()
Dllist.insert_beginning(2)
Dllist.insert_end(0)
Dllist.insert_end(1)
print(Dllist.search(0))
print(Dllist.search(3))
OUTPUT:
RESULT:
EX : 11(D)
QUESTION :
Write a python function to insert elements at the beginning of the doubly linked
list.
AIM:
To develop Type a python function to insert elements at the beginning of the doubly
linked list.
ALGORITHM:
PROGRAM:
class Node:
def init (self, data):
self.item = data
self.nref = None
self.pref = None
class DoublyLinkedList:
def init (self):
self.start_node = None
new_linked_list = DoublyLinkedList()
new_linked_list.insert_in_emptylist(40)
new_linked_list.insert_at_start(30)
new_linked_list.insert_at_start(20)
new_linked_list.insert_at_start(10)
new_linked_list.traverse_list()
OUTPUT:
RESULT:
EX : 12(A)
QUESTION :
AIM:
ALGORITHM:
PROGRAM:
stack = []
stack.append('a')
stack.append('b')
stack.append('c')
print('Stack after elements are pushed:')
print(stack)
OUTPUT:
RESULT:
EX : 12(B)
QUESTION :
Type a python code to insert 3 elements. Also check and print the index value of the
elements stored in the stack.
AIM:
To develop python code to insert 3 elements. Also check and print the index value
of the elements stored in the stack.
ALGORITHM:
PROGRAM:
stack = []
stack.append('a')
stack.append('b')
stack.append('c')
print('Initial stack: ' + str(stack))
for i in range(len(stack)):
print(i, end=" ")
print(stack[i])
OUTPUT:
EX : 12(C)
QUESTION :
Type a python code and display all the elements inserted and also display after
deleting the first element.
AIM:
To develop python code and display all the elements inserted and also display after
deleting the first element.
ALGORITHM:
PROGRAM:
queue = []
queue.append('a')
queue.append('b')
queue.append('c')
OUTPUT:
RESULT:
EX : 12(D)
QUESTION :
Type a python code to insert 3 players in the list. Also check and print the index
value of the players in the list.
AIM:
To develop python code to insert 3 players in the list. Also check and print the index
value of the players in the list.
ALGORITHM:
PROGRAM:
queue= []
queue.append('Player_1')
queue.append('Player_2')
queue.append('Player_3')
for i in range(len(queue)):
print(i, end=" ")
print(queue[i])
OUTPUT:
RESULT: