Python Mca Lab
Python Mca Lab
(1)insert()
(2)remove()
(3)append()
(4)len()
(5)pop()
6)clear()
Algorithms:
PROGRAM
print("List",numbers)
numbers.remove(34)
print("after remove",numbers)
print("after insertion:",numbers)
numbers.append(32)
len(numbers)
print("lengnth:",numbers)
numbers.pop(2)
print("after pop",numbers)
numbers.clear()
OUTPUT
Algorithms :
PROGRAM
#create a dictionary
dict1[5]="VB"
print(dict1[2])
dict1[3]="oracle"
print(dict1)
# get() method
print(dict1.get(3))
# len() method
OUTPUT:
Java
oracle
Length : 5
Algorithm:
PROGRAM:
sum=float(num1)+float(num2)
min=float(num1)-float(num2)
mul=float(num1)*float(num2)
div=float(num1)/float(num2)
OUTPUT:
Algorithms:
words.sort()
print(word)
OUTPUT:
an
example
Hello
Is
This
3)A)Write a python program to construct the following pattern using nested
for loop?
*
**
***
****
*****
****
***
**
*
Aim: To write a python program to construct the pattern using nested for
loop
Algorithm:
Step 1: Start the program
There is a typical structure to print any pattern, i.e., the number of rows
and columns. We need to use two loops to print any pattern, i.e.,
use nested loops.
The outer loop tells us the number of rows, and the inner loop tells us the
column needed to print the pattern.
Accept the number of rows from a user using the input() function to
decide the size of a pattern.
Next, write an outer loop to Iterate the number of rows using a for
loop and range() function.
Use the print() function in each iteration of nested for loop to display the
symbol or number of a pattern (like a star (asterisk *) or number).
Add a new line using the print() function after each iteration of the outer
loop so that the pattern display appropriately
PROGRAM:
for I in range(num,0,-1):
for j in range(i):
print(‘*’,end=””)
print(“)
OUTPUT:
Enter the no.of inputs:5
*
**
***
****
*****
****
***
**
*
3)b)Write a python program, using for loop that prints out the decimal
equivalents of 1/2,1/3,1/4...1/10
PROGRAM:
1)for i in range(2,11):
print((1/i),end=" ")
2)a=[]
for i in range(2,11):
a.append(1/i)
print(a)
Output 1
Output 2
PROGRAM:
amt = 1 * days
amt = 5 * days
else:
print("Invalid")
OUTPUT
ALGORITHM:
Step 1: Start the program
# single inheritance
# Base class
class Parent:
def func1(self):
print("This function is in parent class.")
# Derived class
class Child(Parent):
def func2(self):
print("This function is in child class.")
# Driver's code
object = Child()
object.func1()
object.func2()
OUTPUT:
This function is in parent class.
This function is in child class.
Multiple Inheritance:
# Base class1
class Mother:
mothername = ""
def mother(self):
print(self.mothername)
# Base class2
class Father:
fathername = ""
def father(self):
print(self.fathername)
# Derived class
# Driver's code
s1 = Son()
s1.fathername = "RAM"
s1.mothername = "SITA"
s1.parents()
OUTPUT:
Father : RAM
Mother : SITA
Multilevel Inheritance :
# Base class
class Grandfather:
# Intermediate class
class Father(Grandfather):
def __init__(self, fathername, grandfathername):
self.fathername = fathername
# Derived class
class Son(Father):
def __init__(self, sonname, fathername, grandfathername):
self.sonname = sonname
def print_name(self):
print('Grandfather name :', self.grandfathername)
print("Father name :", self.fathername)
print("Son name :", self.sonname)
# Driver code
s1 = Son('Prince', 'Rampal', 'Lal mani')
print(s1.grandfathername)
s1.print_name()
Output:
Lal mani
Grandfather name : Lal mani
Father name : Rampal
Son name : Prince
ALGORITHM:
Step 3: When we call this function with a positive integer, it will recursively
call itself by decreasing the number.
Step 4: Each function multiplies the number with the factorial of the number
below it until it is equal to one.
Step 5:Our recursion ends when the number reduces to 1. This is called the
base condition.
PROGRAM
def factorial(x):
if x==1:
return 1
else:
return(x*factorial(x-1))
num=3
OUTPUT:
The factorial of 3 is 6
Algorithms:
PROGRAM:
class abc:
def f(x,y):
return pow(x,y)
print(abc.f(2,3))
OUTPUT:
Algorithms:
Program:
file = open("trial.txt", "r")
number_of_lines = 0
number_of_words = 0
number_of_characters = 0
number_of_lines =number_of_lines +1
line = line.strip("\n")
print(line)
words = line.split()
number_of_words += len(words)
number_of_characters += len(line)
file.close()
OUTPUT:
Hello World
Hello Again
Goodbye
lines: 3 words: 5 characters: 29
Algorithms:
program:
import datetime
now = datetime.datetime.now()
print(now.strftime("%Y-%m-%d %H:%M:%S"))
print(now.strftime("%c"))
OUTPUT:
Current date and time:
2023-05-3111:05:20
31/05/202311:52:20AM
Wed,May31,2023
05/31/23 11:52:20
1.create
2.insert
3.select
4.delete operation
Aim:To write a python program using database connection the SQL query
Algorithms:
Program:
import sqlite3
# connect to the database
conn = sqlite3.connect("test.db")
cursor = conn.cursor()
print(cursor.rowcount,"record inserted.")
print(cursor.rowcount,"record inserted.")
conn.commit()
print(cursor.rowcount,"record inserted.")
myresult = cursor.fetchall()
for x in myresult:
print(x)
# delete a student
conn.commit()
print(cursor.rowcount,"record(s) deleted.")
cursor.execute("SELECT * FROM comp5")
myresult = cursor.fetchall()
for x in myresult:
print(x)
conn.close()
OUTPUT:
>>>
1 record inserted.
1 record inserted.
1 record inserted.
1 record(s) deleted.
>>>
10)Write a python program to implement digital clock using GUL
Algorithm:
Program:
import tkinter as tk
import time
def update_clock():
current_time = time.strftime('%H:%M:%S')
clock_label.config(text=current_time)
clock_label.after(1000, update_clock)
root = tk.Tk()
root.title("Digital Clock")
root.geometry("300x150")
clock_label.pack(expand=True)
update_clock()
root.mainloop()
OUTPUT: