0% found this document useful (0 votes)
2 views30 pages

Python Programming Lab Exercises 2023

This document outlines a Python programming lab for B Sc V Semester students, detailing various exercises including functions for calculating factorials, generating Fibonacci sequences, and performing arithmetic operations. It also includes tasks on string manipulation, random number generation, and the use of classes to represent geometric shapes like circles and rectangles. Additionally, the lab covers regular expressions and database interactions, providing a comprehensive overview of practical Python applications.

Uploaded by

s37077352
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)
2 views30 pages

Python Programming Lab Exercises 2023

This document outlines a Python programming lab for B Sc V Semester students, detailing various exercises including functions for calculating factorials, generating Fibonacci sequences, and performing arithmetic operations. It also includes tasks on string manipulation, random number generation, and the use of classes to represent geometric shapes like circles and rectangles. Additionally, the lab covers regular expressions and database interactions, providing a comprehensive overview of practical Python applications.

Uploaded by

s37077352
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

Lab: Python Programming (NEP) 2023-2024 Class: B Sc V Semester

Index
[Link]. particular Page
number
Part B:

Write a Python function to calculate the factorial of a number


1
Write a Python to generate Fibonacci Sequence
2
Write a Python program to get the sum of digits of a non-negative integer
3
Write a Python program to create a module [Link] that contains
4
functions to perform basic arithmetic operations.
5 Write a python program to reverse a string without using built-in
functions
Write a python program to generate random numbers.
6

7 Write a python program to display Multiplication Tables


8 Demonstrate importing the math module and perform any five math
functions.
9 Write a Python class named Circle constructed by a radius and two
methods which will compute the area and the perimeter of a circle.
Write a Python class named Rectangle constructed by a length and width
10
and a method which will compute the area and perimeter of rectangle.
Part B:
Demonstrate usage of basic regular expression with match (), search (),
11
findall (), sub () and split ().
Find the largest and smallest element in the list
12
Demonstrate use of Dictionaries to store and retrieve contact information.
13
Create SQLite Database and Write a Python program to demonstrate
14
modification of an existing table data from SQLite Database
Write a python program that prompts the user for a number and handles a
15
“ValueError”
Inherit a class Box that contains additional method volume. Override the
16 perimeter method to compute perimeter of a Box.
Write a Python program to read a file line by line store it into an array.
17
Write a python program to create a class representing a basic bank
18 account class with deposit and withdrawal methods.
Design Student Registration form using any 5 widgets using Tkinter
19 Module.
Write a python program to create a GUI interface for temperature
20 converter using Tkinter

Dept of Computer Science, S.S.P.O Govt First Grade College Muddebihal-586212 Dist: Vijaypur
Lab: Python Programming (NEP) 2023-2024 Class: B Sc V Semester

Part – A

Dept of Computer Science, S.S.P.O Govt First Grade College Muddebihal-586212 Dist: Vijaypur
Lab: Python Programming (NEP) 2023-2024 Class: B Sc V Semester

1. Python function to calculate the factorial of a number

def factorial(n):

if n == 0:

return 1

else:

return n * factorial(n-1)

n=int(input("Input a number to compute the factorial : "))

print(factorial(n))

OUTPUT :

bsc-cs@bsccs-OptiPlex-3070:~/RRH$ python3 [Link]

enter the number for factorial 5

factorial of 5 is 120

bsc-cs@bsccs-OptiPlex-3070:~/RRH$ python3 [Link]

enter the number for factorial 6

factorial of 6 is 720

Dept of Computer Science, S.S.P.O Govt First Grade College Muddebihal-586212 Dist: Vijaypur
Lab: Python Programming (NEP) 2023-2024 Class: B Sc V Semester

2. Python to generate Fibonacci Sequence

def fibonacci(n):

if n<=1:

return n

else:

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

num=int(input("How many terms you want to display:"))

for i in range(num):

print(fibonacci(i)," ", end=" ")

OUTPUT:
bsc-cs@bsccs-OptiPlex-3070:~$ python3 [Link]
How many terms you want to display:6

0 1 1 2 3 5

Dept of Computer Science, S.S.P.O Govt First Grade College Muddebihal-586212 Dist: Vijaypur
Lab: Python Programming (NEP) 2023-2024 Class: B Sc V Semester

3. Python program to print sum of digits of non-negative integer number

num=int(input(“Enter a positive integer number : ”))

result=0

if num<0:

print("Entered negative number")

else:

while num>0:

digit=int(num%10)

result=result+digit

num=int(num/10)

print("sum is :",result)

OUTPUT :

bsc-cs@bsccs-OptiPlex-3070:~/RRH$ gedit sum_digitExp2.py

bsc-cs@bsccs-OptiPlex-3070:~/RRH$ python3 sum_digitExp2.py

Enter a positive integer number 1234

sum is : 10

Dept of Computer Science, S.S.P.O Govt First Grade College Muddebihal-586212 Dist: Vijaypur
Lab: Python Programming (NEP) 2023-2024 Class: B Sc V Semester

bsc-cs@bsccs-OptiPlex-3070:~/RRH$ python3 sum_digitExp2.py

Enter a positive integer number : -458

Entered negative number

4. Python program to create a module [Link] that contains functions


to perform basic arithmetic operations. Demonstrate importing the
module.

File name: [Link]

def add(a, b):


return a + b

def sub(a, b):


return a - b

def mul(a, b):


return a * b

def div(a, b):


return a / b

File name: [Link]

import Calculation
num1=int(input('Enter first number: '))
num2=int(input('Enter second number: '))

print("Addition=",[Link](num1, num2))
print("Subtraction=",[Link](num1, num2))
print("Multiplication=",[Link](num1, num2))
print("Division=",[Link](num1, num2))

Dept of Computer Science, S.S.P.O Govt First Grade College Muddebihal-586212 Dist: Vijaypur
Lab: Python Programming (NEP) 2023-2024 Class: B Sc V Semester

OUTPUT:

bsc-cs@bsccs-OptiPlex-3070:~/RRH$ python3 [Link]

Enter first number: 56

Enter second number: 12

Addition= 68

Subtraction= 44

Multiplication= 672

Division= 4.666666666666667

bsc-cs@bsccs-OptiPlex-3070:~/RRH$ python3 [Link]

Enter first number: 23

Enter second number: 5

Addition= 28

Subtraction= 18

Multiplication= 115

Division= 4.6

Dept of Computer Science, S.S.P.O Govt First Grade College Muddebihal-586212 Dist: Vijaypur
Lab: Python Programming (NEP) 2023-2024 Class: B Sc V Semester

5. Python program to reverse a string without using built-in functions

my_string=input("Enter the string to reverse")

print("Original String is :",my_string)

rev=""

for i in my_string:

rev=i+rev

print("Reversed string:",rev)

Output :

bsc-cs@bsccs-OptiPlex-3070:~/RRH$ python3 [Link]

Enter the string to reverse welcome

welcome

Original String is : welcome

Reversed string: emoclew

bsc-cs@bsccs-OptiPlex-3070:~/RRH$ python3 [Link]

Enter the string to reverse computer science

Dept of Computer Science, S.S.P.O Govt First Grade College Muddebihal-586212 Dist: Vijaypur
Lab: Python Programming (NEP) 2023-2024 Class: B Sc V Semester

computer science

Original String is : computer science

Reversed string: ecneics retupmoc

6. Python program to generate random numbers.

import random

randomlist = []

for a in range(0,4):

q=[Link](1,30)

[Link](q)

print("List of random numbers is :\n")

print(randomlist)

bsc-cs@bsccs-OptiPlex-3070:~/RRH$ python3 [Link]

List of random numbers is :

[8, 28, 24, 26]

bsc-cs@bsccs-OptiPlex-3070:~/RRH$ python3 [Link]

List of random numbers is :

[16, 29, 12, 2]

Dept of Computer Science, S.S.P.O Govt First Grade College Muddebihal-586212 Dist: Vijaypur
Lab: Python Programming (NEP) 2023-2024 Class: B Sc V Semester

bsc-cs@bsccs-OptiPlex-3070:~/RRH$ python3 [Link]

List of random numbers is :

[5, 15, 18, 19]

7. Python program to display Multiplication Tables

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

print("Multiplication Table for ",number)

for i in range(1,11):

print(number,"X",i,"=",number*i)

bsc-cs@bsccs-OptiPlex-3070:~/RRH$ python3 [Link]


Enter the number 6
Multiplication Table for 6
6X1=6
6 X 2 = 12
6 X 3 = 18
6 X 4 = 24
6 X 5 = 30
6 X 6 = 36
6 X 7 = 42
6 X 8 = 48
6 X 9 = 54
6 X 10 = 60

Dept of Computer Science, S.S.P.O Govt First Grade College Muddebihal-586212 Dist: Vijaypur
Lab: Python Programming (NEP) 2023-2024 Class: B Sc V Semester

8. Demonstrate importing the math module and perform any five math
functions.

import math

a = [Link](90)
print(r"sin of 45 =",a)

b = [Link](5)
print("Factorial of 5 is :",b)

c = [Link](12.6567)
print("[Link](12.6567) =",c)

d = [Link](12.4567)
print("[Link](12.4567) =",d)

e = [Link](5)
print("Exponent of 5 = ",e)

f = [Link](5)
print("log(5) = ",f)

Dept of Computer Science, S.S.P.O Govt First Grade College Muddebihal-586212 Dist: Vijaypur
Lab: Python Programming (NEP) 2023-2024 Class: B Sc V Semester

g = math.log10(5)
print("log10(5) = ",g)

h = [Link](2,6)
print("[Link](2,6) =",h)

i = [Link](625)
print("square root of 625 =",i)

output :
bsc-cs@bsccs-OptiPlex-3070:~/RRH$ python3 [Link]

sin of 45 = 0.8939966636005579
Factorial of 5 is : 120
[Link](12.6567) = 12
[Link](12.4567) = 13
Exponent of 5 = 148.4131591025766
log(5) = 1.6094379124341003
log10(5) = 0.6989700043360189
[Link](2,6) = 64.0
square root of 625 = 25.0

Dept of Computer Science, S.S.P.O Govt First Grade College Muddebihal-586212 Dist: Vijaypur
Lab: Python Programming (NEP) 2023-2024 Class: B Sc V Semester

9. Write a Python class named Circle constructed by a radius and two


methods which will compute the area and the perimeter of a circle.

class Circle:

def Area(self,r):
[Link]=r
r=3.142*[Link]*[Link]
print("Area of Circle=",r)
def Perimeter(self,r):
[Link]=r
p=2*3.142*[Link]
print("Perimeter of Circle=",p)
obj=Circle()
r=int(input("Enter radius for circle"))
[Link](r)
[Link](r)

OUTPUT :

bsc-cs@bsccs-OptiPlex-3070:~$ python3 [Link]


Enter radius for circle4
Area of Circle= 50.272
Perimeter of Circle= 25.136

Dept of Computer Science, S.S.P.O Govt First Grade College Muddebihal-586212 Dist: Vijaypur
Lab: Python Programming (NEP) 2023-2024 Class: B Sc V Semester

bsc-cs@bsccs-OptiPlex-3070:~$ python3 [Link]


Enter radius for circle 7
Area of Circle= 153.958
Perimeter of Circle= 43.988

10. Write a Python class named Rectangle constructed by a length and width
and a method which will compute the area and perimeter of rectangle
import math
class rectangle():
def __init__(self,breadth,length):
[Link]=breadth
[Link]=length
def area(self):
return [Link]*[Link]
def perimeter(self):
return 2*([Link]+[Link])
a=int(input("Enter length of rectangle: "))
b=int(input("Enter breadth of rectangle: "))
obj=rectangle(a,b)
print("Area of rectangle:",[Link]())
print("Area of rectangle:",[Link]())

print()

Output
bsc-cs@bsccs-OptiPlex-3070:~$ python3 [Link]

Enter length of rectangle: 5

Dept of Computer Science, S.S.P.O Govt First Grade College Muddebihal-586212 Dist: Vijaypur
Lab: Python Programming (NEP) 2023-2024 Class: B Sc V Semester

Enter breadth of rectangle: 6


Area of rectangle: 30
Area of rectangle: 22

Part – B

Dept of Computer Science, S.S.P.O Govt First Grade College Muddebihal-586212 Dist: Vijaypur
Lab: Python Programming (NEP) 2023-2024 Class: B Sc V Semester

1. Demonstrate usage of basic regular expression with match (), search (),
findall(), sub( ) and split( ).
import re
line="Python and Java supports OOPS concept and both are easy to learn"
#Regular Expression for Search()
r1=[Link](r'Java',line)
if r1:
print("Match is found")
else:
print("Match not found")
#Regular Expression for Search()
r3=[Link](r'OOPS',line)
if r3:
print("Match is found")
else:
print("Match not found")
r4=[Link](r'OOPS','Procedural',line)
print(r4)
r4=[Link](r'DS|CPP',line)
print(r4)

r5=[Link](r'a',line)
print(r5)

Dept of Computer Science, S.S.P.O Govt First Grade College Muddebihal-586212 Dist: Vijaypur
Lab: Python Programming (NEP) 2023-2024 Class: B Sc V Semester

OUTPUT:

bsc-cs@bsccs-OptiPlex-3070:~$ python3 [Link]

Match not found

Match is found

Python and Java supports Procedural concept and both are easy to learn

[]

['Python ', 'nd J', 'v', ' supports OOPS concept ', 'nd both ', 're e', 'sy to le', 'rn']

Dept of Computer Science, S.S.P.O Govt First Grade College Muddebihal-586212 Dist: Vijaypur
Lab: Python Programming (NEP) 2023-2024 Class: B Sc V Semester

2. Find the largest and smallest element in the list

L=[]
n=int(input("Enter the number of elements to be inserted into list: "))
for x in range(n):
element=int(input(f"enter the {x+1} element to be inserted into list:" ))
[Link](element)
smallest=L[0]
largest=L[0]
for i in range(n):
if smallest>L[i]:
smallest=L[i]
if largest<L[i]:
largest=L[i]
print("The smallest number in the list is : ", smallest)
print("The largest number in the list is : ", largest)

Output :
bsc-cs@bsccs-OptiPlex-3070:~$ python3 [Link]

Enter the number of elements to be inserted into list: 5


Enter the 1 element to be inserted into list:12
Enter the 2 element to be inserted into list:56
Enter the 3 element to be inserted into list:23
Enter the 4 element to be inserted into list:46
Enter the 5 element to be inserted into list:45
The smallest number in the list is : 12
The largest number in the list is : 56

Dept of Computer Science, S.S.P.O Govt First Grade College Muddebihal-586212 Dist: Vijaypur
Lab: Python Programming (NEP) 2023-2024 Class: B Sc V Semester

3. Demonstrate use of Dictionaries to store and retrieve contact information.

n = int(input("Enter how many names you want to enter: "))


# initialize empty dictionary
names={}
for i in range(n):
name=input("Enter name of friend: ")
number=input("Enter phone number: ")
#add name number to dictionary
names[name]=number
print(names)

#add new item


names["Arun"]="9877666234"
print("Modified dictionary ",names)

#delete an item
del names["abc"]

#modify first key value


for name in names:
names[name] = "9456356344"
break
#Dictionary after modifying first key value
print(“Dictionary after modifying first key value\n”)
print(names)

Dept of Computer Science, S.S.P.O Govt First Grade College Muddebihal-586212 Dist: Vijaypur
Lab: Python Programming (NEP) 2023-2024 Class: B Sc V Semester

Output :
bsc-cs@bsccs-OptiPlex-3070:~$ python3 [Link]

Enter how many names you want to enter: 2


Enter name of friend: abc
Enter phone number: 698678607
Enter name of friend: xyz
Enter phone number: 678656958
{'abc': '698678607', 'xyz': '678656958'}
Modified dictionary {'abc': '698678607', 'xyz': '678656958', 'Arun':
'9877666234'}
Dictionary after modifying first key value
{'xyz': '9456356344', 'Arun': '9877666234'}

Dept of Computer Science, S.S.P.O Govt First Grade College Muddebihal-586212 Dist: Vijaypur
Lab: Python Programming (NEP) 2023-2024 Class: B Sc V Semester

4. Write a python program that prompts the user for a number and handles
a “ValueError”

# A ValueError in Python is raised when a function receives an argument of the


correct #type but an inappropriate value. To handle it, you can use a try-except
block to catch the #error and handle it appropriately.

def square_root(n):
if n < 0:
raise ValueError('number must be a non-negative number')
return n ** 0.5

try:
x=int(input("Enter a number"))
y=square_root(x)
print(f"Square root of {x} is {y}")

except ValueError as e:
print(e)

Output :
bsc-cs@bsccs-OptiPlex-3070:~$ python3 [Link]
Enter a number -6
-6
number must be a non-negative number

bsc-cs@bsccs-OptiPlex-3070:~$ python3 [Link]


Enter a number 8
8
Square root of 8 is 2.8284271247461903

Dept of Computer Science, S.S.P.O Govt First Grade College Muddebihal-586212 Dist: Vijaypur
Lab: Python Programming (NEP) 2023-2024 Class: B Sc V Semester

5. Inherit a class Box that contains additional method volume. Override the
perimeter method to compute perimeter of a Box.
class Rectangle:
# define constructor with attributes: length and width
def __init__(self, length , width):
[Link] = length
[Link] = width

# Create Perimeter method


def Perimeter(self):
return 2*([Link] + [Link])

# Create area method


def Area(self):
return [Link]*[Link]

# create display method


def display(self):
print("The length of rectangle is: ", [Link])
print("The width of rectangle is: ", [Link])
print("The perimeter of rectangle is: ", [Link]())
print("The area of rectangle is: ", [Link]())
class Box(Rectangle):
def __init__(self, length, width , height):
Rectangle.__init__(self, length, width)
[Link] = height

# define Volume method


def volume(self):
return [Link]*[Link]*[Link]
def Perimeter(self):
return 4*([Link] + [Link]+[Link])

myRectangle = Rectangle(7 , 5)
[Link]()

Dept of Computer Science, S.S.P.O Govt First Grade College Muddebihal-586212 Dist: Vijaypur
Lab: Python Programming (NEP) 2023-2024 Class: B Sc V Semester

print("----------------------------------")
B1 = Box(7 , 5 , 3)
print("the Perimeter of Box is: " , [Link]())
print("the volume of Box is: " , [Link]())

Output :
bsc-cs@bsccs-OptiPlex-3070:~$ python3 [Link]

The length of rectangle is: 7


The width of rectangle is: 5
The perimeter of rectangle is: 24
The area of rectangle is: 35
----------------------------------
the Perimeter of Box is: 60
the volume of Box is: 105

Dept of Computer Science, S.S.P.O Govt First Grade College Muddebihal-586212 Dist: Vijaypur
Lab: Python Programming (NEP) 2023-2024 Class: B Sc V Semester

7. Demonstrate importing the module. Write a Python program to read a file


line by line store it into an array.

File 1 : [Link]

Welcome to Python Lab

Python is very user friendly programming Language

Python with compact code

This is an example to extract lines from file and store them in as array

File 2: [Link]

content_array=[]

with open("[Link]","r") as file:

lines=[Link]()

for i in lines:

content_array=[Link]() # this strip fn removes new line character from each


line

print(content_array)

OUTPUT :

bsc-cs@bsccs-OptiPlex-3070:~$ python3 [Link]

Welcome to Python Lab

Python is very user friendly programming Language

Python with compact code

This is an example to extract lines from file and store them in as array

Dept of Computer Science, S.S.P.O Govt First Grade College Muddebihal-586212 Dist: Vijaypur
Lab: Python Programming (NEP) 2023-2024 Class: B Sc V Semester

8. Write a python program to create a class representing a basic bank


account class with deposit and withdrawal methods.

class Bank_Account:
def __init__(self):
[Link]=0
print("Hello!!! Welcome to the Deposit & Withdrawal Machine")

def deposit(self):
amount=float(input("Enter amount to be Deposited: "))
[Link] += amount
print("\n Amount Deposited:",amount)

def withdraw(self):
amount = float(input("Enter amount to be Withdrawn: "))
if [Link]>=amount:
[Link]-=amount
print("\n You Withdrew:", amount)
else:
print("\n Insufficient balance ")

def display(self):
print("\n Net Available Balance=",[Link])

# Driver code

# creating an object of class


s = Bank_Account()

# Calling functions with that class object


[Link]()
[Link]()
[Link]()

Dept of Computer Science, S.S.P.O Govt First Grade College Muddebihal-586212 Dist: Vijaypur
Lab: Python Programming (NEP) 2023-2024 Class: B Sc V Semester

9. Write a Python GUI program to design Student Registration Form using


any 5 widgets.

Some of the common widgets used in Tkinter are :

o Frame : serves as a holding area for other widgets and serves as a container.
o Text : It enables us to display and alter text in a variety of styles and offers a
prepared text display.
o Label : Used to display text and images, but we are unable to interact with it.
o Button : Often used add buttons and we may add functions and methods to it.
o Entry : One-line string text can be entered into this widget.
o Labelframe : For intricate window layouts, this widget serves as a separator or
container.
o Listbox : It just has text elements, all of which are the same colour and font.
o Scrollbar: This gives a sliding controller.
o Canvas : Custom widgets can be implemented using the canvas widget.
o Scale : This widget offers graphical slider items that let us choose different scale
values.
o Radiobutton : Use a radio button to carry out one of several choices.
o Checkbox : Use a checkbox to implement on-off choices.
o Listbox : It just has text elements, all of which are the same colour and font.

How to start making a simple registration form using Tkinter :

o Step 1 : The first step is to import the tkinter module (using either tkinter import
* or just import tkinter).
o Step 2 : The primary window of the GUI programme was created.
o Step 3 : Include one or more widgets in the GUI programme (controls such as
buttons, labels, and text boxes, etc.).
o Step 4 : Enter the primary events to react to each event that the user has
triggered.

Dept of Computer Science, S.S.P.O Govt First Grade College Muddebihal-586212 Dist: Vijaypur
Lab: Python Programming (NEP) 2023-2024 Class: B Sc V Semester

import tkinter as tk
import [Link] as box

#creating window
base=[Link]()
[Link]('500x500')
[Link]("Registration Form")

#Label for Form Heading


label0 =[Link](base, text="Registration form",width=20,font=("bold", 20))
[Link](x=90,y=53)

# Label and Entry for student name


label1 =[Link](base, text="FullName",width=20,font=("bold", 10))
[Link](x=80,y=130)

entry_1 =[Link](base)
entry_1.place(x=240,y=130)

#Label and Entry for EmailID


labl_2 =[Link](base, text="Email",width=20,font=("bold", 10))
labl_2.place(x=68,y=180)

entry_02 =[Link](base)
entry_02.place(x=240,y=180)

#Label and Radiobutton for Gender


labl_3 =[Link](base, text="Gender",width=20,font=("bold", 10))
labl_3.place(x=70,y=230)

radio_var =[Link]()
radio1=[Link](base, text="Male",padx = 3, variable=radio_var, value=1)
radio2=[Link](base, text="Female",padx = 15, variable=radio_var, value=2)
[Link](x=235,y=230)
[Link](x=300,y=230)

Dept of Computer Science, S.S.P.O Govt First Grade College Muddebihal-586212 Dist: Vijaypur
Lab: Python Programming (NEP) 2023-2024 Class: B Sc V Semester

#Label and Entry for Age


labl_4 =[Link](base, text="Age:",width=20,font=("bold", 10))
labl_4.place(x=70,y=280)

entry_02 =[Link](base)
entry_02.place(x=240,y=280)

# combobox for Selection of class


class_label=[Link](base, text="Select the Class")
class_label.place(x=310,y=320)

combo1=[Link]()
[Link]("Select")
choice1=["BSc VI", "BSc IV","BSc II"]
class_dropdown=[Link](base, combo1,*choice1)
class_dropdown.place(x=310, y=340)

def exbutton():
[Link]("Message","Form submitted successfully")

bt=[Link](base,
text='Submit',width=20,bg='brown',fg='white',command=exbutton)
[Link](x=180,y=400)

# it will be used for displaying the registration form onto the window
[Link]()
print("Registration form is created seccussfully...")

Dept of Computer Science, S.S.P.O Govt First Grade College Muddebihal-586212 Dist: Vijaypur
Lab: Python Programming (NEP) 2023-2024 Class: B Sc V Semester

OUTPUT :

Dept of Computer Science, S.S.P.O Govt First Grade College Muddebihal-586212 Dist: Vijaypur
Lab: Python Programming (NEP) 2023-2024 Class: B Sc V Semester

10. Create SQLite Database and Write a Python program to demonstrate


modification of an existing table data from SQLite Database

import sqlite3
conn=[Link]('[Link]')

#create a cursor
c= [Link]()

#create a table
[Link]("""CREATE TABLE customers_table (first_name text,last_name
text,email text)""")

#insert values in the columns of the table


[Link]("INSERT INTO customers_table VALUES
('Snehal','Patel','snpatel12@[Link]')")
[Link]("INSERT INTO customers_table VALUES
('Akshay','Joshi','akjoshi@[Link]')")

#Printing all the values before altering the table


print("Table before using ALTER ..")
[Link]("SELECT * FROM customers_table")
print([Link]())

#Alter the table


[Link]("ALTER TABLE customers_table ADD COLUMN UserName
CHAR(25)")

#Print the table after altering


print("Table after using ALTER ..")
[Link]("SELECT * FROM customers_table")
print([Link]())

print("Command executed successfully...")


[Link]()
#close our connection
[Link]()

Dept of Computer Science, S.S.P.O Govt First Grade College Muddebihal-586212 Dist: Vijaypur

You might also like