0% found this document useful (0 votes)
0 views14 pages

Python Lab Programs

The document contains various Python scripts demonstrating different programming concepts, including removing duplicates from a list, creating tuples, set operations, Fibonacci series generation, recursion for factorial, exception handling, palindrome checking, class variable access, method overloading, single inheritance, prime number generation, palindrome number checking, sorting tuples, concatenating dictionaries, and method overriding. Each script is accompanied by a brief description and expected output. The examples serve as practical exercises for learning Python programming.

Uploaded by

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

Python Lab Programs

The document contains various Python scripts demonstrating different programming concepts, including removing duplicates from a list, creating tuples, set operations, Fibonacci series generation, recursion for factorial, exception handling, palindrome checking, class variable access, method overloading, single inheritance, prime number generation, palindrome number checking, sorting tuples, concatenating dictionaries, and method overriding. Each script is accompanied by a brief description and expected output. The examples serve as practical exercises for learning Python programming.

Uploaded by

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

1 . Implement python script to remove duplicates from a list.

PROGRAM:

l=list(input("Enter list values : ").split())

l1=[]

for i in l:

if i not in l1:

l1.append(i)

print("List after deleting duplicate values is ")

for i in l1:

print(i,end=' ')

OUTPUT:

2. Implement python script to create a tuple with different data types.

PROGRAM:

n=input(" Enter Your Name : ")

m=int(input("Enter marks in Python Subject : "))

res=bool(input(" Passed in the subject ( Y/N) : "))

a=float(input(" Enter your Average Marks : "))

t=(n,m,res,a)

print("The tuple With Different types is : ",t)

OUTPUT

N KAVYA
3. Implement python script to perform union, Intersection, Difference

of given two sets

PROGRAM:

s1=set(input("Enter the elements of the first set :").split())

s2=set(input("Enter the elements of the second set :").split())

print("elements of the first set :",s1)

print("elements of the second set :",s2)

print("Union of (S1,S2) is :",s1|s2)

print("Intersection of s1&s2 is :",s1.intersection(s2))

print("Difference of s1-s2 is :",s1-s2)

print("Symmetric Difference of (s1,s2) is :",s1^s2)

OUTPUT :

N KAVYA
4. Define a function, which generates Fibonacci series up to n numbers.

PROGRAM:

def fib(n):

if n==0 or n==1:

return n

return fib(n-1)+fib(n-2)

n=int(input('enter the No.Of terms : '))

print(' Fibonacci series upto %d terms'%n)

for i in range(n):

f=fib(i)

print(f,end=' ')

OUTPUT

5. Implement a python script for factorial of number by using

recursion.

PROGRAM:

def fact(n):

if n==0:

return 1

return n*fact(n-1)

n=int(input(" Enter the number : "))

f=fact(n)

print(" Factorial of %d = %d"%(n,f))

N KAVYA
OUTPUT

6. Implementation of Python script to handle simple errors by using

exception-handling mechanism.

PROGRAM:

try:

a=int(input(" Enter First Value : "))

b=int(input(" Enter the second Value : "))

print(" Sum = ",a+b)

print(" Quotient = ",a//b)

print(" Average = ",(a+b)/2.0)

except ZeroDivisionError:

print(" Division is not possible because the denominator is Zero

")

except ValueError:

print(" please provide integer value only")

else:

print(" Operation Completed Without Exception ")

try:

print(" Difference = ",a-b)

print(" Product = ",a*b)

except NameError as msg:

print(" Error is : ",msg)

finally:

N KAVYA
print(" Possible Arithmetic operations completed ")

OUTPUT :

7. Implementation of python Script to check given string is palindrome

or not.

PROGRAM:

Python Programming Lab Manual

53

s=input("Enter the String : ")

s1=s[::-1]

print(" Reverse of string is : ",s1)

if s==s1:

print(s," is a Palindrome String")

else:

print(s," is Not a Palindrome String")

OUTPUT:

N KAVYA
8.Write a python script to Create &access class variables and methods

Program:

class Robot:

population=0

what="Humanoid Robot"

version=1.0

speed="1THZ"

memory="1ZB"

def _init_(self, name):

self.name = name

print('\n < Initialising ',self.name,' > ')

Robot.population += 1

def _del_(self):

print(self.name , ' is being destroyed!\n')

Robot.population -= 1

if Robot.population == 0:

N KAVYA
print(self.name ,' was the last one.')

else:

print(' \n There are still ', Robot.population ,' robots working.')

def update(cls):

cls.version=2.0

cls.speed = "2THZ"

cls.memory="2ZB"

def sayHi(self):

print("\nHai i am a : ",self.what)

print("My Name is : ",self.name)

print("Version : ",self.version)

print("Speed : ",self.speed)

print("Memory : ",self.memory)

def howMany():

print('\n No of Robots Available curruently : ',Robot.population)

d=Robot('Chitti')

d.sayHi()

Robot.howMany()

d1=Robot('Vennela')

d1.update()

d1.sayHi()

Robot.howMany()

print("\nRobots can do some work here.\n")

print("\nRobots have finished their work. So let's destroy them.\n")

del d

del d1

Robot.howMany()

N KAVYA
Output:

9. Write a python script to implement method overloading

Program:

from multipledispatch import dispatch

Python Programming Lab Manual

61

@dispatch(int,int)

def product(first,second):

result = first*second

print(" Product of two integers = ",result)

@dispatch(int,int,int)

def product(first,second,third):

N KAVYA
result = first * second * third

print(" Product of three integers = ",result)

@dispatch(float,float,float)

def product(first,second,third):

result = first * second * third

print(" Product of three floats = ",result)

product(2,3)

product(2,3,2)

product(2.2,3.4,2.3)

Output

10.Write a python script to implement single heritance

Program:

Python Programming Lab Manual

62

class Parent:

def __init__(self,fname,fage):

self.firstname=fname

self.age=fage

def view(self):

print(self.firstname , self.age)

class Child(Parent):

def __init__(self , fname , fage):

N KAVYA
Parent.__init__(self, fname, fage)

self.lastname="Internet"

def view(self):

print("course name" , self.firstname ,"first came",

self.age , " years ago." , self.lastname, " has courses to master

python")

ob = Child("Python" , '28')

ob.view()

Output:

11. Implement python script to print all the prime numbers with in the

given range

PROGRAM:

n1,n2=input("Enter the range:").split()

n1=int(n1)

n2=int(n2)

print("prime numbers in the range",n1,"to",n2,"are")

for n in range(n1,n2+1):

flag=1

for i in range(2,n//2+1):

r=n%i

if(r==0):

flag=0

N KAVYA
break

if flag==1:

print(n, end=" ")

OUTPUT

12.Implement the python script to check weather the given number is

palindrome or not

PROGRAM:

n=int(input("enter the number"))

rev=0

m=n

while n!=0:

r=n%10

rev=rev*10+r

n=n//10

if rev==m:

print(m, "is a palindrome")

else:

print(m, "is not a palindrome")

OUTPUT:

N KAVYA
13.Implement python script to sort a tuple by its float element.

PROGRAM:

Python Programming Lab Manual

26

n=int(input("Enter number of tuple to create : "))

l=[]

for i in range(n):

print("Enter Elements of %d tuple : "%(i+1),end=' ')

t=tuple(input().split())

l.append(t)

print(sorted(l, key = lambda x: float(x[1]), reverse = True))

OUTPUT:

14.Implement python script to concatenate following dictionaries to

N KAVYA
create new one.

Sample Dictionary: dic1={1:10,2:20} dic2={3:30,4:40}

dic3={5:50,6:60}

Expected Result: {1:10, 2:20,3:30, 4:40, 5:50, 6:60}

PROGRAM:

d1={1:10, 2:20}

d2={3:30, 4:40}

d3={5:50, 6:60}

d={}

for i in (d1,d2,d3):

d.update(i)

print(" Dictionary1 : ",d1)

print(" Dictionary2 : ",d2)

print(" Dictionary3 : ",d3)

print(" The Concatenated Dictionary is : ",d)

OUTPUT

15.Write a python script to implement method overriding

Program:

class Bank:

def getroi(self):

return 10

Python Programming Lab Manual

N KAVYA
63

class SBI(Bank):

def getroi(self):

return 7

class ICICI(Bank):

def getroi(self):

return 8

class Axis(Bank):

def display(self):

print("Axis Bank Rate of interest:",self.getroi())

b1 = Bank()

b2 = SBI()

b3 = ICICI()

b4 = Axis()

print("Bank Rate of interest:",b1.getroi())

print("SBI Rate of interest:",b2.getroi())

print("ICICI Rate of interest:",b3.getroi())

b4.display()

Output:

N KAVYA

You might also like