Lists vs tuples — when to use which
Mutability, use cases, and simple rules to choose correctly.
One concept per article: data structures, syntax, built-ins, and common patterns. No long tutorials—just clear, focused explanations.
Once you know how to write basic Python—variables, data types, functions, and loops—you will keep running into the same ideas: when to use a list vs a tuple, what *args and **kwargs mean, how list comprehensions and decorators work, and why == and is behave differently. This section is a reference for those concepts: one article per topic, with short explanations and runnable examples.
We group topics roughly by theme. Data structures and types: lists vs tuples, mutable vs immutable, dictionaries, sets, slicing, and shallow vs deep copy. Functions and control flow: lambda, default arguments, return values, map and filter, decorators, and closures. Iteration and built-ins: enumerate, zip, range, generators, iterators and iterables. Strings and formatting: string methods, f-strings, and string formatting. Errors and context: exception handling and context managers.
Use the sidebar or the list below to jump to a concept. If you hit an error you do not recognise, check the Errors & Exceptions section; many messages become clear once you understand the underlying concept (for example NoneType has no len() after reading about return values and None)
Mutability, use cases, and simple rules to choose correctly.
Variable-length arguments in plain English, with examples.
What it means, why it matters, and how it affects your code.
A concise way to create lists from existing sequences.
How dictionaries work and when to use them.
What sets are, how they work, and when to use them.
Get both the index and value when looping over a sequence.
Loop over multiple lists at the same time.
Small, one-line functions without a name.
Apply functions to sequences and select items that meet conditions.
Create iterators that produce values one at a time, saving memory.
Add behavior to functions using the @ syntax.
Inner functions that access variables from outer scope.
Essential methods for working with strings.
The easiest and most readable way to format strings in Python.
Create sequences of integers for loops and list creation.
Understanding where variables are accessible in your code.
Understanding the difference when copying nested structures.
Handle errors gracefully without crashing your program.
Automatically manage resources like files and connections.
Essential methods for working with lists.
Get substrings, sublists, or parts of any sequence.
Make function parameters optional with default values.
Understanding how functions return data to the caller.
How to use code from other files and built-in modules.
Make decisions in your code based on conditions.
Loop through lists, strings, and other iterables.
Loop until a condition becomes false.
Combine and negate conditions in your code.
Compare values and get True or False.
How to create and use variables in Python.
Understanding Python's basic data types.
Define and call functions to organize your code.
Create dictionaries in one line, similar to list comprehensions.
Create sets in one line, similar to list comprehensions.
Check if any or all items in a sequence meet a condition.
Sort lists, tuples, strings, and other iterables.
Common functions for working with sequences and numbers.
Determine what type an object is.
Check if an item exists in a sequence or collection.
Three ways to format strings in Python.
Understanding what can be looped over in Python.
The __init__ method initializes new objects when you create them from a class.
Control how your objects appear as strings using __str__ for users and __repr__ for developers.
Define custom length behavior for your objects using the __len__ method.
Make your objects support indexing with square brackets using __getitem__ and __setitem__.
Make your objects callable like functions using the __call__ method.
Create custom iterators using __iter__ and __next__ methods to control iteration behavior.
Control object equality and make objects hashable using __eq__ and __hash__ methods.
Control how your objects evaluate to True or False in boolean contexts using __bool__.
Control how the 'in' operator works with your objects using the __contains__ method.
Override the + operator for your objects using the __add__ method.
Control attribute access and assignment using __getattr__ and __setattr__ methods.
Define cleanup behavior when objects are deleted using the __del__ method.
Control object creation before __init__ using the __new__ method.
Optimize memory usage by restricting object attributes with __slots__.
Access and manipulate the dictionary that stores object attributes using __dict__.
Access class and inheritance information using __class__ and __bases__ attributes.
Access object and module names using __name__ and __module__ attributes.
Access documentation strings using the __doc__ attribute.
Get the file path of a module using the __file__ attribute.
Understand how __main__ works when running Python scripts directly.
Control what gets imported with 'from module import *' using __all__.
Understand how __init__.py files turn directories into Python packages.
Understand how Python stores compiled bytecode in __pycache__ directories.
Store and access version information using the __version__ attribute.
Enable future Python features in current versions using __future__ imports.
Import modules dynamically at runtime using the __import__ function.
Access Python's built-in functions and objects through the __builtins__ module.
Access type annotations using the __annotations__ attribute.
Access global and local namespaces using __globals__ and __locals__ attributes.
Create custom context managers using __enter__ and __exit__ methods for the 'with' statement.
Define default values for missing dictionary keys using the __missing__ method.