Skip to content

Day 4 - Python: Debugging, testing and Regular expression

Welcome to Day 4 of Python! Today we will learn about:

  • Debugging and testing
  • Regular expressions
  • Datetime library

Let's start!

Debugging and testing

Debugging is the process of finding and correcting errors or bugs in code. Python includes a debugger called pdb that allows you to step through your code and inspect variables as you go. You can use pdb to help you figure out where your code is going wrong and how to fix it.

import pdb

def add_numbers(x, y):
    result = x + y
    pdb.set_trace() # Start the debugger at this point in the code
    return result

result = add_numbers(2, 3)

In this example, we define the add_numbers function, which adds two numbers and returns the result. To start the debugger at a specific point in the code, we use the pdb.set trace() function (in this case, after the result has been calculated). This enables us to inspect variables and step through the code to figure out what's going on.

In addition to debugging, testing is an important part of programming. It entails creating test cases to ensure that your code is working properly. Python includes a unittest module that provides a framework for writing and running test cases.

import unittest

def is_prime(n):
    if n < 2:
        return False
    for i in range(2, n):
        if n % i == 0:
            return False
    return True

class TestIsPrime(unittest.TestCase):
    def test_is_prime(self):

if __name__ == '__main__':


Ran 1 test in 0.000s


Regular expressions:

In Python, regular expressions are a powerful tool for working with text data. They enable you to search for and match specific character patterns within a string. Python's re module includes functions for working with regular expressions. For example, you can use regular expressions to search for email addresses within a larger block of text, or to extract specific data from a string that follows a particular pattern.

import re

# Search for a phone number in a string
text = 'My phone number is 555-7777'
match ='\d{3}-\d{4}', text)
if match:

# Extract email addresses from a string
text = 'My email is [email protected], but I also use [email protected]'
matches = re.findall(r'\S+@\S+', text)


Datetime library:

As the name suggests, Python's datetime module allows you to work with dates and times in your code. It includes functions for formatting and manipulating date and time data, as well as classes for representing dates, times, and time intervals. The datetime module, for example, can be used to get the current date and time, calculate the difference between two dates, or convert between different date and time formats.

from datetime import datetime, timedelta

# Get the current date and time
now =
print(now) # Output: 2023-02-17 11:33:27.257712

# Create a datetime object for a specific date and time
date = datetime(2023, 2, 1, 12, 0)
print(date) # Output: 2023-02-01 12:00:00

# Calculate the difference between two dates
delta = now - date
print(delta) # Output: 15 days, 23:33:27.257712


2023-02-17 11:33:27.257712
2023-02-01 12:00:00
15 days, 23:33:27.257712