Objective. Students write Python functions that use conditionals, loops, and try/except blocks to automate a repeated data-processing task and handle at least two types of runtime errors.
This lesson, its worked examples and its practice questions has not yet been verified by an industry-certified professional or by a certification or approval body. It is a study draft and may contain errors. Do not use it as instructions for work on a job site or on live equipment. Always follow your instructor, your supervising professional, and current official references and regulations.
VoTechWorks program approval and accreditation are still in progress. Credentials are issued only by the certifying bodies named in each program, after their own exams.
Writing reliable data pipelines means your code must make decisions, repeat tasks, and recover gracefully when something unexpected happens. This lesson covers three building blocks that make that possible: control flow (conditionals and loops), functions (reusable, named blocks of logic), and error handling (try/except blocks). Together they let you automate a repeated data-processing task without crashing when real-world files are messy—and real-world files are almost always messy.
Data analysts and scientists spend a significant portion of their time cleaning and validating data. A script that crashes the moment it encounters a missing column or a stray text value in a numeric field is not production-ready. Employers—and credentials such as IBM Data Science Professional and CompTIA Data+—expect you to write code that anticipates failure modes and handles them without losing the work already completed.
A conditional statement uses `if`, `elif`, and `else` to branch your program based on a condition that evaluates to `True` or `False`.
```python
if value < 0 or value > 500:
print("Out of range")
elif value == 0:
print("Zero detected")
else:
print("Value is acceptable")
```
Comparison operators (`<`, `>`, `==`, `!=`, `<=`, `>=`) and logical operators (`and`, `or`, `not`) let you combine conditions. Keep conditions as simple and readable as possible; complex nested conditionals are a common source of logic errors.
A `for` loop repeats a block of code for each item in an iterable—a list, a range of numbers, or a collection of filenames.
```python
for filename in file_list:
process_file(filename)
```
A `while` loop continues until a condition becomes `False`. For batch file processing, `for` loops are almost always the right choice because the number of iterations is known in advance.
Use `break` to exit a loop early and `continue` to skip the rest of the current iteration and move to the next one. Avoid using `break` carelessly; it can make code harder to read and debug.
A function wraps a block of logic under a name so you can call it repeatedly without rewriting the code. The `def` keyword defines a function; `return` sends a value back to the caller.
```python
def validate_value(value, col_name, low, high):
"""Return True if value is within [low, high]; raise ValueError otherwise."""
if not isinstance(value, (int, float)):
raise TypeError(f"{col_name} contains a non-numeric value: {value}")
if value < low or value > high:
raise ValueError(f"{col_name} value {value} is outside [{low}, {high}]")
return True
```
Every function you write for this course must include a docstring—the triple-quoted string immediately after the `def` line—and inline comments on any non-obvious line. Docstrings document what the function expects and returns; they are not optional in professional code.
You can signal that something is wrong by raising a built-in exception with `raise`. Two you will use often are:
Raising a descriptive exception is far more useful than silently returning `None` or `False`, because it forces the calling code to deal with the problem explicitly.
A `try/except` block lets you attempt an operation and catch specific exceptions if they occur, so the program continues rather than crashing.
```python
try:
validate_value(row[col], col, 0, 500)
except ValueError as e:
print(f"ValueError: {e}")
log_bad_record(filename, row)
except TypeError as e:
print(f"TypeError: {e}")
log_bad_record(filename, row)
except FileNotFoundError as e:
print(f"File not found: {e}")
```
Always catch the most specific exception type you expect. Catching the bare `Exception` class hides bugs. List each exception type separately or group truly equivalent ones with a tuple: `except (ValueError, TypeError) as e:`.
When validation fails, write the bad record to a log file rather than discarding it silently. Use Python's built-in `open()` in append mode (`"a"`) so successive runs add to the log rather than overwriting it.
```python
def log_bad_record(filename, row, log_path="bad_records.txt"):
"""Append a bad record entry to the log file."""
with open(log_path, "a") as log:
log.write(f"{filename} | {row}\n")
```
The `with` statement closes the file automatically even if an error occurs inside the block—always prefer it over manual `open()`/`close()` calls.
In data work, "safety" means protecting the integrity of data and the systems that depend on it. Before running any batch script on real data:
1. Work on copies. Never modify original source files. Keep raw data read-only.
2. Validate inputs before processing. Check that expected columns exist before iterating rows.
3. Limit file path assumptions. Use `os.path.join()` or `pathlib.Path` rather than hard-coded path strings, which break across operating systems.
4. Test on small samples first. Run your script on two or three files before unleashing it on an entire folder.
5. Review your log file after every run to confirm errors are being caught and recorded, not silently swallowed.
These habits mirror the data quality and pipeline reliability expectations tested on CompTIA Data+ and the IBM Data Science Professional Certificate.
The demo in this lesson builds one function (`validate_value`) and one helper (`log_bad_record`), then wraps them in a `for` loop that processes five CSV files of city traffic counts. After each file, a summary row—filename, total row count, error count—is appended to a results DataFrame. This mirrors the kind of lightweight ETL (extract, transform, load) script you will write on the job and in portfolio projects.
Answer each question, then check it. Nothing is recorded; this is your own practice.
A free tech program — complimentary, as our commitment to get America working. Help us build VoTechWorks. Tell us what works, what doesn’t, and what you want to learn.