Skip to content

Repository files navigation

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:

  • Self-contained files — Each file can be run independently
  • Comprehensive comments — Detailed explanations for every concept
  • Practical examples — Real-world use cases and patterns
  • Progressive difficulty — From basics to advanced topics

Topics Covered

01. Basics

FileTopicDescription
01_print.pyPrint FunctionOutput, formatting, f-strings, escape characters
02_comments.pyCommentsSingle-line, multi-line, docstrings, best practices
03_variables.pyVariablesAssignment, naming conventions, multiple assignment
04_data_types.pyData TypesNumbers, strings, booleans, type conversion

02. Control Flow

FileTopicDescription
01_if_else.pyIf/ElseConditionals, comparison operators, logical operators
02_elif.pyElifMultiple conditions, grading systems, ranges
03_match_case.pyMatch/CasePattern matching (Python 3.10+), guards, unpacking

03. Loops

FileTopicDescription
01_for_loop.pyFor LoopsIteration, range(), enumerate(), zip()
02_while_loop.pyWhile LoopsCounters, sentinels, infinite loops
03_break_continue.pyLoop Controlbreak, continue, pass, for-else

04. Data Structures

FileTopicDescription
01_lists.pyListsIndexing, slicing, methods, comprehensions
02_tuples.pyTuplesImmutability, unpacking, named tuples
03_sets.pySetsOperations, frozen sets, practical uses
04_dictionaries.pyDictionariesMethods, nesting, comprehensions

05. Functions

FileTopicDescription
01_function_basics.pyFunction BasicsDefining functions, scope, docstrings
02_arguments.pyArgumentsPositional, keyword, *args, **kwargs, type hints
03_return_values.pyReturn ValuesMultiple returns, early returns, generators
04_lambda_functions.pyLambdaAnonymous functions, map, filter, reduce

06. Modules & Packages

FileTopicDescription
01_imports.pyImportsImport patterns, standard library, organization
02_custom_modules.pyCustom ModulesCreating modules, packages, __init__.py

07. Error Handling

FileTopicDescription
01_try_except.pyTry/ExceptException handling, else, finally
02_custom_exceptions.pyCustom ExceptionsCreating exceptions, hierarchy, best practices

08. Object-Oriented Programming

FileTopicDescription
01_classes_objects.pyClasses & ObjectsAttributes, methods, self
02_init_methods.pyInit & MethodsConstructors, properties, classmethods
03_inheritance.pyInheritanceSingle/multiple inheritance, super(), MRO
04_polymorphism.pyPolymorphismDuck typing, operator overloading, protocols

09. Advanced Python

FileTopicDescription
01_list_comprehensions.pyComprehensionsList, dict, set comprehensions
02_generators.pyGeneratorsYield, iterators, memory efficiency
03_decorators.pyDecoratorsSimple/parameterized decorators, functools
04_context_managers.pyContext Managerswith statement, __enter__/__exit__, contextlib

10. Best Practices

FileTopicDescription
01_pep8.pyPEP 8Python style guide, naming, formatting
02_type_hinting.pyType HintingType annotations, typing module, generics
03_virtual_envs.pyVirtual Environmentsvenv, 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

RequirementVersionNotes
Python3.10+Recommended (for match/case support)
Python3.8+Minimum (for most features)
# Check your Python version
python --version

File Structure

Each Python file follows a consistent structure:

"""================================================================================File: filename.pyTopic: 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
  2. Control Flow
  3. Loops
  1. Data Structures
  2. Functions
  3. Modules
  1. Error Handling
  2. OOP
  3. Advanced Python
  1. Best Practices
  2. 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.

#ProjectDifficultyConcepts Applied
01Number Guessing Game🟢 BeginnerLoops, Conditionals, Random module
02Expense Tracker🟡 IntermediateDicts, File I/O (JSON), Functions
03Library Management System🟠 AdvancedOOP, Inheritance, Custom Exceptions
04Real-time Weather CLI🚀 ProfessionalAPI 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:

  • 01. Basics — Print, Comments, Variables, Data Types
  • 02. Control Flow — If/Else, Elif, Match/Case
  • 03. Loops — For, While, Break/Continue
  • 04. Data Structures — Lists, Tuples, Sets, Dictionaries
  • 05. Functions — Basics, Arguments, Returns, Lambda
  • 06. Modules & Packages — Imports, Custom Modules
  • 07. Error Handling — Try/Except, Custom Exceptions
  • 08. OOP — Classes, Init, Inheritance, Polymorphism
  • 09. Advanced Python — Comprehensions, Generators, Decorators
  • 10. Best Practices — PEP8, Type Hints, Virtual Environments
  • 11. Testing — Unit Tests, Pytest, Assertions

🏗️ Projects

  • Project 01 — Number Guessing Game (Beginner)
  • Project 02 — Expense Tracker (Intermediate)
  • Project 03 — Library Management System (Advanced)
  • Project 04 — Real-time Weather CLI (Professional)

Happy Learning!

Made with ❤️ by Baraa

About

A complete, folder-based guide to the Python language, from basics to advanced concepts, explained through clear, runnable code examples.

Topics

Resources

Stars

208 stars

Watchers

3 watching

Forks

Contributors

Languages