Home › coding › python › py closures

Py Closures — Free Python Tutorial

Learn Py Closures in Python with a free, beginner-friendly tutorial, examples and practice for Indian students on Syllab.in.

✓ 100% Free ✓ No Login Needed ✓ NCERT / CBSE Aligned ✓ Download as PDF

TL;DR: Learn Py Closures in Python with a free, beginner-friendly tutorial, examples and practice for Indian students on Syllab.in.

Written & reviewed by the Syllab.in Academic Team (CBSE/NCERT subject experts) · Updated Jul 14, 2026

🤖 Stuck on any question? Ask Syllab's free AI Tutor for a step-by-step explanation — instant, unlimited, no login.

Py Closures in Python

A closure is a function that captures variables from its enclosing scope. The inner function "remembers" those variables even after the outer function has returned.

Three conditions: nested function, inner function uses outer variable, outer function returns inner function. Closures are the mechanism behind decorators, factory functions, and callbacks.

Use nonlocal keyword to reassign (not just mutate) a variable from the enclosing scope inside a closure.

Py Closures — Syntax

def outer(x):           # x is captured
    def inner(y):       # inner is the closure
        return x + y    # x from outer scope
    return inner

add5 = outer(5)
print(add5(3))  # → 8

Learn Py Closures step by step with Syllab's free interactive Python tutorial — runnable code examples, practice exercises and instant AI feedback, all free with no signup. Explore the full Python course →

Explore:

  • Syllabus
  • Practice
  • Mock Tests
  • NCERT Solutions
  • Coding
  • GK Quiz
  • Career Predictor
  • AI Tutor
  • Live Quiz
  • Doubt Solver
  • Microlearning
  • Free Alternatives
  • Kids Zone
  • Study Room
  • Calculators
  • Worksheets

Syllab.in — Free learning for Indian students, Class 1–12