Mathew K Analytics

Lesson 5 · Python for Data Science

5 - Automating Repetitive Tasks in Python

Ever get bored copying data, renaming files, or sending the same messages over and over? Today, you will learn how Python can help save you time by…

⬇ Download notebookOpen in Colab ↗

What you'll learn

Data

No separate download needed — the notebook creates or downloads everything it uses.

📓 Full notebook

Download .ipynb
 

Welcome to Automating Repetitive Tasks in Python!#

Ever get bored copying data, renaming files, or sending the same messages over and over?

Today, you will learn how Python can help save you time by automating these boring, repetitive tasks.

You do not need to be a programmer yetyou just need curiosity.

Let's begin our automation adventure!

# First, let's print a welcome message automatically.
print("Welcome, human! Did you know you can automate this message?")
Welcome, human! Did you know you can automate this message?

Why Automate?#

Automation means computers do small, repeatable chores for us.

It saves time, reduces mistakes, and lets us focus on fun or important stuff.

You can use automation for:

  • Renaming lots of files
  • Sending reminders
  • Filling out forms

Lets see basic Python tricks for these tasks.

# Loops let us do a task many times automatically.
for i in range(5):
    print("Repeating message #", i + 1)
    
Repeating message # 1
Repeating message # 2
Repeating message # 3
Repeating message # 4
Repeating message # 5
# Instead of sending five messages, let's automate sending messages to multiple friends.
friends = ["Eva", "Sam", "Luca"]
for name in friends:
    print("Hi,", name, "- this is your reminder to drink water!")
    
Hi, Eva - this is your reminder to drink water!
Hi, Sam - this is your reminder to drink water!
Hi, Luca - this is your reminder to drink water!

How Input Helps Automation#

Python can ask you for information, then use it many times.

Input is a way to interact with your script.

Lets try it below.

# Let's ask for your favorite snack, then repeat a reminder.
snack = input("What is your favorite snack? ")
for i in range(3):
    print("Don't forget to enjoy some", snack, "today!")
    
Don't forget to enjoy some popcorn today!
Don't forget to enjoy some popcorn today!
Don't forget to enjoy some popcorn today!
 
# With automation, tiny changes make a big difference.
# Here, we send personalized reminders for each friend.
for name in friends:
    print("Hey", name + ",")
    print("Don't forget to eat your", snack, "today!")
    print()
    
Hey Eva,
Don't forget to eat your popcorn today!

Hey Sam,
Don't forget to eat your popcorn today!

Hey Luca,
Don't forget to eat your popcorn today!

Automating Repetitive Math#

What if you have a list of numbers and want to add the same bonus to each one?

Let Python do it!

# Suppose these are daily steps counted by your fitness tracker.
steps = [4900, 5200, 4800, 5000, 5300]

# Let's add a 500-step bonus to each day automatically.
steps_with_bonus = []
for count in steps:
    steps_with_bonus.append(count + 500)

print("With bonus:", steps_with_bonus)
With bonus: [5400, 5700, 5300, 5500, 5800]
# Sometimes, the task is to pick only certain items.
# For example, finding step counts above 5000.

active_days = []
for count in steps:
    if count > 5000:
        active_days.append(count)

print("Days over 5000 steps:", active_days)
Days over 5000 steps: [5200, 5300]

Safely Accessing Data#

Sometimes, data might be missing.

Python can help you avoid errors when something is not there.

Lets see a safe access pattern.

# Let's avoid errors if a friend is missing from the list.
search = input("Which friend's reminder are you looking for? ")
if search in friends:
    print("Found! Sending a reminder to", search)
else:
    print("Sorry, friend not found. Add them to the list first.")
    
Found! Sending a reminder to Luca
 
# Lets learn to add and remove people from your friends list.
friends.append("Maya")  # Adding Maya
print("Added Maya:", friends)

friends.remove("Eva")   # Removing Eva
print("After removing Eva:", friends)
Added Maya: ['Eva', 'Sam', 'Luca', 'Maya']
After removing Eva: ['Sam', 'Luca', 'Maya']

Using Built-in Functions to Automate Better#

Python comes with handy functions to save you even more time.

Lets use len() to count things, and sorted() to put them in order.

# How many friends do we have?
print("Number of friends:", len(friends))
# Let's see our friends alphabetically.
print("Sorted friends:", sorted(friends))
Number of friends: 3
Sorted friends: ['Luca', 'Maya', 'Sam']
# Sometimes you want a summarythe total steps you walked.
print("Total steps:", sum(steps))
Total steps: 25200

Automation With Comprehensions#

Comprehensions make short, powerful automation.

They create new lists from old lists in one line.

Try one example below.

# Double your daily steps using a list comprehension!
doubled_steps = [n * 2 for n in steps]
print("Doubled steps:", doubled_steps)
Doubled steps: [9800, 10400, 9600, 10000, 10600]
# You can use comprehensions to filter, too.
even_steps = [n for n in steps if n % 2 == 0]
print("Even step days:", even_steps)
Even step days: [4900, 5200, 4800, 5000, 5300]
# Mini-project: Automate birthday wishes for friends.
birthdays = {"Luca": "June 2", "Sam": "April 10", "Maya": "December 25"}
for name in birthdays:
    print("Happy Birthday,", name + "! Your big day is", birthdays[name])
    
Happy Birthday, Luca! Your big day is June 2
Happy Birthday, Sam! Your big day is April 10
Happy Birthday, Maya! Your big day is December 25
# Mini-project step 2: Ask for a name and check for their birthday.
person = input("Check if a birthday is coming up! Enter a friend's name: ")
if person in birthdays:
    print("Yes!", person + "'s birthday is on", birthdays[person])
else:
    print("Sorry, no birthday foundbut you can add it!")
    
Yes! Maya's birthday is on December 25
 
# Sometimes things can go wrong with automation.
try:
    value = int(input("Type a number to double: "))
    print("Doubled:", value * 2)
except ValueError:
    print("That was not a number. Please try again.")
    
Doubled: 16
 
# You can use functions to reuse your automation.
def greet(name):
    print("Hello,", name, "! This greeting was automated just for you.")

greet("Taylor")
greet("Jordan")
Hello, Taylor ! This greeting was automated just for you.
Hello, Jordan ! This greeting was automated just for you.
# Challenge: Automate filling a simple form.
answers = []
questions = ["Name", "Favorite color", "Lucky number"]
for q in questions:
    reply = input("Please enter your " + q + ": ")
    answers.append(reply)
print("Form filled:", answers)
Form filled: ['Jordan', 'green', '10']
 

Recap: You Are Ready to Automate!#

You learned how to automate repetitive tasks with loops, lists, input, and built-in tools.

You built birthday reminders and filled forms the easy way.

Every task you automate is a little more free time for you.

Keep practicing and try automating something new today!

Thanks for Watching!#

Like, subscribe, and share if you enjoyed the lesson.

Tell us in the comments what you want to automate next!

Happy automating!

Found this useful?

All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.