Lesson 3 of 4Module 1 · 0% of course
Python Foundations · Names & data

Functions, arguments & scope

Functions package logic you reuse. Learn positional/keyword args, return values, and where names live (LEGB).

15 min · 5 steps · videoJump to quiz ↓
Python Tutorial: Variable Scope - Understanding the LEGB rule and global/nonlocal statements Corey Schafer · 21 min
Open in YouTube
Step 1 of 5 · 1. Anatomy of a function15 min read ·

1. Anatomy of a function

def notional(qty, px, fx=1.0):
    """Trade value in USD."""
    return qty * px * fx

notional(100, 99.5)            # positional
notional(qty=100, px=99.5, fx=1.36)  # keyword

def creates a function object and binds a name to it. return sends a value back; without it the function returns None. Defaults like fx=1.0 are evaluated once (recall the mutable-default trap from Lesson 1).

Next: 2. Scope: LEGB
‹ PrevNext ›