Practical No : 1
Read
#write a program to demonstrate various file mode in python
#1 using read mode
#2 using write mode
#3 using append mode
# Sample
#For read
f = open('file.txt','r') SAMPLE FILE DATA: HELLO
data = f.read() SAURABH
print(data)
f.close()
OUTPUT:
HELLO SAURABH
Write
#sample
f = open('file.txt','w')
f.write("saurabh")
f.close()
OUTPUT:
saurabh
Append
#Sample
f = open('file.txt','a')
f.write("saurabh")
f.close()
OUTPUT:
Saurabh saurabh
Practical No 2-1
#Write a program to demonstrate use of regular expression in python
#Find all function using RegEx
import re
txt = "on the horizon"
x = re.findall("on",txt)
print(x)
Output:
['on', 'on']
P-2-2 SEARCH FUNCTION
import re
txt = "Hello python"
x = re.search("Hello",txt)
print("The on word is located in position: ",x.start())
Output:
The on word is located in position: 0
P-2-3 SPLIT FUNCTION
import re
txt = "Hello python"
x = re.split("\s",txt)
print(x)
output:
['Hello', 'python']
Practical no - 3
1.Write a program to demonstrate concept of date time and calendar
A . Write a program to print yesterday,today,tomorrow.
import datetime
today = datetime.date.today()
yesterday = today - datetime.timedelta(days = 1)
tomorrow = today + datetime.timedelta(days = 1)
print("yesterday: ",yesterday)
print("today : ",today)
print("tomorrow: ",tomorrow)
Output:
yesterday: 2024-01-10
today : 2024-01-11
tomorrow: 2024-01-12
B.Write a python program to add 5 sec to the current time:
import datetime
x = datetime.datetime.now()
y = x + datetime.timedelta(0.5)
print(x.time())
print(y.time())
Output:
11:27:16.611091
23:27:16.611091
C. write a program to display a python calendar
Given month november 2024
import calendar
year = 2024
month = 11
print(calendar.month(year,month))
Output:
November 2024
Mo Tu We Th Fr Sa Su
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30
Practical no - 4
Write a python program to demonstrate different types exception handling
1. Exception handling usingTry except block
try:
num1 = int(input("Enter a first number: "))
num2 = int(input("Enter a second number: "))
result = num1/num2
print(result)
except:
print("Error : Denominator cannot be 0 ")
Output:
Enter a first number: 45
Enter a second number: 12
3.75
2. Catching specific exception in python
try:
numbers = [1,3,5,7]
print(numbers[4])
except ZeroDivisionError:
print("Error : Denominator cannot be 0")
except IndexError:
print("Index out of bound")
Output:
Index out of bound
3. Try with else clause
#Write a program to reciprocal of even numbers
try:
num = int(input("Enter a number: "))
assert num % 2 == 0
except:
print("Not an even numbers")
else:
reciprocal = 1/num
print("reciprocal of given number :", reciprocal)
Output:
Enter a number: 12
reciprocal of given number : 0.08333333333333333
4. Try Finally block
try:
num1 = int(input("Enter a first number: "))
num2 = int(input("Enter a second number: "))
result = num1/num2
print(result)
except:
print("Error : Denominator cannot be 0 ")
finally:
print("This is finally block ")
Output:
Enter a first number: 10
Enter a second number: 12
0.833333333333333
This is finally block
Practical no - 5
#write a program to work with data bases in python to perform
operations such as
#a connecting to databases
#b creating and dropping tables
#c inserting and upadating into tables
Write a program to work with databases in python to perform
operations such as
A: Connecting to databases
B. Creating and dropping tables
C. Inserting and updating into tables
A:
import sqlite3
conn = sqlite3.connect('FYCS.db')
cursor = conn.cursor()
conn.commit()
conn.close()
Output:
Connecting to database.py
B:
import sqlite3
#a
conn = sqlite3.connect('example.db')
cursor = conn.cursor()
#b
cursor.execute('''CREATE TABLE IF NOT EXISTS users(id INTEGER PRIMARY
KEY, age INTEGER)''')
cursor.execute("INSERT INTO users (name, age) VALUES (?, ?)",
('John Doe', 25))
cursor.execute("INSERT INTO users (name, age) VALUES (?, ?)",
('Jane Smith',30))
cursor.execute("UPDATE users SET age = 26 WHERE name = 'John Doe'")
cursor.execute('SELECT * FROM users')
print("Data after updating John Doe's age:")
print(cursor.fetchal())
cursor.execute("DROP TABLE IF EXITS users")
conn.commit()
conn.close()
Practical no 6
Write a program to demonstrate concept of threading and
multitasking in python