I1 · Python Programming for AI

Scope and Lambda Functions

Scope is the part of a program where a name can be used. Variables created inside a function are local to it and disappear when it ends; variables created at the top level are global. A lambda is a small anonymous function written in one expression, such as lambda x: x * 2, often passed to functions like sorted().

8 min Intermediate Jump to the activity

What you need: The earlier topics of this module.

Definition

Why can't a function see a variable created inside another function?

Scope is the part of a program where a name can be used. Variables created inside a function are local to it and disappear when it ends; variables created at the top level are global. A lambda is a small anonymous function written in one expression, such as lambda x: x * 2, often passed to functions like sorted().

In simple words

Each function call gets its own private notebook. What it writes there is gone when it finishes, and it can't scribble in another function's notebook. A lambda is a function so short you don't bother naming it: sorted(students, key=lambda s: s[1]) means 'sort by the second item'.

Why it matters

Scope prevents functions from accidentally changing each other's variables, which keeps large programs safe. Lambdas are used constantly for sorting and for quick transformations in data code.

How it works

  • Local: created inside a function, visible only there.
  • Global: created at the top level, readable inside functions.
  • Assigning to a name inside a function creates a new local variable, even if a global has the same name.
  • Prefer passing values in and returning results out over changing globals.

In code

A local variable that shadows a global, and lambdas used to sort records.

scope_and_lambda.py
count = 10            # global

def add_five():
    count = 5         # a new local variable, not the global
    return count

print("Inside the function:", add_five())
print("Global is unchanged:", count)

students = [("Aarav", 72), ("Meera", 91), ("Kabir", 64)]
by_marks = sorted(students, key=lambda s: s[1], reverse=True)
print("By marks:", by_marks)
print("By name length:", sorted(students, key=lambda s: len(s[0])))

double = lambda x: x * 2
print("double(21) =", double(21))

Output

Inside the function: 5
Global is unchanged: 10
By marks: [('Meera', 91), ('Aarav', 72), ('Kabir', 64)]
By name length: [('Aarav', 72), ('Meera', 91), ('Kabir', 64)]
double(21) = 42

Try it yourself

Which value is used?

x = 1 at the top level. Is each x local or global?

  • print(x) at the top level after calling f()

  • x inside def f(): x = 5; return x

  • x inside def g(): return x + 1 (no assignment)

  • A parameter: def h(x): return x * 2

Common mistakes

  • Expecting a function to change a global by assigning to it.
  • Using a local variable after the function has ended.
  • Writing long lambdas. If it needs more than one expression, use def.

Check yourself

Question 1

1. A variable created inside a function is…

Question 2

2. sorted([3, -5, 1], key=lambda n: abs(n)) is…

Question 3

3. lambda x: x + 1 is…

Summary

  • Local variables live inside a function; globals live at the top level.
  • Assigning inside a function creates a local; pass values instead of changing globals.
  • lambda makes a tiny function, ideal as a sort key.

Finished reading?

Sources: W3Schools: Python Lambda