0% found this document useful (0 votes)
8 views3 pages

Assignment3 MCA-306 Dakshay

This document contains an assignment for a Programming with Python course, detailing five programming tasks. The tasks include creating and manipulating arrays, displaying date and time formats, implementing object-oriented programming concepts, handling exceptions, and performing database operations. Each task is accompanied by Python code examples demonstrating the required functionality.

Uploaded by

neetusharma3497
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)
8 views3 pages

Assignment3 MCA-306 Dakshay

This document contains an assignment for a Programming with Python course, detailing five programming tasks. The tasks include creating and manipulating arrays, displaying date and time formats, implementing object-oriented programming concepts, handling exceptions, and performing database operations. Each task is accompanied by Python code examples demonstrating the required functionality.

Uploaded by

neetusharma3497
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/ 3

Name: Dakshay

Roll No: 2724160001

Assignment 03
Subject: Programming with Python

Subject Code: MCA-306

Class/Semester: MCA - 3rd Semester

Session: 2024-2025 (Odd)

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

arr = array.array('i', [10, 20, 30, 40])


print("Original array:", arr)

# Accessing individual elements


print("First element:", arr[0])
print("Second element:", arr[1])

# Insert before second element


arr.insert(1, 15)
print("Updated array:", arr)

2. Write a program to display the various Date Time formats:

from datetime import datetime

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:

Inheritance, Function Overloading, Operator Overloading

class Animal:
def speak(self):
print("Animal speaks")

class Dog(Animal):
def speak(self):
print("Dog barks")

def greet(self, name=None):


if name:
print(f"Hello {name}")
else:
print("Hello")

def __add__(self, other):


return "Overloaded + operator"

d = Dog()
d.speak()
d.greet()
d.greet("Tom")
print(d + d)

4. Write a program for exception handling.

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()

You might also like