Free Interactive Python Course

Python Tutorial — Learn Python Step by Step

Key takeaway: Master Python programming from scratch — variables, loops, functions, OOP, file handling, and libraries — with clear human-readable explanations and runnable code examples.

50+ Structured Lessons
Runnable Code Examples
100% Free No Registration
Updated October 2026

Python Variable Scope

Variable scope determines where a variable can be accessed. Python uses the LEGB rule: Local, Enclosing, Global, Built-in — searched in that order.

Local vs Global Scope

# Global variable
message = "Hello, World"

def greet():
    # Local variable — only exists inside this function
    name = "Alice"
    print(f"{message}, {name}")  # Can READ global

greet()         # Hello, World, Alice
# print(name)  # NameError: name is not defined (local to greet)

# Local variables shadow globals
x = 100

def example():
    x = 50  # Creates a NEW local variable, does not change global
    print(x)  # 50

example()
print(x)  # 100 (unchanged)

The LEGB Rule

# L - Local: inside the current function
# E - Enclosing: inside enclosing (outer) functions
# G - Global: module-level variables
# B - Built-in: Python built-in names (print, len, etc.)

x = "global"

def outer():
    x = "enclosing"

    def inner():
        x = "local"
        print(x)  # "local" (L found first)

    inner()
    print(x)  # "enclosing" (inner did not modify this)

outer()
print(x)  # "global" (outer did not modify this)

The global Keyword

# Without global, assignment creates a local variable
counter = 0

def increment():
    global counter  # tells Python to use the module-level variable
    counter += 1

increment()
increment()
print(counter)  # 2

# Common use: module-level configuration
_debug = False

def enable_debug():
    global _debug
    _debug = True

The nonlocal Keyword

# nonlocal refers to the enclosing function scope (not global)
def make_counter():
    count = 0

    def increment():
        nonlocal count  # modify the enclosing variable
        count += 1
        return count

    return increment

counter = make_counter()
print(counter())  # 1
print(counter())  # 2
print(counter())  # 3

# Without nonlocal, assignment raises UnboundLocalError
def broken_counter():
    count = 0
    def increment():
        count += 1  # Error! Python sees assignment, treats as local
        return count
    return increment

Best Practices

# AVOID excessive global state — pass data through parameters
# BAD: relies on global
user_data = {}

def process():
    global user_data
    user_data["processed"] = True

# GOOD: explicit input/output
def process(data):
    return {**data, "processed": True}

result = process({"name": "Alice"})

# Use classes to bundle related state instead of globals
class Counter:
    def __init__(self):
        self.count = 0

    def increment(self):
        self.count += 1
        return self.count

c = Counter()
print(c.increment())  # 1
print(c.increment())  # 2
  • Python resolves names using LEGB order: Local → Enclosing → Global → Built-in.
  • Assignment inside a function creates a local variable unless declared global or nonlocal.
  • Use global to modify module-level variables; use nonlocal for enclosing function variables.
  • Minimize global state — prefer passing data through function parameters and return values.
  • Use classes or closures instead of globals when you need persistent state.

Python Programming Tutorial — Learn Python in a practical, step-by-step way. According to the official Python documentation, Python emphasizes code readability and simplicity, making it ideal for beginners.

Related tools: Python Online Compiler | Python Examples | JavaScript Tutorial | Java Tutorial

Python Career & Learning Guide

Python is the world's most popular programming language for beginners, data science, AI/ML, web development, and automation. This tutorial teaches Python step-by-step with clear explanations and runnable code examples in our free Python Compiler.

Why Learn Python in 2026?

  • #1 Most Popular Language: Ranked first on TIOBE, Stack Overflow, and GitHub.
  • AI and Data Science: The primary language for machine learning (TensorFlow, PyTorch, scikit-learn) and data analysis (Pandas, NumPy).
  • Web Development: Django and Flask power scalable backends at companies like Instagram and Spotify.
  • Automation & Scripting: Automate files, emails, web scraping, and data processing in minutes.

Python vs Other Languages

FeaturePythonJavaJavaScriptC++
SyntaxVery clean, readableVerboseModerateComplex
TypingDynamic, strongStatic, strongDynamic, weakStatic, strong
Best ForAI/ML, Data, WebEnterprise, AndroidWeb Frontend/BackendSystems, Games
Learning Time2–4 weeks basics4–6 weeks basics3–4 weeks basics8–12 weeks basics

Frequently Asked Questions

Do I need prior programming experience to learn Python?

No! Python is specifically designed to be beginner-friendly. This tutorial starts from zero and builds up gradually.

Which Python version should I use?

Python 3.10+ is recommended. All examples in this tutorial use Python 3 syntax.

Is this Python tutorial completely free?

Yes, 100% free with no registration or sign-up required. Access all lessons and code examples anytime.