Python · Stage 6 – Reusable Code · Lesson 36 of 50

Local and Global Scope

Understand where a variable name can be used.

def greet():
    message = "Hello"
    print(message)

greet()
Line-by-line explanation
  1. The function is defined.
  2. message is created locally inside it.
  3. The function can display message.
  4. The call runs the function.

Prefer parameters and return values over changing global variables. They make each function’s inputs and outputs visible.