Python Functions, Iteration & Data Transformations
GATE 2027 DA core: Programming in Python
“Build small reusable transformations and make data flow explicit.”
1.Prerequisites and reusable transformations
§ 1.1.Prerequisites: Python values, loops, and built-in collections. Use [Python Collections](/notes/data-structures/da-python-built-in-data-structures) as a refresher.
Functions name a reusable transformation. Parameters receive references to argument objects; reassigning a local parameter does not rebind the caller's name, but mutating a shared mutable object can be visible to the caller. Return computed outputs rather than relying on print side effects.
List comprehensions provide a compact map/filter pattern. Example: clean = [x.strip().lower() for x in raw if x is not None]. This retains input order, skips missing entries, trims whitespace, and lowercases each retained string. The expression is evaluated once for each element that passes the filter.
This lesson introduces programming-level transformations. The DA syllabus's named data-transformation methods—normalization, discretization, sampling, and compression—are treated separately in the database/warehousing module; do not confuse those with relational database normalization.
Worked example: summarize observations
[4, None, 6, 10], return the mean of observed values without changing the input.Build observed = [x for x in readings if x is not None], producing [4, 6, 10].
Compute sum(observed) / len(observed) = 20 / 3. The input list remains unchanged.
A reusable function should define what happens for an empty observed list (for example, raise ValueError); do not silently divide by zero.
Function and transformation checks
What does a Python function return if execution reaches its end without a `return` statement?
In `[x*x for x in values if x > 0]`, when is `x*x` evaluated?
One-minute revision
“Write a function contract, transform to a fresh result when appropriate, and handle missing or empty data explicitly.”
- •Mean = sum(observed) / count(observed)
- •Comprehension order: iterate → filter → transform
- •Implicit
Nonereturn - •Mutable default values
- •Mutating while iterating
- •Undefined missing-data behavior
Primary Academic References & Exam Sources
Every concept in Notes From a B.Tech Brain is synthesized from authoritative academic texts, seminal peer-reviewed research, verified exam archives, and renowned university lectures.
Curated for clarity, rigorous numerical solving, and semester excellence:
Visual thinkers who want to see each algorithm step animated clearly.
GATE-targeted coverage with step-by-step GATE PYQ walkthroughs.