Data validation and transformation
Chapter 82 of 132 · Level 5 · Databases, APIs & integration · Python demonstrations with recorded outputs. Learn through reading, predicting and testing real Python code.
Course disclaimer · independent educational content
01 · Why this concept matters
External data cannot be trusted merely because it looks like a dictionary. Validation checks shape, type, range and business rules before transformation or storage.
Learning goal: Data validation and transformation. Start by reading what the program does, then inspect its exact output. Where an example illustrates only a prerequisite, study the named advanced feature separately before using it in production.
02 · The important subtopics
Study these connected ideas as you work through the demonstration:
- Schema validation with Pydantic
- Missing values and malformed records
- Normalization and business rules
- Error reports and rejected rows
03 · Pinpoint: read the code line by line
- Line 1
def valid_product(record):Define or import. Notice what values enter this statement and which names it affects. - Line 2
return isinstance(record.get('name'), str) and isinstance(record.get('price'), (int, float)) and record['price'] >= 0Control the next step. Notice what values enter this statement and which names it affects. - Line 3
print(valid_product({'name': 'Pen', 'price': 1.5}))Display a result. Notice what values enter this statement and which names it affects. - Line 4
print(valid_product({'name': 'Pen', 'price': -1}))Display a result. Notice what values enter this statement and which names it affects.
Find the literal input values and arguments, follow each operation in execution order, and verify the result below. This habit turns a code listing into a testable explanation.
04 · Runnable Python example and verified output
Data validation and transformation: working demonstration
Python · runnabledef valid_product(record):
return isinstance(record.get('name'), str) and isinstance(record.get('price'), (int, float)) and record['price'] >= 0
print(valid_product({'name': 'Pen', 'price': 1.5}))
print(valid_product({'name': 'Pen', 'price': -1}))
True False
Why it works: External data cannot be trusted merely because it looks like a dictionary. Validation checks shape, type, range and business rules before transformation or storage.
Worked example 2 · Data Validation And Transformation in practice
Python · executed and checkeddef validate_person(data):
if not isinstance(data.get('age'), int): raise TypeError('age must be an integer')
return {'name': data['name'].strip(), 'age': data['age']}
print(validate_person({'name': ' Ana ', 'age': 25}))
{'name': 'Ana', 'age': 25}Pinpoint learning: This practical data validation and transformation demonstration uses a small input you can inspect. Run it exactly as shown, compare the displayed result, then change one value and predict how the result will change.
Understand the code line by line
1: def validate_person(data):— Define a reusable function. Its body executes when called.2: if not isinstance(data.get('age'), int): raise TypeError('age must be an integer')— Choose which indented branch executes based on a condition.3: return {'name': data['name'].strip(), 'age': data['age']}— Send the computed result back to the caller.4: print(validate_person({'name': ' Ana ', 'age': 25}))— Display the value so you can compare it with the verified output.
05 · Independent practice and answer guidance
Exercise A · Adapt the code
Add validation that rejects blank product names and quantities that are not whole numbers.
Exercise B · Predict
Before pressing Run, predict the first displayed line. Next, change one literal value and record the new observed output. Explain the difference in one clear sentence.
Exercise C · Debug
Make a temporary invalid change, read the exception or failed assertion if one occurs, restore the working code, and explain the repair.
Worked explanation for the original example
External data cannot be trusted merely because it looks like a dictionary. Validation checks shape, type, range and business rules before transformation or storage.
True False
06 · Build, verify and advance
Real-world task: Implement an import pipeline for messy customer records.
Run the original code; explain the important lines; change one input and verify the new outcome; use a test or recorded result to justify your conclusion.
