python-by-example

Python by Example

Python by Example

A practical, example-driven Python repository designed to help you learn Python from fundamentals to advanced concepts through clear, runnable code.

Each file is self-contained and focuses on a single concept, making the repository suitable for structured learning, revision, or reference.


Table of Contents


Overview

This repository contains a complete Python tutorial designed for both beginners and experienced developers looking to refresh their knowledge. Each topic is organized in separate folders with practical, runnable examples.

Key Features:


Topics Covered

01. Basics

| File | Topic | Description | |:—–|:——|:————| | 01_print.py | Print Function | Output, formatting, f-strings, escape characters | | 02_comments.py | Comments | Single-line, multi-line, docstrings, best practices | | 03_variables.py | Variables | Assignment, naming conventions, multiple assignment | | 04_data_types.py | Data Types | Numbers, strings, booleans, type conversion |

02. Control Flow

| File | Topic | Description | |:—–|:——|:————| | 01_if_else.py | If/Else | Conditionals, comparison operators, logical operators | | 02_elif.py | Elif | Multiple conditions, grading systems, ranges | | 03_match_case.py | Match/Case | Pattern matching (Python 3.10+), guards, unpacking |

03. Loops

| File | Topic | Description | |:—–|:——|:————| | 01_for_loop.py | For Loops | Iteration, range(), enumerate(), zip() | | 02_while_loop.py | While Loops | Counters, sentinels, infinite loops | | 03_break_continue.py | Loop Control | break, continue, pass, for-else |

04. Data Structures

| File | Topic | Description | |:—–|:——|:————| | 01_lists.py | Lists | Indexing, slicing, methods, comprehensions | | 02_tuples.py | Tuples | Immutability, unpacking, named tuples | | 03_sets.py | Sets | Operations, frozen sets, practical uses | | 04_dictionaries.py | Dictionaries | Methods, nesting, comprehensions |

05. Functions

| File | Topic | Description | |:—–|:——|:————| | 01_function_basics.py | Function Basics | Defining functions, scope, docstrings | | 02_arguments.py | Arguments | Positional, keyword, *args, **kwargs, type hints | | 03_return_values.py | Return Values | Multiple returns, early returns, generators | | 04_lambda_functions.py | Lambda | Anonymous functions, map, filter, reduce |

06. Modules & Packages

| File | Topic | Description | |:—–|:——|:————| | 01_imports.py | Imports | Import patterns, standard library, organization | | 02_custom_modules.py | Custom Modules | Creating modules, packages, __init__.py |

07. Error Handling

| File | Topic | Description | |:—–|:——|:————| | 01_try_except.py | Try/Except | Exception handling, else, finally | | 02_custom_exceptions.py | Custom Exceptions | Creating exceptions, hierarchy, best practices |

08. Object-Oriented Programming

| File | Topic | Description | |:—–|:——|:————| | 01_classes_objects.py | Classes & Objects | Attributes, methods, self | | 02_init_methods.py | Init & Methods | Constructors, properties, classmethods | | 03_inheritance.py | Inheritance | Single/multiple inheritance, super(), MRO | | 04_polymorphism.py | Polymorphism | Duck typing, operator overloading, protocols |

09. Advanced Python

| File | Topic | Description | |:—–|:——|:————| | 01_list_comprehensions.py | Comprehensions | List, dict, set comprehensions | | 02_generators.py | Generators | Yield, iterators, memory efficiency | | 03_decorators.py | Decorators | Simple/parameterized decorators, functools | | 04_context_managers.py | Context Managers | with statement, __enter__/__exit__, contextlib |

10. Best Practices

| File | Topic | Description | |:—–|:——|:————| | 01_pep8.py | PEP 8 | Python style guide, naming, formatting | | 02_type_hinting.py | Type Hinting | Type annotations, typing module, generics | | 03_virtual_envs.py | Virtual Environments | venv, pip, requirements.txt |


Getting Started

Follow these steps to clone the repository and set it up in your local editor:

  1. Clone the repository Open your terminal and run:
    git clone https://github.com/blshaer/python-by-example.git
    cd python-by-example
    
  2. Set up a Virtual Environment (Recommended) It’s best practice to keep your projects isolated. Run these commands:
    python -m venv venv
    # On Windows:
    .\venv\Scripts\activate
    # On macOS/Linux:
    source venv/bin/activate
    
  3. Install Dependencies Install the necessary tools for testing and formatting:
    pip install -r requirements.txt
    
  4. Open in your code editor We recommend Visual Studio Code. Open the project directly:
    code .
    
  5. Explore and Run Navigate to any example, like 01_basics, and run the file:
    cd 01_basics
    python 01_print.py
    
  6. Read the comments — Detailed explanations are provided inline for every concept!

  7. Try the Tests — Go to the new testing module to see how professional code is verified:
    pytest 11_testing/01_test_basics.py
    

Prerequisites

Requirement Version Notes
Python 3.10+ Recommended (for match/case support)
Python 3.8+ Minimum (for most features)
# Check your Python version
python --version

File Structure

Each Python file follows a consistent structure:

"""
================================================================================
File: filename.py
Topic: Topic Name
================================================================================

Description of what the file covers...

Key Concepts:
- Concept 1
- Concept 2
- ...

================================================================================
"""

# -----------------------------------------------------------------------------
# Section 1: Basic Example
# -----------------------------------------------------------------------------
# ... code with inline comments ...

# -----------------------------------------------------------------------------
# Section 2: Advanced Example
# -----------------------------------------------------------------------------
# ... more code ...

Learning Path

🌱 Beginner 📈 Intermediate 🚀 Advanced 💼 Professional
1. [Basics](./01_basics/) 2. [Control Flow](./02_control_flow/) 3. [Loops](./03_loops/) 4. [Data Structures](./04_data_structures/) 5. [Functions](./05_functions/) 6. [Modules](./06_modules_packages/) 7. [Error Handling](./07_error_handling/) 8. [OOP](./08_oop/) 9. [Advanced Python](./09_advanced_python/) 10. [Best Practices](./10_best_practices/) 11. [Testing](./11_testing/)
 Recommended Flow:
Beginner     →  01_basics → 02_control_flow → 03_loops
Intermediate →  04_data_structures → 05_functions → 06_modules
Advanced     →  07_error_handling → 08_oop → 09_advanced
Professional →  10_best_practices → 11_testing

Projects

Put your skills to the test! Each project includes a challenge description (README.md) so you can try building it yourself before looking at the solution.

# Project Difficulty Concepts Applied
01 Number Guessing Game 🟢 Beginner Loops, Conditionals, Random module
02 Expense Tracker 🟡 Intermediate Dicts, File I/O (JSON), Functions
03 Library Management System 🟠 Advanced OOP, Inheritance, Custom Exceptions
04 Real-time Weather CLI 🚀 Professional API Requests, Decorators, Type Hints

💡 Tip: Read the project README.md first and try to build it on your own before looking at solution.py!


Progress Tracker

Use this checklist to track your learning progress:

🏗️ Projects


Happy Learning!

Made with ❤️ by Baraa