Learn Python
A structured guide to Python, from your first program to data structures, functions, exceptions, files, and classes, with examples you can run.
Welcome
Start with what Python is, how to install it, and how to run your first program.
-
What Is Python? A Beginner-Friendly Guide to the Python Language
Learn what Python is, what it is used for, and why beginners and universities choose it. See how it compares with JavaScript, Java, and C.
-
Installing Python 3.12+ on macOS, Windows, and Linux
Install Python 3.12+ on macOS, Windows, or Linux, verify the setup, set PATH correctly, and fix the most common install problems.
-
Your First Python Program: Write, Run, and Understand It
Write and run your first Python program, understand print(), comments, variables, and common beginner mistakes in Python 3.12+.
-
Running Python: REPL, Scripts, and Notebooks Explained
Learn the difference between the Python REPL, scripts, and notebooks, and how to run each one correctly from the command line or editor.
-
Editors & IDEs for Python: How to Choose the Right One
Learn how Python editors and IDEs differ, what features matter, and how to choose a setup that helps you write, run, and debug code faster.
Python Basics
Learn how Python reads code, names values, and stores numbers, text, and truth values.
-
Python Syntax & Indentation: How Blocks and Whitespace Work
Learn Python syntax and indentation, how blocks are formed, why whitespace matters, and how to avoid common indentation errors.
-
Python Comments & Docstrings: Writing Clear Code and Documentation
Learn how Python comments and docstrings work, when to use each, how triple quotes behave, and common mistakes to avoid.
-
Python Variables and Assignment: Names, Values, and Rebinding
Learn how Python variables work, how assignment binds names to objects, and how rebinding, copying, and mutable values really behave.
-
Python Data Types Overview: Numbers, Strings, Lists, and More
Learn Python data types, how they store values, when to use each one, and how to avoid common beginner mistakes.
-
Python Numbers: int, float, and complex Explained
Learn Python int, float, and complex numbers, how arithmetic works, how to convert between them, and common mistakes to avoid.
-
Python Strings Basics: Text, Quotes, Indexing, and Common Operations
Learn Python string basics: how to create text values, quote them, index characters, slice, combine, and avoid common beginner mistakes.
-
Python Booleans and Truthiness: True, False, and Values in Conditions
Learn Python booleans and truthiness, how values become True or False, and why conditions, comparisons, and empty values behave the way.
-
Python None: What It Means, How to Check It, and Common Mistakes
Learn what Python None means, how to check for it correctly, how it behaves in code, and the mistakes that confuse beginners.
-
Python Type Conversion: Converting Between str, int, float, and bool
Learn Python type conversion with int(), float(), str(), and bool(). See how explicit casting works, common mistakes, and surprising edge.
-
Python print() and input(): Basic Console Output and Input
Learn how Python print() displays text and values, how input() reads user input, and how to combine them for simple interactive programs.
Operators
See how Python calculates, compares, and combines values, and how is and in differ from ==.
-
Python Arithmetic Operators: +, -, *, /, //, %, and **
Learn Python arithmetic operators, how each one works, when results become floats, and how to avoid common division and precedence mistakes.
-
Python Comparison Operators: ==, !=, <, >, <=, and >=
Learn Python comparison operators, how == differs from is, how chained comparisons work, and how to avoid common mistakes.
-
Python Logical Operators: and, or, not Explained
Learn Python logical operators and, or, and not, including truthy values, short-circuiting, precedence, and common mistakes with examples.
-
Python Assignment and Augmented Assignment Operators
Learn Python assignment and augmented assignment operators, including =, +=, -=, *=, aliasing, and the mistakes that cause surprising.
-
Python Identity and Membership Operators: is and in
Learn Python's identity and membership operators, when to use is vs ==, how in works with sequences, sets, and dictionaries, and common.
Strings
Work with text: string methods, slicing, and f-strings for formatting.
-
Python String Indexing & Slicing: Access and Cut Text
Learn Python string indexing and slicing, including negative indexes, slice steps, and common off-by-one mistakes with clear examples.
-
Python String Methods Reference: str Methods, Syntax, and Examples
Learn Python string methods with clear examples, return values, and common mistakes. See how str methods work and when to use each one.
-
Python f-Strings and String Formatting: A Practical Guide
Learn how Python f-strings work, how to format values, and when to use format specifiers, alignment, and braces in real code.
Control Flow
Choose paths with if and match, and repeat work with for and while loops.
-
Python if, elif, and else: Conditional Statements Explained
Learn how Python if, elif, and else choose between code paths with clear examples, common mistakes, and the order conditions are checked.
-
Python match (Pattern Matching): How Structural Pattern Matching Works
Learn Python match statement pattern matching, including case patterns, guards, OR patterns, and common mistakes with clear examples.
-
Python for Loops: How to Iterate Over Sequences and Ranges
Learn how Python for loops work, how to use range(), loop over sequences, avoid off-by-one errors, and handle common for-loop mistakes.
-
Python while Loops: How They Work and When to Use Them
Learn how Python while loops work, when they stop, how to avoid infinite loops, and how to write reliable loop conditions.
-
Python range() Explained: How to Create Number Sequences
Learn how Python range() works, how to use start, stop, and step, and how to avoid off-by-one mistakes in loops and slices.
-
Python break, continue, and loop else Explained
Learn how Python break, continue, and loop else work, when each one runs, how they change loop flow, and common mistakes to avoid.
Data Structures
Store groups of values in lists, tuples, dictionaries, and sets, and build them with comprehensions.
-
Python Lists: Mutable Sequence Basics, Methods, and Examples
Learn Python lists, how they store ordered items, how mutation works, and how to use common list methods, slicing, and loops safely.
-
Python List Methods Reference: append(), extend(), insert(), remove()
Learn Python list methods with clear examples of append, extend, insert, pop, remove, sort, reverse, clear, copy, count, and index.
-
Python Tuples: Ordered, Immutable Sequences in Python
Learn Python tuples, how they work, when to use them, tuple packing and unpacking, and common mistakes with immutability and indexing.
-
Python Dictionaries: Key-Value Storage, Lookup, and Common Uses
Learn how Python dictionaries store key-value pairs, how to read and update them, and the mistakes that cause lookup and mutation bugs.
-
Python Sets: Unique Items, Fast Membership, and Set Operations
Learn Python sets: how they store unique items, test membership, remove duplicates, and use union, intersection, and difference operations.
-
Python Comprehensions: List, Set, and Dictionary Syntax
Learn Python comprehensions, how list, set, and dictionary comprehensions work, when to use them, and common mistakes to avoid.
-
Python Mutable vs Immutable Objects: Key Differences and Examples
Learn the difference between mutable and immutable Python objects, how aliasing works, and when to choose each with clear examples.
-
Python Shallow vs Deep Copy: Differences, Examples, and Pitfalls
Learn the difference between shallow and deep copy in Python, when to use each, and how to avoid shared nested data bugs.
Functions
Define functions, pass arguments, return values, and understand scope and recursion.
-
Python Defining Functions: Syntax, Parameters, and Return Values
Learn how to define Python functions with def, parameters, return values, docstrings, and common mistakes like mutable defaults.
-
Python Positional, Keyword, and Default Arguments
Learn how Python positional, keyword, and default arguments work, how they interact, and how to avoid common calling mistakes.
-
Python *args and **kwargs: Passing Variable Arguments
Learn how Python *args and **kwargs collect extra positional and keyword arguments, how to use them in functions, and common mistakes.
-
Python Return Values: How Functions Send Data Back
Learn how Python function return values work, how to return one or many results, and common mistakes that cause None or unpacking errors.
-
Python Scope and the LEGB Rule: Local, Enclosing, Global, Built-in
Learn Python scope and the LEGB rule, how local, enclosing, global, and built-in names are resolved, and why UnboundLocalError happens.
-
Python Lambda Functions: Anonymous Functions With lambda
Learn Python lambda functions, how anonymous functions work, when to use them, and common mistakes with closures, sorting, and map/filter.
-
Python Recursion: How Recursive Functions Work and How to Trace Them
Learn Python recursion with base cases, call stacks, tracing examples, RecursionError fixes, and when a loop is better than recursion.
-
Python Type Hints: Syntax, Meaning, and Real Use in Functions
Learn Python type hints for functions: how annotations work, how to read them, common mistakes, and how they help with clearer code.
Errors & Exceptions
Read Python's error messages, handle exceptions, and raise your own.
-
Python Errors and Exceptions: Types, Handling, and Debugging
Learn what Python errors and exceptions mean, how they differ, how to handle them with try/except, and how to debug common failures.
-
Python try, except, else, and finally: How Exception Handling Works
Learn how Python try, except, else, and finally work together to handle errors, run cleanup code, and avoid common exception handling.
-
Python Raising Exceptions and Custom Exceptions Explained
Learn how to raise exceptions in Python, create custom exception classes, and choose the right error type for clear, testable code.