Introduction To Python
Introduction To Python
Python is known for its versatility. It can be used for small projects, as
well as large-scale applications. Additionally, Python has a rich
ecosystem of libraries and frameworks that further extend its
functionality. Libraries like numpy and pandas are extensively used for
data analysis, while frameworks like Django and Flask make it easy to
build web applications.
The CSV format is widely used due to its simplicity and ease of
integration with various applications. In Python, the csv module makes
it straightforward to handle CSV files by providing methods for reading
and writing the data as lists or dictionaries.
In essence, CSV files are a great choice for quick, simple data storage
and sharing, but their limitations must be considered when handling
more sophisticated data management tasks.
MODULES USED IN PYTHON
1. CSV Module
The csv.reader() function is used to read the records from the CSV file
and display them to the user.
The csv.writer() function is used to add new records to the CSV file or
overwrite the file with updated data when a record is edited or deleted.
2. input() Function
The input() function is built into Python and is used to prompt the user
for input from the command line or console. This function always
returns the input as a string, which can then be processed or converted
into other data types if needed.
3. print() Function
The print() function is another built-in Python function that outputs data
to the console. It is used to display information or feedback to the user
during the execution of the program. The print() function is commonly
used in interactive applications to guide users, display results, or
confirm actions.
Python provides the try and except blocks to handle exceptions (errors).
This allows the program to gracefully handle errors and avoid crashes,
instead of letting the program terminate unexpectedly.
SOURCE CODE
import csv
from datetime import datetime
# Display menu
def display_menu():
print("\nPersonalized To-Do List")
print("1. Add Task")
print("2. View All Tasks")
print("3. Edit Task")
print("4. Delete Task")
print("5. Filter Tasks by Priority")
print("6. Filter Tasks by Deadline")
print("7. Exit")
# Add a task
def add_task(filename):
task_name = input("Enter task name: ")
priority = input("Enter priority (High/Medium/Low): ").capitalize()
deadline = input("Enter deadline (YYYY-MM-DD): ")
try:
datetime.strptime(deadline, '%Y-%m-%d') # Validate date format
except ValueError:
print("Invalid date format. Please enter in YYYY-MM-DD.")
return
with open(filename, mode='a', newline='') as file:
writer = csv.writer(file)
writer.writerow([task_name, priority, deadline])
print("Task added successfully!")
# View all tasks
def view_tasks(filename):
print("\n--- To-Do List ---")
try:
with open(filename, mode='r') as file:
reader = csv.reader(file)
for row in reader:
print(f"Task: {row[0]}, Priority: {row[1]}, Deadline: {row[2]}")
except FileNotFoundError:
print("No tasks found. Add a task first!")
print("--- End of List ---")
# Edit a task
def edit_task(filename):
task_name = input("Enter the task name to edit: ")
updated = False
tasks = []
try:
with open(filename, mode='r') as file:
reader = csv.reader(file)
for row in reader:
if row[0].lower() == task_name.lower():
print(f"Editing task: {row[0]}")
row[1] = input(f"Enter new priority (current: {row[1]}): ").capitalize()
row[2] = input(f"Enter new deadline (current: {row[2]}): ")
updated = True
tasks.append(row)
except FileNotFoundError:
print("No tasks found. Add a task first!")
return
if updated:
with open(filename, mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerows(tasks)
print("Task updated successfully!")
else:
print("Task not found!")
# Delete a task
def delete_task(filename):
task_name = input("Enter the task name to delete: ")
tasks = []
deleted = False
try:
with open(filename, mode='r') as file:
reader = csv.reader(file)
for row in reader:
if row[0].lower() != task_name.lower():
tasks.append(row)
else:
deleted = True
except FileNotFoundError:
print("No tasks found. Add a task first!")
return
if deleted:
with open(filename, mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerows(tasks)
print("Task deleted successfully!")
else:
print("Task not found!")
# Filter tasks by priority
def filter_by_priority(filename):
priority = input("Enter priority to filter (High/Medium/Low): ").capitalize()
print(f"\n--- Tasks with {priority} Priority ---")
found = False
try:
with open(filename, mode='r') as file:
reader = csv.reader(file)
for row in reader:
if row[1] == priority:
print(f"Task: {row[0]}, Priority: {row[1]}, Deadline: {row[2]}")
found = True
except FileNotFoundError:
print("No tasks found. Add a task first!")
return
if not found:
print(f"No tasks found with {priority} priority.")
print("--- End of List ---")
# Filter tasks by deadline
def filter_by_deadline(filename):
deadline = input("Enter deadline to filter tasks before (YYYY-MM-DD): ")
try:
deadline_date = datetime.strptime(deadline, '%Y-%m-%d')
except ValueError:
print("Invalid date format. Please enter in YYYY-MM-DD.")
return
print(f"\n--- Tasks Due Before {deadline} ---")
found = False
try:
with open(filename, mode='r') as file:
reader = csv.reader(file)
for row in reader:
task_deadline = datetime.strptime(row[2], '%Y-%m-%d')
if task_deadline <= deadline_date:
print(f"Task: {row[0]}, Priority: {row[1]}, Deadline: {row[2]}")
found = True
except FileNotFoundError:
print("No tasks found. Add a task first!")
return
if not found:
print("No tasks found before the given deadline.")
print("--- End of List ---")
# Main function
def main():
filename = 'todo_list.csv'
while True:
display_menu()
choice = input("Enter your choice: ")
if choice == '1':
add_task(filename)
elif choice == '2':
view_tasks(filename)
elif choice == '3':
edit_task(filename)
elif choice == '4':
delete_task(filename)
elif choice == '5':
filter_by_priority(filename)
elif choice == '6':
filter_by_deadline(filename)
elif choice == '7':
print("Exiting the program. Goodbye!")
break
else:
print("Invalid choice! Please try again.")
if __name__ == "__main__":
main()
OUTPUT
1)ADDING TASKS TO THE CSV FILE
2) VIEWING ALL TASKS