Python Programming from First Principles: Learn to Code, Think Computationally, and Solve Real-World Problems
PDFEPUB
ABOUT THE BOOK

Preface

On this pageWho this book is forWhat this book coversTo get the most out of this bookDownload code filesConventions used

Learning to program means learning to turn a question into steps a computer can carry out. Python gives us a readable way to express those steps, but understanding comes from examining what a program does and testing why it works. This book starts with small instructions and builds toward functions, objects and applications you can explain and adapt.

Who this book is for

This book is for beginners, university students, independent learners and researchers who want a practical foundation in Python. No previous programming experience is required. You should be able to use a computer, edit text and save files; the opening material introduces the tools needed to run Python.

Examples connect programming with familiar tasks, including text processing, library records and a university course system. Readers interested in the humanities, social sciences and data-driven work can use these examples as starting points without needing specialist knowledge of those fields.

What this book covers

The book has eight parts. Work through them in order when the concepts are new to you; use the contents to return to a specific topic later.

  • Part 1 - Introduction to Python: prepare your tools, run your first programs, and learn how scripts and notebooks fit into a Python workflow.
  • Part 2 - Basic Concepts: work with numbers, strings, names, input, conversions, Boolean expressions and operators.
  • Part 3 - Collections: organize related values using lists, tuples, sets, dictionaries and nested records.
  • Part 4 - Control Flow: choose between actions and repeat work using conditions, pattern matching and loops.
  • Part 5 - Functions: organize reusable behavior with parameters, return values and scope, then explore flexible arguments, decorators, recursion and generators.
  • Part 6 - Modules: separate code into files and use standard-library tools for dates, mathematics, JSON and regular expressions.
  • Part 7 - Object-Oriented Programming: create classes and objects, distinguish instance and class members, examine relationships and contracts, and model designs with UML. The University Course System project brings these ideas together.
  • Part 8 - Advanced Python Concepts: apply specialized collections, file handling, serialization, exceptions, tests, concurrency, HTTP requests and HTML parsing.

To get the most out of this book

Read an example before running it. Predict what it will do, run it with the stated inputs, then change one thing and compare the result with your prediction. Explain the change in your own words before moving on. Complete the coding challenges to practise using an idea without copying a worked example.

Use a computer on which you can install and run Python and save your own files. Follow the platform and environment guides in Getting Started. The match statement needs Python 3.10 or later. Some later examples require separately installed packages; each relevant section identifies them. Terminal commands, multi-file tests and process pools need a local environment rather than only a browser workspace.

Run examples in their stated order when they share definitions. A continuation identifies the earlier class, function or file it needs. Intentional errors explain a failure; exercise starters need completion. Live-service results, dates, random values and timings can vary.

If you use an AI assistant, begin with your own attempt. Ask for a focused hint or an explanation, then check the answer against the code and results you can reproduce. A live AI account is not required to study the book.

Download code files

Open Companion resources in the online book to download the code, notebooks and supporting files. Extract the complete bundle into a new practice folder and keep its directory structure. Read the setup instructions for the example you want to run; a notebook is a practice copy, not a replacement for the book’s explanation.

Open Companion resources.

Essential examples and output remain in the printed text, so you can reason about them before opening a computer.

Conventions used

Bold introduces an important term or highlights a screen label. Inline monospace identifies code, filenames and commands, such as Book and python example.py. Italics identify titles or provide light emphasis.

A Listing identifies a code example. Output shows what it prints and is not code to type. Expected output labels behavior that has not been executed; target output belongs to a task you must complete. Templates and signatures illustrate syntax rather than complete programs.

Figure and Table captions identify visuals and reference data. UML symbols are explained before use. Try it asks for a bounded change; a Coding Challenge asks you to apply the idea independently. Online prediction answers can be opened after your attempt; print shows them directly. Do not type terminal prompts or notebook input labels.

Continue to Getting Started →