Python To-Do List Script Example
This beginner-friendly project shows how to build a simple command-line to-do list script in Python.
It is a good example because it combines several core Python skills in one small program:
- lists
- loops
input()ifstatements- functions like
append()andpop()
If you are still learning these basics, this example helps you see how they work together in a real script.
What this script does
This program:
- Creates a simple text-based to-do list
- Lets the user view tasks, add tasks, and remove tasks
- Stores tasks in a Python list
- Runs in a loop until the user chooses to quit
Skills this example teaches
By reading and running this script, you practice:
- How to store items in a list
- How to use
input()to get user choices - How to use
while Truefor a menu loop - How to use
if,elif, andelseto handle options - How to use
append()andpop()with lists - How to display numbered items with
enumerate()
If you need more background first, see Python lists explained for beginners, Python while loops explained, and Python if, else, and elif explained.
The complete to-do list script
Here is the full runnable script:
tasks = []
while True:
print("\nTo-Do List")
print("1. View tasks")
print("2. Add task")
print("3. Remove task")
print("4. Quit")
choice = input("Choose an option: ")
if choice == "1":
if not tasks:
print("No tasks yet.")
else:
for index, task in enumerate(tasks, start=1):
print(f"{index}. {task}")
elif choice == "2":
task = input("Enter a task: ")
tasks.append(task)
print("Task added.")
elif choice == "3":
if not tasks:
print("No tasks to remove.")
else:
for index, task in enumerate(tasks, start=1):
print(f"{index}. {task}")
number = int(input("Enter task number to remove: "))
removed = tasks.pop(number - 1)
print(f"Removed: {removed}")
elif choice == "4":
print("Goodbye!")
break
else:
print("Invalid choice.")
Sample interaction
To-Do List
1. View tasks
2. Add task
3. Remove task
4. Quit
Choose an option: 2
Enter a task: Buy milk
Task added.
To-Do List
1. View tasks
2. Add task
3. Remove task
4. Quit
Choose an option: 1
1. Buy milk
To-Do List
1. View tasks
2. Add task
3. Remove task
4. Quit
Choose an option: 3
1. Buy milk
Enter task number to remove: 1
Removed: Buy milk
How the task list is stored
The line below creates an empty list:
tasks = []
This list holds all tasks while the program is running.
Important points:
tasksstarts empty- Each task is stored as a string
- New tasks are added to the end of the list
- Removing a task changes the list only in memory
For example:
tasks = []
tasks.append("Buy milk")
tasks.append("Study Python")
print(tasks)
Output:
['Buy milk', 'Study Python']
The basic version does not save tasks to a file. That means when you close the program, the list disappears.
If you want to understand the add method in more detail, see Python list append() method.
How the menu loop works
The whole program runs inside this loop:
while True:
This means the menu keeps showing again and again until the program reaches:
break
Inside the loop, the program:
- Prints the menu
- Gets the user's choice
- Runs a different block of code based on that choice
The choice is handled with if, elif, and else:
choice = input("Choose an option: ")
if choice == "1":
print("View tasks")
elif choice == "2":
print("Add task")
elif choice == "3":
print("Remove task")
elif choice == "4":
print("Goodbye!")
break
else:
print("Invalid choice.")
This is a common pattern in simple command-line programs.
If you want more detail on user input, see Python input() function explained or how to get user input in Python.
How adding a task works
The add option uses this code:
task = input("Enter a task: ")
tasks.append(task)
print("Task added.")
What happens here:
- The program asks the user for task text
- The text is saved in the variable
task tasks.append(task)adds it to the list- A short confirmation message is printed
Example:
tasks = []
task = input("Enter a task: ")
tasks.append(task)
print(tasks)
If the user types Wash dishes, the list becomes:
['Wash dishes']
You could also improve this by checking for empty input:
task = input("Enter a task: ")
if task == "":
print("Task cannot be empty.")
else:
tasks.append(task)
print("Task added.")
How viewing tasks works
The view option first checks whether the list is empty:
if not tasks:
print("No tasks yet.")
This is a simple way to test whether a list has any items.
If the list is not empty, the script prints each task with a number:
for index, task in enumerate(tasks, start=1):
print(f"{index}. {task}")
Why use enumerate(tasks, start=1)?
enumerate()gives both the position and the valuestart=1makes the numbering begin at 1 instead of 0- This is easier for users to read
Example:
tasks = ["Buy milk", "Call Sam", "Study Python"]
for index, task in enumerate(tasks, start=1):
print(f"{index}. {task}")
Output:
1. Buy milk
2. Call Sam
3. Study Python
For more on this, see Python enumerate() function explained.
How removing a task works
Removing a task takes a few more steps.
First, the script shows the numbered task list:
for index, task in enumerate(tasks, start=1):
print(f"{index}. {task}")
Then it asks which task to remove:
number = int(input("Enter task number to remove: "))
This line does two things:
input()gets text from the userint()converts that text into a number
Finally, it removes the item:
removed = tasks.pop(number - 1)
print(f"Removed: {removed}")
Why number - 1?
- Users count tasks as 1, 2, 3
- Python list indexes are 0, 1, 2
- So task 1 is actually index 0
Example:
tasks = ["Buy milk", "Call Sam", "Study Python"]
number = 2
removed = tasks.pop(number - 1)
print("Removed:", removed)
print("Remaining tasks:", tasks)
Output:
Removed: Call Sam
Remaining tasks: ['Buy milk', 'Study Python']
The pop() method removes an item and returns it at the same time. See Python list pop() method for more details.
Be careful here:
- If the user types text instead of a number,
int()can raise aValueError - If the number is too large or too small,
pop()can raise anIndexError
A safer version with basic error handling
The first script works, but the remove section can crash if the user enters bad input.
Here is a safer version with basic error handling:
tasks = []
while True:
print("\nTo-Do List")
print("1. View tasks")
print("2. Add task")
print("3. Remove task")
print("4. Quit")
choice = input("Choose an option: ")
if choice == "1":
if not tasks:
print("No tasks yet.")
else:
for index, task in enumerate(tasks, start=1):
print(f"{index}. {task}")
elif choice == "2":
task = input("Enter a task: ").strip()
if task == "":
print("Task cannot be empty.")
else:
tasks.append(task)
print("Task added.")
elif choice == "3":
if not tasks:
print("No tasks to remove.")
else:
for index, task in enumerate(tasks, start=1):
print(f"{index}. {task}")
try:
number = int(input("Enter task number to remove: "))
if number < 1 or number > len(tasks):
print("That task number does not exist.")
else:
removed = tasks.pop(number - 1)
print(f"Removed: {removed}")
except ValueError:
print("Please enter a valid number.")
elif choice == "4":
print("Goodbye!")
break
else:
print("Invalid choice.")
This version improves the program by:
- Handling
ValueErrorif the user types text - Checking that the task number is in range
- Avoiding empty tasks with
.strip()
If you want to learn this pattern, see how to handle exceptions in Python.
Ways to improve the project
Once the basic version works, you can extend it.
Good next steps:
- Mark tasks as done
- Save tasks to a file
- Load tasks when the program starts
- Sort tasks by priority
- Split repeated code into functions
For example, saving tasks to a file is a natural next project. A related example is Python read and write text file example.
Common mistakes
These are some common problems beginners run into with this project:
- Forgetting to convert
input()toint()before using it as a number - Using the task number directly instead of subtracting 1
- Trying to remove a task when the list is empty
- Entering a task number that does not exist
- Accidentally using
appendwithout parentheses - Placing
breakoutside the loop
Helpful debugging prints
If something is not working, print values to see what the program is doing:
print(tasks)
print(choice)
print(number)
print(len(tasks))
print(type(choice))
print(type(number))
These can help you answer questions like:
- Is
choicea string or a number? - Is
numberthe value you expected? - Does
taskscontain what you think it contains?
If you get an out-of-range error, see IndexError: list index out of range fix explained.
If int() fails, see ValueError: invalid literal for int() with base 10 fix.
FAQ
Why does the script use number - 1 when removing a task?
Users usually count from 1, but Python list indexes start at 0. Subtracting 1 matches the user's number to the correct list position.
Why are my tasks lost when I close the program?
The basic version stores tasks only in memory. To keep them after the program closes, save them to a file.
What error happens if I type text instead of a task number?
You may get ValueError when int() tries to convert text that is not a valid number.
Can I build this project with functions?
Yes. A cleaner version can use separate functions for showing tasks, adding tasks, and removing tasks.
See also
- Python lists explained for beginners
- Python list
append()method - Python list
pop()method - Python
enumerate()function explained - Python
input()function explained - How to handle exceptions in Python
Copy the script, run it, and then improve it. A great next step is adding file saving or moving repeated code into functions.