Python Programming
Python Programming
Write python program for calculator and store result in dictionary , in dictionary when user
selects add then key add will be added, when subtract is selected then key subtract is added
and output is stored in list tin appending mode if next time add is again selected.
Source Code:-
OUTPUT-
Experiment-2
Write Python program for check URL is present in string or not. import re
def Find(string):
regex = r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-
z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()
<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))"
url = re.findall(regex,string)return [x[0] for x in url]
OUTPUT-
Experiment-3
Write a program to implement single and multiple inheritance using python programming.
Source Code:-
OUTPUT:
INPUT:
class A:
def A(self):
print('This is class A.')
class B:
def B(self):
print('This is class B.')
class C(A,B):
def C(self):
print('This is class C which inherits features of both classes A and B.')
o = C()
o.A()
o.B()
o.C()
OUTPUT:
Experiment-4
Write Python program for addition of two matrix nested loop.
Source Code:-
X = [[1,2,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[9,8,7],
[6,5,4],
[3,2,1]]
result = [[0,0,0],
[0,0,0],
[0,0,0]]
for i in range(len(X)):
for j in range(len(X[0])):
result[i][j] = X[i][j] + Y[i][j]
for r in result:
print(r)
Output-
Experiment-5
Write a python program for addition of two matrix using numpy and zip().
Source Code:-
import numpy as np
P = np.array([[1, 2], [3, 4]])
Q = np.array([[4, 5], [6, 7]])
print("Elements of the first matrix")
print(P)
print("Elements of the second matrix")
print(Q)
print("The sum of the two matrices is")
print(np.add(P, Q))
Output:-
Output-
Experiment- 7
Write a python program to Implement Nested dictionary concept.
Source Code:-
Dict = { 'Dict1': { },
'Dict2': { }}
print("Nested dictionary 1-")
print(Dict)
Dict = { 'Dict1': {'name': 'Ali', 'age': '19'},
'Dict2': {'name': 'Bob', 'age': '25'}}
print("\nNested dictionary 2-")
print(Dict)
Dict = { 'Dict1': {1: 'G', 2: 'F', 3: 'G'},
'Dict2': {'Name': 'Geeks', 1: [1, 2]} }
print("\nNested dictionary 3-")
print(Dict)
Output-
Experiment-8
Write python program for implement operator overloading for + operator that can work on string
as well as list and dictionary.
class A:
def __init__(self, a):
self.a = a
def __add__(self, o):
return self.a + o.a
ob1 = A(1)
ob2 = A(2)
ob3 = A("Geeks")
ob4 = A("For")
print(ob1 + ob2)
print(ob3 + ob4)
# Actual working when Binary Operator is used.
print(A.__add__(ob1 , ob2))
print(A.__add__(ob3,ob4))
#And can also be Understand as :
print(ob1.__add__(ob2))
print(ob3.__add__(ob4))
Output-
Experiment- 9
Write Python program to read json file and print data on console.
Source Code:-
import json
employee ='{"id":"09", "name": "Kanhaiya", "department":"MCA"}'
employee_dict = json.loads(employee)
print(employee_dict)
print(employee_dict['name'])
Output-
Experiment-10
Python program for Binary Search (Recursive and Iterative).
Source Code:-
Output-
Experimet-11
Implement data-abstraction in python, try to create object of abstract class.
Source Code:-
Output-
Experiment-12
Write python program to maintain student’s database in a file that is student name, admission
no, subjects, marks. Initially there is only 3 subjects and 3 marks, further there 2 more subjects
are added , update the file
Source Code:-
class Student:
marks = []
def getData(self, rn, name, m1, m2, m3):
Student.rn = rn
Student.name = name
Student.marks.append(m1)
Student.marks.append(m2)
Student.marks.append(m3)
def displayData(self):
print ("Roll Number is: ", Student.rn)
print ("Name is: ", Student.name)
print ("Marks in subject 1: ", Student.marks[0])
print ("Marks in subject 2: ", Student.marks[1])
print ("Marks in subject 3: ", Student.marks[2])
print ("Marks are: ", Student.marks)
print ("Total Marks are: ", self.total())
print ("Average Marks are: ", self.average())
def total(self):
return (Student.marks[0] + Student.marks[1] +Student.marks[2])
def average(self):
return ((Student.marks[0] + Student.marks[1] +Student.marks[2])/3)
r = int (input("Enter the roll number: "))
name = input("Enter the name: ")
m1 = int (input("Enter the marks in the first subject: "))
m2 = int (input("Enter the marks in the second subject: "))
m3 = int (input("Enter the marks in the third subject: "))
s1 = Student()
s1.getData(r, name, m1, m2, m3)
s1.displayData()
Output-
Experiment-13
Python code to remove duplicate elements
Source Code:-
def Remove(duplicate):
final_list = []
for num in duplicate:
if num not in final_list:
final_list.append(num)
return final_list
duplicate = [2, 4, 10, 20, 5, 2, 20, 4]
print(Remove(duplicate))
Output-
Experiment-14
Implement method overloading for variable number of argument, suppose for addition of two
numbers , addition of 3 numbers, ……addition of 5 numbers, same function will take care of
result.
Source Code:-
Output-
Experiement-15
Write python program for design website login page, register page, student-detail page, library-
detail page.
Source Code:-
import tkinter as tk
import mysql.connector
from tkinter import *
def submitact():
user = Username.get()
passw = password.get()
print(f"The name entered by you is {user} {passw}")
logintodb(user, passw)
def logintodb(user, passw):
if passw:
db = mysql.connector.connect(host ="localhost",user = user,password = passw,db ="College")
cursor = db.cursor()
else:
db = mysql.connector.connect(host ="localhost",user = user,db ="College")
cursor = db.cursor()
savequery = "select * from STUDENT"
try:
cursor.execute(savequery)
myresult = cursor.fetchall()
for x in myresult:
print(x)
print("Query Executed successfully")
except:
db.rollback()
print("Error occurred")
root = tk.Tk()
root.geometry("300x300")
root.title("DBMS Login Page")
# Defining the first row
lblfrstrow = tk.Label(root, text ="Username -", )
lblfrstrow.place(x = 50, y = 20)
Username = tk.Entry(root, width = 35)
Username.place(x = 150, y = 20, width = 100)
lblsecrow = tk.Label(root, text ="Password -")
lblsecrow.place(x = 50, y = 50)
password = tk.Entry(root, width = 35)
password.place(x = 150, y = 50, width = 100)
submitbtn = tk.Button(root, text ="Login",
bg ='blue', command = submitact)
submitbtn.place(x = 150, y = 135, width = 55)
root.mainloop()
Output-