Python 3 Complete Guide: Core Syntax, Data Structures & Best Practices
What You Will Master in This Tutorial
- Master foundational syntax: control flow, truthy/falsy evaluation, and string formatting.
- Understand mutable vs. immutable types (lists, tuples, dicts, sets).
- Write modular, reusable functions with type annotations and docstrings.
1. Pythonic Syntax and Type Annotations
Python emphasizes readability. Modern Python 3.12+ makes extensive use of type hinting to improve IDE autocomplete and eliminate runtime type errors.
from typing import List
def calculate_discount(prices: List[float], discount_rate: float = 0.10) -> float:
if not 0 <= discount_rate <= 1:
raise ValueError("Discount rate must be between 0.0 and 1.0")
subtotal = sum(prices)
return round(subtotal * (1 - discount_rate), 2)
Knowledge Check: Test Your Understanding
Frequently Asked Questions
Going deeper: Python fundamentals in practice
The fundamentals in this guide become powerful when they are combined on real work. A small automation script, for example, uses everything at once: dictionaries to hold configuration, lists to collect results, functions with type annotations and docstrings so the next reader β often future you β can reuse them safely, and truthy/falsy checks that keep conditionals short and readable. Modern Python 3.12+ rewards this style because annotations feed your IDE's autocomplete and let tools like MyPy catch type errors before the code ever runs. Developers who internalize the mutable-versus-immutable distinction early also stop losing afternoons to a whole category of bugs, where a function quietly modifies a list the caller still needed.
Common mistakes
- Treating lists and tuples as interchangeable. Lists are mutable and tuples are not; modifying a tuple raises an error, and mutating a shared list inside a function surprises every caller that kept a reference.
- Skipping virtual environments. Installing every project's packages into the system Python guarantees dependency conflicts sooner or later; an isolated environment per project prevents them entirely.
- Ignoring type annotations until production. Untyped code pushes type errors to runtime, where users find them. Annotations following PEP 484/585 let MyPy and your IDE catch the same bugs while you write.
- Writing functions without docstrings or reuse in mind. One-off scripts that tangle logic into a single block cannot be tested or reused; small functions with docstrings become a personal library that compounds over time.
Frequently asked questions
What is the difference between a list and a tuple?
Mutability. A list can be changed after creation β items added, removed, or replaced β while a tuple is immutable once defined. Use lists for collections that evolve and tuples for fixed records whose contents should never change accidentally.
When should I start using type annotations?
From your very first functions. Annotating parameters and return types, as in the discount example in this guide, improves IDE autocomplete immediately and lets static checkers like MyPy catch mismatched types before you run the program.
What should I build after finishing this guide?
Small, complete projects that exercise the fundamentals together: a script that reads data into lists and dictionaries, processes it with documented, annotated functions, and prints formatted results. That combination β data structures, control flow, and modular functions β is the foundation every advanced topic builds on. Finishing one small project end to end will teach you more than reading three more chapters of theory.