0% found this document useful (0 votes)
1 views7 pages

Python Pbprograms

Uploaded by

Namirah Sakarde
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)
1 views7 pages

Python Pbprograms

Uploaded by

Namirah Sakarde
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

1.

Implement python script to remove duplicates from a list

# Remove duplicates from a list

list1 = [10, 20, 30, 20, 40, 10]


list2 = list(set(list1))
print("List after removing duplicates:", list2)

Output:
List after removing duplicates: [10, 20, 30, 40]

[Link] program to find repeated items in a tuple


t = (1, 2, 3, 2, 4, 5, 1, 6)
repeated = []
for i in t:
if [Link](i) > 1 and i not in repeated:
[Link](i)

print("Tuple:", t)
print("Repeated items:", repeated)

Output
Tuple: (1, 2, 3, 2, 4, 5, 1, 6)
Repeated items: [1, 2]

[Link] python script to check whether the given key already exists or not in a
dictionary

# Python program to check if a key exists in a dictionary

d={‘apple’:1,’banana’:2,’cherry’:3}
If ‘banana’ in d:
Print(“banana key exists”)
else:
Print (“banana key does not exist “)
If ‘orange’ in d:
Print(“orange key exists”)
else:
Print(“orange key does not exists”)

Output:
banana key exists
Orange key does not exists
4. Write a python script to implement Method overloading .
class Shopping:
def bill(self, *args):
# Single item purchase
if len(args) == 1:
print("Total Bill:", args[0])
# Two items purchase
elif len(args) == 2:
print("Total Bill:", args[0] + args[1])
# Multiple items purchase
elif len(args) > 2:
total = sum(args)
print("Total Bill:", total)
else:
print("No items purchased")
# Object creation
obj = Shopping()
# Method calls
[Link](500)
[Link](500, 300)
[Link](200, 300, 400, 100)

Output:

Total Bill: 500

Total Bill: 800

Total Bill: 1000

5. Create SQLite Database and perform Operations on Tables


import sqlite3
conn = [Link]('[Link]')
cursor = [Link]()
[Link]("DROP TABLE IF EXISTS STUDENT")
sql = '''CREATE TABLE STUDENT(NAME CHAR(20) NOT NULL ,REGNO INT PRIMARY
KEY,COURSE CHAR(20),AGE INT)'''
[Link](sql)
print("TABLE CREATED SUCCESSFULLY")
[Link]()
[Link]('''INSERT INTO STUDENT( NAME , REGNO , COURSE , AGE) VALUES
('BHARATH','21211044','BCA','18')''')
[Link]('''INSERT INTO STUDENT( NAME , REGNO , COURSE , AGE) VALUES
('BASAVA','21211045','BCA','21')''')
[Link]('''INSERT INTO STUDENT( NAME , REGNO , COURSE , AGE) VALUES
('SAJAY','21211054','BCA','21')''')
[Link]('''INSERT INTO STUDENT( NAME , REGNO , COURSE , AGE)
VALUES('CHARAN','21211013','BCA','21')''')
print("Records inserted.")
[Link]()
[Link]('''select * from STUDENT''')
result = [Link]();
print(result)
[Link]('''update student set AGE=19 where REGNO=21211044''')
[Link]('''DELETE FROM STUDENT WHERE AGE >20''')
print("contents of the table after delete operation ")
[Link]("SELECT * from STUDENT")
print([Link]())
[Link]()

Output:
TABLE CREATED SUCCESSFULLY
Records inserted.
[('BHARATH', 21211044, 'BCA', 18), ('BASAVA', 21211045, 'BCA', 21), ('SAJAY', 21211054,
'BCA', 21), ('CHARAN', 21211013, 'BCA', 21)]
contents of the table after delete operation
[('BHARATH', 21211044, 'BCA', 19)]

6. Create a GUI using Tkinter module.


from tkinter import *
win =Tk()
[Link]("300x200")
def blue():
[Link](bg="blue")
def red():
[Link](bg="red")
def pink():
[Link](bg="yellow")
Button(win, text="BLUE",command=blue).pack()
Button(win, text="RED",command=red).pack()
Button(win, text="YELLOW",command=yellow).pack()
[Link]()
7. Drawing Linechart and Bar chart using Matplotlib
import [Link] as plt
x=[1,2,3,4,5,6,7,8,9,10]
y=[10,22,34,45,22,56,43,87,64,73]
[Link](x=x,height=y,width=0.4)
[Link](x,(2001,2002,2003,2004,2005,2006,2007,2008,2009,2010))
y2=[i+2 for i in y]
[Link](x,y ,color='green')
[Link](x,y)
[Link]("stock price of ABC company")
[Link]('years')
[Link]('stock price')
[Link]()

output
8. Draw Histogram and pie chart using Matplotlib
a. Matplotlib
from matplotlib import pyplot as plt
[Link]("HISTOGRAM")
[Link]('x axis')
[Link]('y axis')
x = [300,400,500,1000,2000]
[Link](x,10)
[Link]()

Output:
B. Piechart
from matplotlib import pyplot as plt
import numpy as np
fig=[Link]()
ax=fig.add_axes([0,0,1,1])
[Link]('equal')
langs=['C','C++','Java','Python','PHP']
students=[23,17,35,29,12]
[Link](students,labels=langs,autopct=('%1.2f%%'))

9. program to create array using numpy and perform operations on array


import numpy as np
a=[Link]([10,20,30])
b=[Link]([2,3,4])
print('first array')
print(a)
print('Second array')
print(b)
print("Adding two arrays")
print([Link](a,b))
print("Subtracting two arrays")
print([Link](a,b))
print("Dividing two arrays")
print([Link](a,b))
print("reciprocal of arrays")
print([Link](b))
print("applying power function")
print([Link](a,2))
print("Applying mod function")
print([Link](a,b))
print("applying remainder function")
print([Link](a,b))
print("Adding elements in an array")
print([Link](a))
print("Finding mean in an array")
print([Link](a))
print("Finding average in an array")
print([Link](a))

Output:
first array
[10 20 30]
Second array
[2 3 4]
Adding two arrays
[12 23 34]
Subtracting two arrays
[ 8 17 26]
Dividing two arrays
[5. 6.66666667 7.5 ]
reciprocal of arrays
[0 0 0]
applying power function
[100 400 900]
Applying mod function
[0 2 2]
applying remainder function
[0 2 2]
Adding elements in an array
60
Finding mean in an array
20.0
Finding average in an array
20.0

You might also like