Back to Data Science Notes
Topic #49

Functions, Lambda & Scope

By the end of this lesson, you will be able to define reusable functions, create concise anonymous functions using lambdas, and understand how variable scope affects data access in Python.

What it is

In data science, functions are blocks of organized, reusable code that perform a single action. They help avoid repetition and make code modular. A lambda function is a small, anonymous function defined with the lambda keyword, typically used for short operations like filtering or mapping data. Scope refers to the region of your program where a variable is visible and accessible. Understanding scope prevents bugs caused by unintended variable overwriting or access errors.

Why it matters

  • Reusability: Write logic once (e.g., cleaning text) and apply it to multiple datasets.
  • Readability: Functions with clear names explain intent better than raw loops.
  • Efficiency: Lambdas allow quick inline transformations without defining full functions.
  • Maintainability: Isolating logic in functions makes debugging easier when data pipelines break.

Syntax or steps

A standard function uses the def keyword, followed by a name, parameters in parentheses, and a colon. The body is indented. A lambda follows the pattern lambda arguments: expression. Variables defined inside a function have local scope, meaning they cannot be accessed outside it unless returned. Variables defined outside functions have global scope.

Example

# Define a regular function to calculate squared values
def square_list(numbers):
    result = []
    for n in numbers:
        result.append(n ** 2)
    return result

# Use a lambda for a quick transformation
square_lambda = lambda x: x ** 2

# Apply both to a dataset
data = [1, 2, 3, 4]
print("Regular Function:", square_list(data))
print("Lambda Map:", list(map(square_lambda, data)))

# Scope demonstration
global_var = "I am global"

def check_scope():
    local_var = "I am local"
    print(global_var) # Accessible
    print(local_var)  # Accessible here

check_scope()
# print(local_var) # This would cause an error

The square_list function iterates through input numbers and returns a new list. The square_lambda achieves the same mathematical operation but is more concise. The map function applies the lambda to each element in data. In the scope section, global_var is readable inside the function, but local_var exists only within check_scope.

Common mistakes

  • Modifying global variables unintentionally: Avoid changing global state inside functions; pass data as arguments instead.
  • Overusing lambdas: Complex logic in a one-line lambda becomes unreadable. Use def if the function exceeds one simple expression.
  • Forgetting to return: Functions without a return statement yield None, which can break downstream calculations.
  • Variable shadowing: Naming a local variable the same as a global one can hide the global value, leading to confusion.

When to use it

FeatureStandard Function (def)Lambda Function
ComplexityHigh (multi-step logic)Low (single expression)
ReusabilityNamed and easily reusedOften one-off or passed as argument
DebuggingEasier (traceback shows name)Harder (shows <lambda>)
Best ForData cleaning pipelines, modelsSorting keys, map/filter operations

Practice

Guided Exercise: Write a function called filter_positive that takes a list of integers and returns only those greater than zero. Then, rewrite it using a lambda with the filter built-in.

Challenge: Create a dictionary of student scores. Use a lambda function as the key argument in sorted() to sort the students by their scores in descending order.

Hint: For the challenge, remember that sorted(dict.items(), key=lambda item: item[1], reverse=True) accesses the score (value) at index 1.

Quick check

Question: Why might a data scientist prefer a lambda function over a standard function when sorting a DataFrame column?

Answer: Because sorting often requires a simple, one-time transformation (like extracting a specific part of a string or negating a value), making the concise syntax of a lambda cleaner and faster to write than defining a separate named function.

Summary

Functions encapsulate logic for reuse and clarity, while lambdas provide a lightweight way to handle simple transformations. Mastering scope ensures that your data processing scripts remain predictable and free from hidden variable conflicts.

Want to go beyond the notes?

Join Coding Now Tech Institute's Data Science course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Functions, Lambda & Scope – FAQs

Quick answers about learning Functions, Lambda & Scope in Data Science.

This free note from Coding Now Tech Institute explains Functions, Lambda & Scope in Data Science — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Data Science topic on Coding Now Tech Institute, including Functions, Lambda & Scope, is 100% free with no signup required.
With focused practice, most students grasp Functions, Lambda & Scope in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now