Skip to content
Chapter Practice Arena

Advanced Python Building Blocks Problems

Targeted practice challenges specifically curated for this chapter. Inspect hints, verify outputs, and check Pythonic solutions.

All Chapters
Difficulty:
Chapter:
Showing 6 challenges · 16 total · Page 1 of 3
#1beginnerDecorators
Write a decorator named before that prints Before and then calls the decorated no-argument function. Decorate a function named task that prints Task. Calling task() should display Before followed by Task.
Starter Code
def before(function):
    # Define and return a wrapper
    pass

@before
def task():
    print('Task')

task()
Expected Output:Before Task
#2beginnerEnums and Named Tuples
Define an OrderStatus enum with PENDING = 'pending' and SHIPPED = 'shipped'. Define an Order named tuple with order_id: int and status: OrderStatus. Create an order with ID 7 and status SHIPPED. Print the order ID and status value.
Starter Code
from enum import Enum
from typing import NamedTuple

# Define OrderStatus and Order here

# Create an order and print the required fields
Expected Output:7 shipped
#3beginnerRegular Expressions
Use re.findall() to extract all consecutive digit groups from the string 'A12 B7 C305'. Print the resulting list.
Starter Code
import re

text = 'A12 B7 C305'
# Write your solution below
Expected Output:['12', '7', '305']
#4intermediateRegular Expressions
Given the string 'Python is\tfun', use re.sub() to replace each run of one or more whitespace characters with a single space. Print the cleaned string.
Starter Code
import re

text = 'Python   is\tfun'
# Replace repeated whitespace and print the result
Expected Output:Python is fun
#5intermediateRegular Expressions
Use a regex with named groups to extract the year, month, and day from '2026-10-12'. Print the resulting dictionary using groupdict().
Starter Code
import re

text = '2026-10-12'
pattern = # Write a pattern with year, month, and day groups
match = re.fullmatch(pattern, text)

if match:
    print(match.groupdict())
Expected Output:{'year': '2026', 'month': '10', 'day': '12'}
#6beginnerContext Managers
Use with and open() to write the text 'ok' to temp.txt. Then open the file again and print its contents. Use UTF-8 encoding for both operations.
Starter Code
# Write your solution below
Expected Output:ok