Assignment3 MCA-306 Dakshay
Assignment3 MCA-306 Dakshay
Assignment 03
Subject: Programming with Python
1. Write a program to create an array and display the array items by Accessing individual element through
indexes and insert a new item before the second element in an existing array.
import array
now = datetime.now()
print("Current date and time:", now)
print("Current year:", now.year)
print("Month of year:", now.month)
print("Week number of the year:", now.strftime("%U"))
print("Weekday of the week:", now.strftime("%A"))
print("Day of year:", now.strftime("%j"))
print("Day of month:", now.day)
print("Day of week:", now.weekday())
3. Write a program to create a class and apply following concepts of Object Oriented Programming:
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def speak(self):
print("Dog barks")
d = Dog()
d.speak()
d.greet()
d.greet("Tom")
print(d + d)
try:
a = int(input("Enter numerator: "))
b = int(input("Enter denominator: "))
result = a / b
print("Result:", result)
except ZeroDivisionError:
print("Cannot divide by zero")
except ValueError:
print("Invalid input")
finally:
print("Execution completed")
5. Write a program for connecting to a Database. Perform insertion, deletion and updation operation in the
database.
import sqlite3
# Connecting to database
conn = sqlite3.connect('student.db')
cur = conn.cursor()
# Create table
cur.execute("CREATE TABLE IF NOT EXISTS student (id INTEGER PRIMARY KEY, name TEXT)")
# Insert
cur.execute("INSERT INTO student (name) VALUES ('Dakshay')")
# Update
cur.execute("UPDATE student SET name='Dakshay Kumar' WHERE id=1")
# Delete
cur.execute("DELETE FROM student WHERE id=1")
conn.commit()
conn.close()