SyntaxPilot
Courses/Data Analytics with Python/Lesson 7

Data Analytics with Python · Lesson 7 of 90 · 17 min

Python for data 4: functions, errors and files

functiondefparameterargumentreturndefault argumentkeyword argument*args**kwargslambdascopemoduleexceptiontry/except/finally

These are the notes for Data Analytics with Python, episode 7. In episode 6 we built a weekly report for Ramesh's chai stall. Here we make that code reusable with functions, make it survive bad data with exceptions (try, except, finally, raise), and let it read and save real files with open(), the with statement and the csv, json and pathlib modules. Every term is defined the first time it appears and collected in the Glossary at the end. Every program below was run for real (Python 3.13); outputs are copied unedited.

The problem: 52 copies of the same code

Ramesh wants the weekly report for all 52 weeks of the year. The obvious way is to copy the code 52 times and change the numbers in each copy. Then he finds a mistake in the average formula. He fixes it in week 1, and the other 51 copies still have the bug. Copy-paste code multiplies every mistake.

Real-life example: a recipe card. The cook doesn't rewrite the recipe for every customer; it is written once, has a name, and you just order "one masala chai". In code, that recipe card is a function.

Functions: def, parameters, arguments, return

A function is a named block of code that you write once and run whenever you call it (write its name followed by brackets). You have already used built-in functions: print(), len(), sum().

  • def (short for define) starts a function definition.
  • A parameter is a placeholder name in the definition for data the function will receive (cups below).
  • An argument is the actual value you pass when you call the function (week1).
  • The body is the indented block under the def line.
  • return sends a value back to the caller and ends the function immediately.
def weekly_avg(cups):
    total = sum(cups)
    return total / len(cups)

week1 = [320, 280, 410, 300, 350, 390, 450]
week2 = [300, 310, 290, 420, 380, 360, 400]
print(weekly_avg(week1))
print(weekly_avg(week2))
357.14285714285717
351.42857142857144

One definition, called twice. Fix the formula once, inside the function, and every week is fixed.

Defining a function runs nothing. Writing the recipe card cooks nothing; the body runs only when you call the function.

print() only shows a value on the screen for a human. It gives nothing back to the code. A function with no return gives back None, Python's value for "nothing".

def weekly_avg(cups):
    print(sum(cups) / len(cups))

avg = weekly_avg([320, 280, 410])
print("avg is", avg)
print(avg + 10)
336.6666666666667
avg is None
Traceback (most recent call last):
  File "main.py", line 6, in <module>
    print(avg + 10)
          ~~~~^~~~
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

Missing arguments

def weekly_avg(cups):
    return sum(cups) / len(cups)

print(weekly_avg())
Traceback (most recent call last):
  File "main.py", line 4, in <module>
    print(weekly_avg())
          ~~~~~~~~~~^^
TypeError: weekly_avg() missing 1 required positional argument: 'cups'

A positional argument is matched to a parameter by its position in the brackets. Every parameter without a default needs one, and the error message names the missing parameter.

Default and keyword arguments

Real-life example: at a chai stall "one chai" means regular sugar unless you say otherwise (a default). "Chai, no sugar, extra ginger" names exactly what you change (a keyword).

  • A default argument is a parameter with a ready value (price=12), used when the caller doesn't pass one.
  • A keyword argument is passed by the parameter's name (gst=0). With keywords, the order doesn't matter.
  • round(x, 2) rounds to 2 decimal places.
def bill(cups, price=12, gst=0.05):
    amount = cups * price
    return round(amount * (1 + gst), 2)

print(bill(10))
print(bill(10, 15))
print(bill(10, gst=0))
print(bill(price=20, cups=3))
126.0
157.5
120
63.0

bill(10) = 10 × 12 × 1.05 = 126.0. bill(10, 15) changes the price by position. bill(10, gst=0) skips the price and changes only the tax (the result is the whole number 120 because every value in that calculation was an int). Rule: parameters with defaults must come after parameters without defaults, or Python gives SyntaxError: parameter without a default follows parameter with a default.

args and *kwargs

When you don't know how many values will come:

  • *args: a star before a parameter collects any number of extra positional arguments into one tuple (an unchangeable sequence, episode 5).
  • **kwargs (keyword arguments): two stars collect any extra named arguments into a dict.
  • The names args and kwargs are only conventions; the stars do the work.
def total(*days):
    print("got:", days)
    return sum(days)

print(total(320, 280))
print(total(320, 280, 410, 300))

def item(name, **details):
    print(name, details)

item("chai", price=12, size="small")
got: (320, 280)
600
got: (320, 280, 410, 300)
1310
chai {'price': 12, 'size': 'small'}

You will see **kwargs in pandas and matplotlib, where functions pass options through to other functions.

lambda: tiny nameless functions

sorted() can take a key: a function that says what to compare. To sort menu items by price we need a function that returns item 1 of each (name, price) pair.

A lambda is a small function with no name, written in one line: lambda arguments: expression. It can hold only one expression, which it returns automatically.

menu = {"chai": 12, "samosa": 15, "bun maska": 25,
        "vada pav": 18}
def get_price(pair):
    return pair[1]
print(sorted(menu.items(), key=get_price))
print(sorted(menu.items(), key=lambda p: p[1]))
double = lambda x: x * 2
print(double(21))
[('chai', 12), ('samosa', 15), ('vada pav', 18), ('bun maska', 25)]
[('chai', 12), ('samosa', 15), ('vada pav', 18), ('bun maska', 25)]
42

Use def for anything longer than one expression; use lambda for short throwaway keys.

Scope: local and global variables

Scope is where in the code a variable can be seen. Real-life example: a hotel. Things in your room are visible only in your room; the lobby noticeboard is visible to every guest.

  • A local variable is created inside a function and exists only while that function runs.
  • A global variable is created at the top level of the file, outside any function; functions can read it.
rate = 12
def bill(cups):
    total = cups * rate
    return total

print(bill(10))
print(total)
120
Traceback (most recent call last):
  File "main.py", line 7, in <module>
    print(total)
          ^^^^^
NameError: name 'total' is not defined

total was local to bill(); it vanished when the call ended. If you need a value outside, return it. (A function can change a global only with the global keyword, which is best avoided: pass values in and return results.)

Modules and import

A module is a Python file of ready-made functions and values. import loads it. import x as y gives it a short nickname (an alias); from x import name brings in a single name. A package is a bigger collection of modules (NumPy and pandas are packages).

import math
import statistics as st
from statistics import median

cups = [320, 280, 410, 300, 350, 390, 450]
print(math.sqrt(144))
print(st.mean(cups))
print(median(cups))
12.0
357.14285714285717
350

The mean is the ordinary average; the median is the middle value when the numbers are sorted (episode 34 covers both in depth). Any file you write is a module too: save functions in chai_tools.py, and import chai_tools in another file in the same folder gives you chai_tools.weekly_avg(...).

Exceptions: when one bad value crashes everything

Real data is messy. Ramesh's helper typed N/A on a day the register was lost.

log = ["320", "280", "N/A", "410"]
total = 0
for c in log:
    total += int(c)
print("Total:", total)
Traceback (most recent call last):
  File "main.py", line 4, in <module>
    total += int(c)
             ~~~^^^
ValueError: invalid literal for int() with base 10: 'N/A'
  • An exception is an error that happens while the program runs. Python raises it; if nobody handles it, the program stops.
  • The printout is the traceback: the trail of lines that led to the error. Read it from the bottom up: the last line names the exception and its message.
  • ValueError means the type was right (a string) but the value can't be used (it isn't a number).

try and except

Real-life example: a delivery person finds one locked door, notes it, and continues the route.

  • try: the block of risky lines.
  • except SomeError: the block that runs only if that error happens inside try.
log = ["320", "280", "N/A", "410"]
total = 0
for c in log:
    try:
        total += int(c)
    except ValueError:
        print("skipped bad value:", c)
print("Total:", total)
skipped bad value: N/A
Total: 1010

Always name the exception you expect. A bare except: catches everything, including your own typos (NameError), and hides real bugs.

finally

finally is a block that runs no matter what: error or not, even after a return. It is for clean-up, such as closing a file or a database connection. ZeroDivisionError is raised when you divide by zero, for example averaging an empty list.

def safe_avg(values):
    try:
        return sum(values) / len(values)
    except ZeroDivisionError:
        print("no data, returning 0")
        return 0
    finally:
        print("checked", len(values), "values")

print(safe_avg([320, 280]))
print(safe_avg([]))
checked 2 values
300.0
no data, returning 0
checked 0 values
0

raise: errors on purpose

Python wouldn't complain about negative cups, but they are impossible. raise creates an exception yourself, with your own message. except ValueError as e stores the exception object in e, so you can print its message.

def add_sale(cups):
    if cups < 0:
        raise ValueError(f"cups can't be negative: {cups}")
    return cups

print(add_sale(320))
try:
    add_sale(-5)
except ValueError as e:
    print("Caught:", e)
add_sale(-5)
320
Caught: cups can't be negative: -5
Traceback (most recent call last):
  File "main.py", line 11, in <module>
    add_sale(-5)
    ~~~~~~~~^^^^
  File "main.py", line 3, in add_sale
    raise ValueError(f"cups can't be negative: {cups}")
ValueError: cups can't be negative: -5

Failing loudly is better than silently storing wrong data.

Exceptions you'll meet most

Exception Meaning Example that causes it
ValueError Right type, unusable value int("N/A")
TypeError Wrong type for the operation None + 10, "5" + 5
NameError Name not defined (or out of scope) print(total) outside the function
KeyError Missing dictionary key menu["coffee"]
IndexError Position out of range [1, 2][5]
ZeroDivisionError Division by zero sum([]) / len([])
FileNotFoundError The file isn't there open("missing.txt")

Files: making data outlive the program

Variables live in memory (RAM), the computer's fast short-term storage, which is cleared when the program ends, like a whiteboard wiped every night. A file on disk (long-term storage) is like Ramesh's sales register: still there tomorrow.

open(name, mode) opens a file and returns a file object (here f) with methods like .write(), .read() and .close().

File modes:

Mode Meaning If the file exists If it doesn't
"r" (default) read read from the start FileNotFoundError
"w" write erased first created
"a" append (add at the end) kept, new text added created
"x" create only FileExistsError created
add "b" binary (images, Excel) e.g. "rb"

"\n" is the newline character: it ends a line of text.

f = open("sales.txt", "w")
f.write("320\n280\n410\n")
f.close()

f = open("sales.txt", "r")
print(f.read())
f.close()
320
280
410

The with statement

If you forget close(), or the program crashes before it, the data may not be fully saved. The with statement opens a file for the indented block and closes it automatically when the block ends, even after an error. A context manager is the general name for objects that work with with.

with open("sales.txt", "w") as f:
    f.write("320\n280\n")
with open("sales.txt", "a") as f:
    f.write("410\n")
with open("sales.txt") as f:
    for line in f:
        print(int(line) + 1)
with open("sales.txt", "w") as f:
    f.write("999\n")
with open("sales.txt") as f:
    print(f.read())
321
281
411
999

Looping over a file object gives one line at a time. Notice "w" wiped the file: only 999 is left.

with open("sales_2025.txt") as f:
    print(f.read())
Traceback (most recent call last):
  File "main.py", line 1, in <module>
    with open("sales_2025.txt") as f:
         ~~~~^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'sales_2025.txt'

CSV files with the csv module

CSV (comma-separated values) is a table saved as plain text: a header row of column names, then one line per row with commas between values. Splitting lines by hand breaks on values that contain commas or quotes; the csv module handles them correctly.

  • csv.writer(f).writerows(rows) writes a list of lists.
  • csv.DictReader(f) reads each row as a dict keyed by the header.
  • Open CSV files with newline="" so the module controls line endings (otherwise Windows gets blank lines between rows).
import csv
rows = [["day", "cups", "weather"],
        ["Mon", 320, "sunny"], ["Tue", 280, "rainy"],
        ["Wed", 410, "sunny"]]
with open("week.csv", "w", newline="") as f:
    csv.writer(f).writerows(rows)

with open("week.csv", newline="") as f:
    for row in csv.DictReader(f):
        print(row)
{'day': 'Mon', 'cups': '320', 'weather': 'sunny'}
{'day': 'Tue', 'cups': '280', 'weather': 'rainy'}
{'day': 'Wed', 'cups': '410', 'weather': 'sunny'}

Every value comes back as text ('320'), because a CSV file is plain text:

import csv
with open("week.csv", "w", newline="") as f:
    csv.writer(f).writerows([["day", "cups"],
                             ["Mon", 320], ["Tue", 280]])
total = ""
with open("week.csv", newline="") as f:
    for row in csv.DictReader(f):
        total += row["cups"]
print("Wrong total:", total)
with open("week.csv", newline="") as f:
    print("Right total:",
          sum(int(r["cups"]) for r in csv.DictReader(f)))
Wrong total: 320280
Right total: 600

Adding strings concatenates (joins) them. Convert with int() or float() first. (pandas' read_csv converts types for you, episode 13.)

JSON files with the json module

JSON (JavaScript Object Notation) is a text format for nested data (dicts inside lists inside dicts). It is what most web APIs (programs that serve data to other programs over the internet) send. It looks like Python dicts and lists.

  • json.dump(obj, f, indent=2) writes an object to a file (indent makes it readable).
  • json.load(f) reads it back as Python objects.
  • json.dumps(obj) / json.loads(text) do the same with strings (the s is for string).
import json
menu = {"chai": 12, "samosa": 15, "items": ["tea", "snack"],
        "open": True}
with open("menu.json", "w") as f:
    json.dump(menu, f, indent=2)

with open("menu.json") as f:
    loaded = json.load(f)
print(loaded["samosa"], type(loaded))
print(json.dumps({"day": "Mon", "cups": 320, "note": None}))
15 <class 'dict'>
{"day": "Mon", "cups": 320, "note": null}

Translations: None ↔ null, True ↔ true, dict ↔ object, list ↔ array. JSON keys are always strings.

pathlib: file paths that work everywhere

A path is the address of a file, like data/week.csv. Windows writes data\week.csv. pathlib.Path builds paths correctly on any system.

  • / joins path parts: Path("data") / "week.csv".
  • .mkdir(exist_ok=True) creates a folder, no error if it exists.
  • .exists(), .name (file name), .stem (name without extension), .suffix (extension).
  • .read_text() / .write_text() read or write a whole text file in one call.
from pathlib import Path
folder = Path("data")
folder.mkdir(exist_ok=True)
p = folder / "week.csv"
p.write_text("day,cups\nMon,320\n")
print(p)
print(p.exists(), p.name, p.stem, p.suffix)
print(p.read_text())
print(Path("data/missing.csv").exists())
data/week.csv
True week.csv week .csv
day,cups
Mon,320

False

Check .exists() before opening to avoid FileNotFoundError.

Mini project: load, clean, summarise, save

A function loads a CSV, skips bad values instead of crashing, returns two values (a tuple, unpacked into cups, bad), and the summary is saved as JSON.

import csv, json
from pathlib import Path
def load_cups(path):
    good, bad = [], 0
    with open(path, newline="") as f:
        for row in csv.DictReader(f):
            try:
                good.append(int(row["cups"]))
            except ValueError:
                bad += 1
    return good, bad
p = Path("week.csv")
p.write_text("day,cups\nMon,320\nTue,N/A\nWed,410\nThu,300\n")
cups, bad = load_cups(p)
avg = round(sum(cups) / len(cups), 1)
out = {"days": len(cups), "skipped": bad, "avg": avg}
Path("summary.json").write_text(json.dumps(out))
print(Path("summary.json").read_text())
{"days": 3, "skipped": 1, "avg": 343.3}

Change the file name, and the same function handles any week: a tiny data pipeline (a chain of steps: load → clean → summarise → save).

Common mistakes

Mistake Exact error Why Fix
print() instead of return TypeError: unsupported operand type(s) for +: 'NoneType' and 'int' A function without return gives None return the value
Calling without an argument TypeError: weekly_avg() missing 1 required positional argument: 'cups' The parameter has no default Pass the argument
Using a local variable outside NameError: name 'total' is not defined Locals vanish after the call Return it and store it
int("N/A") on raw data ValueError: invalid literal for int() with base 10: 'N/A' Not a number try / except ValueError
Opening a missing file FileNotFoundError: [Errno 2] No such file or directory: 'sales_2025.txt' Wrong name or folder Path(...).exists(), check the folder
Opening with "w" to add data (no error: old data erased) "w" starts fresh Use "a" to append
Adding CSV values directly (no error: '320280') CSV values are strings int(row["cups"])
Bare except: (no error: bugs hidden) Catches everything Name the exception

Practice

  1. Write percent_change(old, new) that returns the percentage change, and raises ValueError if old is 0.
Solution
def percent_change(old, new):
    if old == 0:
        raise ValueError("old value can't be 0")
    return (new - old) / old * 100

print(percent_change(300, 330))
try:
    percent_change(0, 50)
except ValueError as e:
    print("Error:", e)
10.0
Error: old value can't be 0
  1. Write describe(*values) that returns the smallest, largest and average of any number of values.
Solution
def describe(*values):
    return min(values), max(values), sum(values) / len(values)

print(describe(320, 280, 410))
(280, 410, 336.6666666666667)
  1. Save your screen time for 3 days to screen.csv with the csv module, read it back and print the average.
Solution
import csv
with open("screen.csv", "w", newline="") as f:
    csv.writer(f).writerows([["day", "hours"], ["Mon", 3.5], ["Tue", 5], ["Wed", 2.5]])
with open("screen.csv", newline="") as f:
    hours = [float(r["hours"]) for r in csv.DictReader(f)]
print(sum(hours) / len(hours))
3.6666666666666665
  1. Store a snack menu in menu.json, load it, and print the items sorted by price, most expensive first.
Solution
import json
with open("menu.json", "w") as f:
    json.dump({"chai": 12, "samosa": 15, "bun maska": 25}, f)
with open("menu.json") as f:
    menu = json.load(f)
print(sorted(menu.items(), key=lambda p: p[1], reverse=True))
[('bun maska', 25), ('samosa', 15), ('chai', 12)]

Quick quiz

def f(x, y=2):
    return x * y

print(f(3), f(3, y=5))

A) 6 15 B) 5 8 C) 3 15 D) TypeError

Answer

A) 6 15. f(3) uses the default y=2 (3 × 2); f(3, y=5) overrides it with a keyword argument (3 × 5).

Interview questions

  1. What is the difference between a parameter and an argument? A parameter is the name in the function definition; an argument is the value passed in a call.
  2. What does a function return if it has no return statement? None.
  3. What are *args and **kwargs? *args collects extra positional arguments into a tuple; **kwargs collects extra keyword arguments into a dict.
  4. Why is a mutable default argument like def f(x=[]) dangerous? The default list is created once, when the function is defined, and shared by every call, so values pile up between calls. Use x=None and create the list inside.
  5. When would you use lambda instead of def? For a short, one-expression function used once, such as a key= for sorting.
  6. What does finally do? Runs its block whether or not an exception happened, for clean-up.
  7. Why use with open(...)? It closes the file automatically, even if an error happens.
  8. Why does csv.DictReader give '320' instead of 320? CSV is plain text; the module doesn't guess types. Convert explicitly.

Glossary

Term Meaning Example
Function A named block of code you write once and call many times def weekly_avg(cups):
Call Running a function by writing its name and brackets weekly_avg(week1)
def Keyword that defines a function def bill(cups):
Parameter Placeholder name in a function definition cups
Argument Actual value passed in a call week1, 10
Positional argument Argument matched to a parameter by position bill(10, 15)
Body The indented lines of a function return ...
return Sends a value back and ends the function return total / len(cups)
None Python's value for "nothing"; what a function without return gives avg is None
Default argument Parameter with a ready value used when none is passed price=12
Keyword argument Argument passed by the parameter's name bill(10, gst=0)
*args Collects extra positional arguments into a tuple def total(*days):
**kwargs Collects extra keyword arguments into a dict def item(name, **details):
Tuple Unchangeable sequence (320, 280)
lambda One-line function with no name; returns its expression lambda p: p[1]
key (sorting) Function that tells sorted what to compare sorted(x, key=len)
Scope Where in the code a variable can be seen local vs global
Local variable Made inside a function; exists only during the call total in bill()
Global variable Made at the top level; readable everywhere rate = 12
Module A Python file of ready-made code you import math, statistics
import Loads a module import math
Alias Short nickname for a module import statistics as st
Package A collection of modules NumPy, pandas
Mean The ordinary average st.mean(cups)
Median The middle value of sorted data median(cups)
Exception An error raised while the program runs ValueError
Traceback The error report showing the lines that led to it; read bottom-up Traceback (most recent call last)
try Block of risky lines try:
except Block that handles a named exception except ValueError:
finally Block that always runs (clean-up) finally:
raise Creates an exception on purpose raise ValueError("...")
ValueError Right type, unusable value int("N/A")
TypeError Wrong type for an operation None + 10
NameError Unknown name print(total)
ZeroDivisionError Division by zero 1 / 0
FileNotFoundError The file doesn't exist open("missing.txt")
Memory (RAM) Fast short-term storage, cleared when the program ends variables
Disk Long-term storage where files live sales.txt
File Named data stored on disk week.csv
open() Opens a file and returns a file object open("sales.txt", "w")
File object The handle for an open file, with read/write methods f.write(...)
File mode How a file is opened: read, write, append "r", "w", "a"
Newline character \n, ends a line of text "320\n"
with statement Opens a resource for a block and closes it automatically with open(...) as f:
Context manager Object that works with with (sets up and cleans up) a file object
CSV Comma-separated values: a table as plain text Mon,320,sunny
Header row First line of a CSV with the column names day,cups,weather
csv module Reads and writes CSV correctly csv.DictReader(f)
DictReader Reads CSV rows as dicts keyed by the header row["cups"]
Concatenate Join strings end to end "320" + "280" → "320280"
JSON Text format for nested data, used by web APIs {"cups": 320}
API A program that serves data to other programs a weather API
json module Converts between Python objects and JSON json.load(f)
null JSON's version of None "note": null
Path The address of a file data/week.csv
pathlib Module for building and checking paths Path("data") / "week.csv"
stem / suffix File name without extension / the extension week / .csv
Data pipeline A chain of steps: load, clean, summarise, save the mini project

Summary

  • A function packages code: def name(parameters):, an indented body, and return to give a result back. print only shows; without return you get None.
  • Defaults and keyword arguments make calls flexible; *args (tuple) and **kwargs (dict) accept any number of extras; lambda is a one-line nameless function.
  • Local variables live only inside their function; modules give ready-made functions via import.
  • Exceptions stop a program unless handled: try / except (name the error), finally for clean-up, raise for your own errors.
  • Files keep data after the program ends: open() with a mode ("r", "w" erases, "a" appends), and with closes them for you.
  • csv reads tables (values come back as text), json handles nested data, pathlib builds paths that work everywhere.
  • Next episode: NumPy arrays, maths on a whole column in one line.