[go: up one dir, main page]

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

Explaination Py

The document contains practical Python programming exercises focusing on SQLite database operations, exception handling, inheritance, and file reading. It includes code examples for creating a student database, handling user input with error checking, demonstrating inheritance and method overriding, and reading from a file while managing potential errors. Each section is accompanied by explanations and sample outputs to illustrate the concepts.

Uploaded by

Mgm Mallikarjun
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 views5 pages

Explaination Py

The document contains practical Python programming exercises focusing on SQLite database operations, exception handling, inheritance, and file reading. It includes code examples for creating a student database, handling user input with error checking, demonstrating inheritance and method overriding, and reading from a file while managing potential errors. Each section is accompanied by explanations and sample outputs to illustrate the concepts.

Uploaded by

Mgm Mallikarjun
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/ 5

Python Programming Lab (Practical)

Program 4 – SQLite Database Example

import sqlite3

conn = sqlite3.connect('student_data.db')
cursor = conn.cursor()

cursor.execute("""
CREATE TABLE IF NOT EXISTS students (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
marks INTEGER
)
""")

cursor.execute("INSERT INTO students (id, name, marks)


VALUES (1, 'Amit', 85)")
cursor.execute("INSERT INTO students (id, name, marks)
VALUES (2, 'Bhavna', 78)")
cursor.execute("INSERT INTO students (id, name, marks)
VALUES (3, 'Chirag', 92)")
conn.commit()

print("Before Modification:")
cursor.execute("SELECT * FROM students")
rows = cursor.fetchall()
for row in rows:
print(row)

cursor.execute("UPDATE students SET marks = 88 WHERE id =


2")
conn.commit()

print("\nAfter Modification:")
cursor.execute("SELECT * FROM students")
rows = cursor.fetchall()
for row in rows:
print(row)

conn.close()

Sample Output

Prof. Raju Vathari TSBAS Jamkhandi Page 1


Python Programming Lab (Practical)

Before Modification:
(1, 'Amit', 85)
(2, 'Bhavna', 78)
(3, 'Chirag', 92)

After Modification:
(1, 'Amit', 85)
(2, 'Bhavna', 88)
(3, 'Chirag', 92)

Explanation of Output

* Before modification: The table shows initial marks.


* After modification: Bhavna’s marks are updated from 78 →
88.

Program 5 – Exception Handling (User Input)

try:
number = int(input("Enter a number: "))
print("You entered:", number)
except ValueError:
print("Invalid input! Please enter a valid integer
number.")

Sample Outputs

Case 1 – Valid Input

Enter a number: 25
You entered: 25

Case 2 – Invalid Input

Enter a number: abc


Invalid input! Please enter a valid integer number.

Prof. Raju Vathari TSBAS Jamkhandi Page 2


Python Programming Lab (Practical)
Explanation of Output

* Valid input → Program prints the number.


* Invalid input → Program catches `ValueError` and shows a
friendly error message.

Program 6 – Inheritance and Method Overriding

class Rectangle:
def __init__(self, length, width):
self.length = length
self.width = width

def perimeter(self):
return 2 * (self.length + self.width)

class Box(Rectangle):
def __init__(self, length, width, height):
super().__init__(length, width)
self.height = height

def volume(self):
return self.length * self.width * self.height

def perimeter(self):
return 4 * (self.length + self.width + self.height)

box1 = Box(5, 3, 2)

print("Volume of box:", box1.volume())


print("Perimeter of box:", box1.perimeter())

Sample Output

Volume of box: 30
Perimeter of box: 40

Explanation of Output

* Volume → `5 * 3 * 2 = 30`
* Perimeter (sum of all edges for a box) → `4 * (5 + 3 + 2)
Prof. Raju Vathari TSBAS Jamkhandi Page 3
Python Programming Lab (Practical)
= 40`
* Shows how inheritance and method overriding work.

Program 7 – Reading File and Storing in Array

filename = 'sample.txt'
lines = []

try:
with open(filename, 'r') as file:
for line in file:
lines.append(line.strip())

print("File content stored in an array:")


for line in lines:
print(line)

except FileNotFoundError:
print(f"The file '{filename}' does not exist.")

Sample Output (Assuming `sample.txt` contains)

Hello World is fun


File handling example

Program Output

File content stored in an array:


Hello World is fun
File handling example

If file does not exist

The file 'sample.txt' does not exist.

Explanation of Output

Prof. Raju Vathari TSBAS Jamkhandi Page 4


Python Programming Lab (Practical)
* Reads each line from the file → stores in a list/array.
* `strip()` removes `\n`.
* Prints each line stored in the array.
* Handles missing file error gracefully.

Prof. Raju Vathari TSBAS Jamkhandi Page 5

You might also like