Home / About Us / Volume I Contents / Ch 01: Python Fundamentals

Python Fundamentals for AI/ML

Chapter 1: From First Principles to Intelligent Systems

Source: The Python Tutorial
Python Software Foundation - Licensed under PSF License Version 2


Why Python Comes First

Artificial intelligence is often introduced through its most impressive final products: a model that writes prose, a classifier that recognizes a tumor image, a retrieval system that finds a buried fact, or an agent that coordinates several steps of reasoning. Those results are important, but they are not where understanding begins. Understanding begins with the ordinary machinery underneath them: names, values, lists, conditions, loops, functions, files, errors, and the discipline of making a computer do exactly what was intended.

We begin with Python because it is the working language through which much of modern artificial intelligence and machine learning (AI/ML) is taught, tested, and built. Python is not merely a convenient syntax. It is the laboratory bench on which data are cleaned, statistical ideas are made executable, models are trained, and clinical or scientific systems are inspected before they are trusted. A reader who skips these foundations may still be able to run a prepared notebook, but that reader cannot yet judge whether the notebook is correct.

For a physician, scientist, student, or institutional leader entering AI/ML, this distinction matters. The purpose of this chapter is not to make the reader memorize Python trivia. The purpose is to build enough command of the language that later chapters on statistics, machine learning, neural networks, retrieval, and agents can be read with independence rather than passive acceptance.

What This Chapter Teaches

This chapter is organized in three movements. First, we orient the reader to the working environment: Visual Studio Code, Jupyter notebooks, project structure, workspaces, and basic programming conventions. This material is included because a beginner cannot reason confidently about code if the screen, files, and execution model remain mysterious.

Second, we teach the core language elements that recur throughout the rest of the book: numbers, text, lists, control flow, functions, data structures, modules, exceptions, and classes. These are the grammar of Python. They are also the grammar of reproducible computational thought.

Third, we bridge from the language itself to the first tools of applied AI/ML: debugging, regular expressions, NumPy, Pandas, and a first machine-learning pipeline. By the end of the chapter, the reader should not only have seen Python code, but should understand why each kind of Python object and each style of instruction will reappear in later work.

Movement Sections Purpose
Orientation 0.1-0.5 Establish the editor, notebook, project, workspace, and coding habits needed to work reproducibly.
Python language core 1-9 Teach the objects and instructions that make Python programs readable and controllable.
AI/ML bridge 10-15 Connect Python fundamentals to debugging, text processing, numerical arrays, tabular data, and a first model.

Read the code cells in this chapter as demonstrations of ideas, not as isolated programming exercises. The examples are here to make the text concrete. The central aim is conceptual control: knowing what the code is doing, why it is written that way, and how to recognize when it is wrong.

Glossary and Reference Tables

Long lookup tables belong in a reference section, not in the middle of a teaching chapter. Chapter 1 therefore keeps its main text focused on the sequence of ideas and points the reader to Appendix A when a term, convention, package, or tool needs to be checked.

Use Appendix A: Glossary and Reference Tables as the chapter's reference shelf. The most useful links while reading this chapter are:

If you are new to computing or programming, skim the appendix first. If you already know the basics, use it only when a term in the chapter needs clarification.



0.1 The Development Environment: Visual Studio Code

Why the Working Environment Matters

Before we write Python, we must know where the work is being done. A programming environment is more than a blank page. It is the place where the text of a program, the files it depends on, the Python interpreter, the notebook kernel, the terminal, the debugger, and the version-control history all meet. When a beginner is confused by Python, the problem is often not Python itself. The problem is that the reader cannot yet see which part of the environment is responsible for the behavior on the screen.

Visual Studio Code (VS Code) is the environment used in this chapter because it brings those pieces into one workspace. The editor shows the source text. The terminal runs commands. The notebook interface executes cells. The debugger lets us pause a program and inspect what it is doing. Source control records what changed. These are not decorative conveniences. They are the instruments that make computational work reproducible.

The first task, therefore, is not to memorize every menu in VS Code. The first task is to recognize the few surfaces that will recur throughout the book and to understand what question each surface answers.

Surface Question it answers Why it matters
Editor What did we write? The program begins as readable text.
Explorer Where is the file? Python behavior depends on file location and project structure.
Terminal What command actually ran? Reproducibility requires knowing the exact command and environment.
Notebook kernel Which Python process executed the cell? A notebook can display old output unless the kernel state is understood.
Problems and debugger Where did execution fail? Errors are evidence. They point to the line and state that need attention.
Source control What changed since the last known state? Serious work requires memory outside the author's recollection.

Python Extensions and Interpreter Selection

VS Code becomes a Python environment through extensions. The Python extension provides language support and debugging. Pylance provides fast analysis of names, imports, and types. The Jupyter extension allows notebooks to run inside the editor. Formatters and linters help keep code consistent, but they do not replace understanding.

The most important configuration is interpreter selection. The interpreter is the Python executable that will run the code. In a project, it should usually be the interpreter inside the project's virtual environment, commonly named .venv. If a notebook or terminal uses the wrong interpreter, imports may fail, package versions may differ, and results may not be reproducible.

In practical terms, a beginner should learn three checks early:

  1. Which folder is open as the workspace?
  2. Which Python interpreter is selected?
  3. Which notebook kernel is running the current notebook?

Those questions sound mundane, but they prevent a large fraction of beginner errors. They also form the habit that later becomes essential in clinical and scientific AI systems: always identify the execution context before interpreting the result.

0.1b Navigating VS Code Without Losing the Thread

VS Code contains many panels and commands, but a learner does not need to master all of them at once. The reader needs a mental map that prevents disorientation. The window can be understood as five working regions: the Activity Bar, the Side Bar, the Editor, the Panel, and the Status Bar.

The Activity Bar is the narrow strip of icons on the left. It chooses the kind of work being shown: files, search, source control, running and debugging, or extensions. The Side Bar is the larger panel next to it. Its content changes depending on which Activity Bar icon is selected. The Editor is the central area where notebooks and source files are opened. The Panel at the bottom commonly holds the terminal, output, and problem reports. The Status Bar at the bottom gives the current branch, interpreter, kernel, line position, and other context.

This layout matters because programming requires frequent movement between reading, running, checking, and revising. A student may write a line in the editor, run it in a notebook cell, see an error in the output, inspect a variable, search for a function, and then commit the corrected file. The motions are mechanical, but the reasoning is not. The environment should make the reasoning visible instead of hiding it.

Region Primary use Habit to develop
Activity Bar Select the kind of work Know whether you are viewing files, search, source control, or debugging.
Side Bar Inspect project context Use the file tree and outline to understand where you are.
Editor Read and write source Keep source text and notebook narrative readable.
Panel Run commands and inspect output Treat output as evidence, not decoration.
Status Bar Check execution context Confirm branch, interpreter, and kernel before trusting a result.

The Command Palette is the fastest way to reach many VS Code actions. It is opened with Ctrl+Shift+P. Rather than hunting through menus, type the action you want: select interpreter, create terminal, run notebook cell, format document, or open settings. This pattern is worth learning because it turns the editor from a maze of menus into a searchable instrument.

The practical goal of this section is simple. When later chapters ask the reader to run a notebook, inspect a variable, install a package, or trace an error, the reader should know which part of VS Code is responsible for that action. Tool fluency is not the subject of this book, but without a stable tool environment, the subject of the book cannot be learned with confidence.


0.2 Jupyter Notebooks

The Notebook as a Scientific Record

A Jupyter notebook is an interactive document that combines explanation, executable Python code, displayed output, tables, and figures in one file. This format is useful for learning because it allows the reader to move from a written idea to a running example without leaving the page. It is also useful in science because it can preserve the path from data to result.

The power of a notebook, however, is also its danger. A notebook can show output that was produced earlier, perhaps before the current text or code was changed. A variable can remain in memory from a cell that is no longer visible nearby. A result can look authoritative simply because it is displayed under a cell. For this reason, learning notebooks properly means learning both how to run them and how to distrust them until the execution order is clear.

The basic unit of a notebook is the cell. A markdown cell contains explanatory text, headings, tables, equations, or narrative. A code cell contains Python instructions. The kernel is the Python process that executes those code cells. Output is the visible result returned by the kernel after a cell runs. These four ideas - cell, markdown, kernel, and output - are enough to understand most notebook behavior.

Notebook element Plain meaning Question to ask
Markdown cell Human explanation What idea is being taught?
Code cell Executable Python What instruction is being tested?
Kernel Running Python process Which interpreter and memory state are active?
Output Result of execution Was this produced by the current code in the current run?
Run order Sequence of execution Were the cells run from top to bottom?

Execution Order and Kernel State

A notebook is most trustworthy when it can be run from the first cell to the last cell without hidden preparation. This is why Run All matters. It asks whether the notebook is a reproducible document or merely a record of a private interactive session. In clinical, scientific, or engineering work, that distinction is critical.

The kernel remembers variables created by previous code cells. If a later cell uses a variable, the result depends on whether that earlier variable still exists and what value it currently holds. Restarting the kernel clears that memory. Running all cells after a restart is therefore the simplest test of whether the notebook actually contains the full argument.

The practical habit is straightforward:

  1. Select the intended project interpreter as the notebook kernel.
  2. Restart the kernel when the notebook state is uncertain.
  3. Run the notebook from top to bottom.
  4. Treat any failure as information about what assumption was missing.

This habit will matter throughout the book. Later chapters will use notebooks to examine data, train models, build retrieval systems, and reason about agents. Each of those tasks depends on the same discipline introduced here: the displayed result is meaningful only when the path that produced it is known.

0.2b Working Productively in Notebook Cells

The notebook interface gives the reader many buttons, shortcuts, and panels. The important point is not to memorize the interface. The important point is to understand what each action does to the record of thought.

Running a cell sends its code to the kernel. Running a cell and advancing moves the reader to the next step in the argument. Inserting a cell changes the structure of the explanation. Converting a cell to markdown changes it from executable instruction to written narrative. Clearing output removes displayed results but does not by itself prove that the notebook will run again. Restarting the kernel removes memory and forces the notebook to prove itself from the beginning.

Action Meaning Why it matters
Run current cell Execute one step Useful for focused inspection.
Run all cells Rebuild the full argument Tests whether the notebook is reproducible.
Restart kernel Clear Python memory Removes hidden variables and stale state.
Clear outputs Remove displayed results Prepares the notebook for review, but does not validate it.
Insert markdown cell Add explanation Keeps reasoning visible to the reader.
Insert code cell Add a computational step Changes what the notebook actually does.

The Variables viewer, Outline, breadcrumbs, and cell status indicators are useful because they expose state that would otherwise be hidden. The Variables viewer shows what names currently exist in memory. The Outline shows the chapter structure created by headings. Cell status indicators show whether a cell has run, is running, or failed. These details may seem minor in a beginner chapter, but they become essential when the notebook grows into a scientific or clinical workflow.

The safest rule is to treat a notebook as both a lesson and a laboratory notebook. The prose should explain the intention. The code should perform one understandable step. The output should be interpretable. The sequence should be repeatable. When those four conditions are met, a notebook is not merely an interactive worksheet; it is a reproducible explanation.


0.3 Anatomy of a Python Project

Why Files and Folders Are Part of the Program

A Python project is not only the code inside one cell or one script. It is the organized collection of files that tells the reader, the interpreter, and future collaborators how the work is meant to run. The folder structure says where the source code lives, where configuration belongs, where tests are placed, what dependencies are required, and which files should never be committed to version control.

This matters because many programming errors are really project-structure errors. A module cannot be imported because the package is not organized correctly. A notebook works on one machine but not another because the dependencies were never recorded. A secret key appears in a file because configuration and secrets were not separated. A model result cannot be reproduced because the environment was not described.

The point of project anatomy is therefore not neatness for its own sake. The point is reproducibility, safety, and intelligibility. A well-structured project lets a new reader answer three questions quickly: what is the project, how is it run, and what must not be exposed.

The Main Parts of a Python Project

Most Python projects contain the same kinds of parts even when their names differ. A beginner should learn the purpose of each part before memorizing any particular layout.

Project part Usual role Why it matters
Source package Holds the program's Python modules Separates reusable logic from notebooks and scratch work.
__init__.py Marks a folder as a Python package Allows Python to treat related modules as one package.
__main__.py or entry script Defines how the project starts Gives the project a deliberate execution path.
Virtual environment Holds project-specific packages Prevents one project from silently depending on another project's tools.
Dependency file Records required packages Lets another machine rebuild the same software environment.
Configuration file Stores non-secret settings Keeps behavior explicit without hard-coding every value.
Environment file Stores local secrets or private values Must be protected and excluded from version control.
Tests Check expected behavior Turn assumptions into repeatable evidence.
README or documentation Explains purpose and usage Helps a future reader understand the project without guessing.
.gitignore Lists files Git should ignore Prevents secrets, caches, outputs, and virtual environments from being committed.

Packages, Configuration, and Secrets

The source package is the part of the project that should become dependable. It holds functions, classes, and modules that can be imported and tested. Notebooks may teach ideas or demonstrate results, but the reusable machinery should usually live in ordinary Python files. This separation becomes increasingly important as the book moves from small examples to pipelines, retrieval systems, and clinical AI workflows.

Configuration is the set of values that control how a program runs: file paths, model names, service endpoints, feature flags, and other settings. These values should be visible enough that the behavior of the program can be understood, but they should not be mixed randomly into the code. Secrets are different. Passwords, tokens, connection strings, and private keys must not be stored in source code or committed to Git. A project that cannot distinguish configuration from secrets is not ready for serious use.

The Reproducibility Habit

The reproducibility habit begins early. When starting a project, ask what another reader would need in order to run it tomorrow on a different machine. The answer usually includes the source files, the dependency list, the intended Python version, the command that starts the work, and enough documentation to understand the purpose. It should not include private credentials, generated caches, or local machine clutter.

This habit is not administrative overhead. It is the same discipline that later allows a clinical model, a data pipeline, or an AI agent to be reviewed. If the project structure is confusing at the beginning, the scientific or clinical claim built on top of it will become harder to trust.


0.4 Multi-Project Workspace Layout

Why One Repository May Contain More Than One Project

As projects become more serious, a single repository may hold more than one working component. A clinical AI system, for example, may contain a web application, a data pipeline, tests, deployment scripts, documentation, and notebooks. These parts belong together because they support one institutional purpose, but they should not be confused with one another.

The organizing principle is separation with coordination. Each project needs its own clear place for source code, dependencies, configuration, tests, and local private values. Shared policy and documentation can live at the repository root. This lets the whole system be governed as one body while still allowing each component to run in a controlled environment.

The long file-by-file vocabulary for this topic is maintained in Appendix A, Project Structure and Dependencies. The main idea to carry forward is simpler: the folder layout is part of the design. It tells future readers what belongs together, what must stay private, what can be reproduced, and where execution begins.

The Practical Rule

A beginner should ask four questions when looking at a workspace:

  1. Which folder is the repository root?
  2. Which subfolder contains the project I am working on?
  3. Which Python environment and dependency file belong to that project?
  4. Which files are local secrets or generated artifacts and therefore should not be committed?

These questions are not clerical. They are the first form of software governance. If the workspace layout is unclear, later claims about a model, pipeline, or agent will also become unclear.

0.5 Coding Habits and Professional Conventions

Code is read far more often than it is written. This is why conventions matter. A convention is not a decoration added after the real work is done. It is a shared agreement that allows a future reader to understand the author's intention with less guessing.

The most important habit is naming. A name should tell the reader what role a value, function, class, or file plays in the program. A name such as patient_age carries meaning. A name such as x usually does not, unless the surrounding mathematical expression makes the convention obvious. Clear names are not a beginner luxury; they are the first defense against confusion.

The second habit is modularity. A growing program should move from isolated statements, to functions, to modules, to packages. This progression is not about elegance. It is about making each idea small enough to test, explain, reuse, and correct. Repeated code should make the author suspicious, because every copy becomes another place for the same bug to survive.

The third habit is disciplined error reading. Beginners often treat errors as failures. Professionals treat errors as evidence. A traceback gives the type of error, the message, and the path of execution that produced it. Later sections will teach this directly, but the attitude belongs here: do not hide errors until you understand them.

The detailed lookup tables for naming conventions and common beginner traps are in Appendix A, Coding Conventions and Beginner Traps. The chapter keeps the main lesson in prose because the goal is not to memorize a table. The goal is to understand why readable, modular, reproducible code is the beginning of scientific and clinical reliability.


Section 1: Numbers as the First Model of Computation

Python begins with numbers because numbers make computation visible. Before a program can classify an image, search a corpus, optimize a neural network, or summarize a clinical note, it must first evaluate expressions correctly. This section uses arithmetic as the simplest possible laboratory for that idea.

The interpreter can act like a calculator, but the deeper lesson is not the calculator. The deeper lesson is that Python reads an expression, applies rules, produces a value, and assigns that value a type. Once that rhythm is clear, later topics such as arrays, tensors, probabilities, loss functions, and model outputs become less mysterious.

A few operators are worth knowing immediately because they appear throughout scientific Python:

Operator Meaning Example Why it matters
+ Addition $2 + 2 = 4$ Combines quantities
- Subtraction $50 - 5 \times 6 = 20$ Measures differences
* Multiplication $5 \times 6 = 30$ Scales a quantity
/ Division $8 / 5 = 1.6$ Produces a floating-point result
// Floor division $17 // 3 = 5$ Keeps the whole-number quotient
% Modulo $17 \% 3 = 2$ Keeps the remainder
** Exponentiation $2^{7} = 128$ Expresses powers and growth

This table is short enough to stay in the chapter because it supports the first examples directly. Larger lookup material belongs in Appendix A, Python Language. The main point here is conceptual: every expression has both a value and a type.

Python's ordinary numeric types are int, float, and complex. An int represents whole numbers such as 2 or 20. A float represents decimal approximations such as 1.6 or 5.0. A complex value represents a real and imaginary component, written with j, as in 3+5j. Specialized types such as Decimal and Fraction exist when exact decimal or rational arithmetic is required, but they are not the starting point for AI/ML work.

One rule deserves early attention: ordinary division with / returns a float, even when the result looks like a whole number. This is not a nuisance. It is Python telling you that division belongs to a different numerical world than counting. Later, when measurements, probabilities, gradients, and loss values enter the story, that distinction will matter.

In [1]:
# === Section 1: Numbers ===
# Python works as a calculator right out of the box.
# print() is a built-in function that displays output below the cell.

# Basic arithmetic — the four operations you already know
print("2 + 2          =", 2 + 2)          # addition
print("50 - 5*6       =", 50 - 5*6)      # subtraction & multiplication (* means multiply)
print("(50 - 5*6) / 4 =", (50 - 5*6) / 4)  # parentheses control order, / is division
print("8 / 5          =", 8 / 5)         # division ALWAYS returns a float (decimal)
2 + 2          = 4
50 - 5*6       = 20
(50 - 5*6) / 4 = 5.0
8 / 5          = 1.6
In [2]:
# Floor division (//) drops the decimal, modulo (%) gives the remainder,
# and ** raises to a power (exponentiation).
print("17 / 3  =", 17 / 3)    # classic division → float (5.666...)
print("17 // 3 =", 17 // 3)   # floor division → int (drops the .666)
print("17 % 3  =", 17 % 3)    # modulo → remainder after dividing (17 = 5*3 + 2)
print("5 ** 2  =", 5 ** 2)    # 5 squared (5 × 5 = 25)
print("2 ** 7  =", 2 ** 7)    # 2 to the power 7 (2×2×2×2×2×2×2 = 128)
17 / 3  = 5.666666666666667
17 // 3 = 5
17 % 3  = 2
5 ** 2  = 25
2 ** 7  = 128
In [3]:
# Variables store values so you can reuse them later.
# The = sign is ASSIGNMENT ("put this value into this name"), not math equality.
width = 20             # create a variable called 'width', assign value 20
height = 5 * 9         # expressions on the right side are evaluated first, then assigned
print("width * height =", width * height)  # use * to multiply the two variables

# When you mix int and float in an expression, Python promotes int → float automatically
print("4 * 3.75 - 1 =", 4 * 3.75 - 1)    # int 4 × float 3.75 → float result
width * height = 900
4 * 3.75 - 1 = 14.0

Formatting Numbers for Readers

Computation is not finished when Python has the correct value. A program also has to communicate the value clearly. In clinical, scientific, and business settings, careless number display can make correct arithmetic look untrustworthy. Too many decimal places distract the reader; too few can hide meaning.

Python's f-strings solve this problem by letting the author place a value inside text and specify how that value should appear. The expression f"{value:.2f}" means: insert value, display it as a fixed-point number, and show two digits after the decimal point.

A compact reference is enough for the examples below:

Specifier Meaning Example display
.2f Fixed-point, 2 decimal places 100.50
.4f Fixed-point, 4 decimal places 3.1416
>12.2f Right-align in a 12-character field 100.50
.2e Scientific notation 1.01e+02
.2% Percentage 12.50%
,.2f Thousands separator 1,234,567.89

The point is not to memorize every specifier now. The point is to notice that presentation is part of computation. A model output, a laboratory value, a survival estimate, or a cost calculation must be both computed and shown in a form that a reader can interpret.

In [4]:
# f-strings: prefix a string with f to embed variables inside {curly braces}.
# The :.2f inside the braces is a FORMAT SPECIFIER: show 2 decimal places, fixed-point.

tax = 12.5 / 100       # 12.5% as a decimal (0.125)
price = 100.50          # the item price
tax_amount = price * tax           # calculate tax
total = price + tax_amount         # add tax to price
print(f"Price: ${price:.2f}")      # :.2f → 2 decimal places → "100.50"
print(f"Tax:   ${tax_amount:.2f}") # tax_amount = 12.5625 → displayed as "12.56"
print(f"Total: ${round(total, 2):.2f}")  # round() rounds to 2 decimals first
Price: $100.50
Tax:   $12.56
Total: $113.06
In [5]:
# Format specifier showcase — these all go inside f-string braces: {value:specifier}
# The :<12 and :>12 control LEFT and RIGHT alignment within a 12-character-wide field.
tax = 12.5 / 100
price = 100.50

print(f"{'Format':<12} {'Result':>12}")    # :<12 left-align, :>12 right-align
print(f"{'-'*12} {'-'*12}")                 # '-'*12 repeats the dash 12 times
print(f"{{:.2f}}       {price:>12.2f}")     # .2f = 2 decimal fixed-point
print(f"{{:.4f}}       {price:>12.4f}")     # .4f = 4 decimal places
print(f"{{:>12.2f}}    {price:>12.2f}")     # >12 = right-align in 12-char field
print(f"{{:.2e}}       {price:>12.2e}")     # .2e = scientific notation (1.01e+02)
print(f"{{:.2%}}       {tax:>12.2%}")       # .2% = percentage format (12.50%)
print(f"{{:,.2f}}      {1234567.89:>12,.2f}")  # , = thousands separator
Format             Result
------------ ------------
{:.2f}             100.50
{:.4f}           100.5000
{:>12.2f}          100.50
{:.2e}           1.00e+02
{:.2%}             12.50%
{:,.2f}      1,234,567.89

Section 2: Text, Strings, and the Shape of Meaning

Numbers taught the first rule of computation: Python evaluates an expression and gives the result a type. Text introduces the next rule: many values are not quantities at all. A patient name, a diagnosis, a file path, a gene symbol, a medication instruction, a paragraph from a note, and a URL are all represented as text. In Python, the type for text is str, short for string.

A string is a sequence of characters. That definition is simple, but it carries a great deal of power. Because a string is a sequence, Python can ask for the first character, the last character, a range of characters, or the length of the whole value. Because a string is text, Python can also search it, clean it, split it, join it, change its case, and prepare it for display. Much of data science begins with this quiet work of making text reliable enough to analyze.

Strings can be written with single quotes, double quotes, or triple quotes. Single and double quotes are equivalent; the practical choice is usually readability. If the text contains an apostrophe, double quotes may avoid an escape character. If the text spans several lines, triple quotes can preserve that shape. Raw strings, written with an r prefix, are useful when backslashes should remain literal, especially in Windows paths and regular expressions.

Indexing: positions inside text

Indexing means selecting one character by its position. Python starts counting at zero. In the word Python, the character P is at index 0, y is at index 1, and n is at index 5. Negative indexes count backward from the end, so -1 means the last character. This convention may feel unnatural at first, but it becomes consistent across strings, lists, arrays, and many later AI/ML data structures.

Think of the indexes as positions on a ruler, not as ordinary counting labels. Once that mental model is in place, the notation stops feeling arbitrary. It becomes a precise way to refer to a location inside a sequence.

Slicing: taking a meaningful segment

Slicing selects a range of characters with s[start:stop]. The start position is included; the stop position is excluded. For example, word[0:2] gives the first two characters of Python, because it includes positions 0 and 1 but stops before position 2. This half-open rule appears throughout Python, including range(), lists, arrays, and table operations.

There is a useful invariant behind slicing: s[:i] + s[i:] reconstructs the original string. The left slice takes everything before position i; the right slice takes everything from position i onward. The two pieces meet without overlap and without a gap. This is one of the small rules that makes Python feel coherent once you see it.

Immutability: text is replaced, not changed in place

Strings are immutable. That means an existing string cannot have one character changed in place. If word is Python, Python will not allow word[0] = 'J'. Instead, you build a new string from pieces of the old one, such as 'J' + word[1:]. This is not a limitation to resent. It is a design rule that makes text values stable and predictable. Later, when lists are introduced, the contrast between immutable strings and mutable lists will explain many beginner errors before they happen.

The official technical reference for strings is the Python documentation page for str, the text sequence type. Use it when you want the exact language rules; use this chapter to understand why those rules matter.

In [6]:
# === Section 2: Strings ===
# Strings are sequences of characters enclosed in quotes.
# Python accepts both single quotes ' ' and double quotes " " — they're equivalent.

print('spam eggs')               # single quotes — the simplest way
print("Paris rabbit :)")         # double quotes — useful when string contains '
print('doesn\'t')               # backslash \ ESCAPES a special character (the quote)
print("doesn't")                # ...or just use the OTHER quote type to avoid escaping
spam eggs
Paris rabbit :)
doesn't
doesn't
In [7]:
# Special characters: \n means "newline" (start a new line)
s = 'First line.\nSecond line.'  # \n is an ESCAPE SEQUENCE embedded in the string
print(s)                          # prints on two lines
print()                           # print() with no arguments just prints a blank line

# Raw strings: prefix with r to disable escape sequences (useful for file paths)
print(r'C:\this\name')           # r'' makes \n literal, not a newline
First line.
Second line.

C:\this\name
In [8]:
# Concatenation (+) joins strings; repetition (*) repeats them
print(3 * 'un' + 'ium')          # 'un' repeated 3 times + 'ium' → 'unununium'

# Adjacent string LITERALS (not variables) auto-concatenate — useful for long text
text = ('Put several strings within parentheses '   # line 1 of the string
        'to have them joined together.')             # auto-joined to line 1
print(text)
unununium
Put several strings within parentheses to have them joined together.
In [9]:
# INDEXING: access individual characters by position (0-based — first char is index 0)
word = 'Python'
print("word[0]  =", word[0])     # 'P' — first character (index 0)
print("word[5]  =", word[5])     # 'n' — sixth character (index 5)
print("word[-1] =", word[-1])    # 'n' — NEGATIVE index counts from the end (-1 = last)
print("word[-2] =", word[-2])    # 'o' — second from the end
print()

# SLICING: extract a substring with [start:stop] — start included, stop EXCLUDED
print("word[0:2] =", word[0:2])  # 'Py' — characters at index 0 and 1 (not 2)
print("word[2:5] =", word[2:5])  # 'tho' — indices 2, 3, 4
print("word[:2]  =", word[:2])   # 'Py' — omit start → starts from 0
print("word[4:]  =", word[4:])   # 'on' — omit stop → goes to end
print()

# The INVARIANT: any split point reassembles the original string
print("word[:2] + word[2:] =", word[:2] + word[2:])  # 'Py' + 'thon' = 'Python'
print("len(word) =", len(word))  # len() returns the number of characters
word[0]  = P
word[5]  = n
word[-1] = n
word[-2] = o

word[0:2] = Py
word[2:5] = tho
word[:2]  = Py
word[4:]  = on

word[:2] + word[2:] = Python
len(word) = 6
In [10]:
# Strings are IMMUTABLE — once created, individual characters CANNOT be changed.
# This would raise TypeError: word[0] = 'J'

# Instead, build a NEW string from parts of the old one:
print('J' + word[1:])   # 'J' + 'ython' → 'Jython' (replaced first character)
print(word[:2] + 'py')  # 'Py' + 'py' → 'Pypy' (replaced everything after index 2)
Jython
Pypy

String Methods: named operations on text

A method is a function that belongs to an object. A string method is therefore a named operation that belongs to a string. This dot notation is important: name.lower() reads as, take this particular string named name and ask it to produce a lowercase version. The method acts on the value to its left.

String methods are not a random vocabulary list. They represent recurring text tasks: normalize case, remove surrounding whitespace, search for a substring, replace one phrase with another, split text into parts, and join parts back into a coherent string. These tasks appear constantly in clinical notes, filenames, web data, natural language processing, and API responses.

The examples below show the pattern rather than every possible method. For the complete instruction manual, use the official Python documentation for str and its methods.

In [11]:
# String methods are named operations attached to a string value.
# This compact set covers the pattern used by many other text methods.

name = "  SAnJaY Awasthi  "
clean_name = name.strip().casefold().title()
print("clean name:", clean_name)

filename = "report_final.csv"
print("is data file?:", filename.endswith((".csv", ".tsv", ".xlsx")))

message = "AI,ML,DL"
parts = message.split(",")
print("parts:", parts)
print("joined:", " / ".join(parts))

url = "https://example.com/page"
protocol, separator, rest = url.partition("://")
print(f"protocol={protocol}, rest={rest}")
casefold:   sanjay
capitalize: Sanjay
center:     _______Sanjay_______
count 'an': 2
count 'a':  3
find 'nan': 2
find 'xyz': -1
is data?: True
is temp?: False
tabs:  01      012     0123
replace: Hello Python World
join: AI / ML / DL
strip:   'data science'
lstrip:  'data science   '
protocol=https, rest=example.com/page
step=2: ra e
reverse: # 321 gninraeL taerG

Section 3: Lists, Mutable Sequences, and Working Collections

Strings taught sequence: a value can have an order, positions, slices, and a length. Lists keep that sequence idea and add a new power: mutability. A list is an ordered collection inside square brackets. It can hold numbers, strings, other lists, or a mixture of object types. More importantly, a list can be changed after it is created.

This is the first major contrast with strings. A string is stable; when you want different text, you build a new string. A list is a working container; when you want to add, remove, or replace an item, you can often change the existing list. That is useful for accumulating results, cleaning records, building batches of files, collecting model outputs, and representing rows of data before they become arrays or tables.

Because lists are sequences, the indexing and slicing rules from strings carry forward. items[0] means the first item, items[-1] means the last item, and items[1:4] means a new list containing positions 1, 2, and 3. The reader should notice the continuity: Python is not inventing a new grammar for every object. It is reusing a small number of patterns across many structures.

Mutability: the power and the danger

Mutability is powerful because a program can build a result step by step. It is dangerous because two names can point to the same list. If rgba = rgb, Python has not copied the list. It has created a second name for the same object. A change through rgba is therefore visible through rgb. This is not a bug in Python; it is the consequence of names referring to objects.

The practical rule is simple: assignment gives another name to the same list; slicing, .copy(), or list() can create a shallow copy. A shallow copy is enough when the list contains simple values. If the list contains other lists, the inner lists are still shared, and deeper copying requires deliberate care. This idea will return later when data structures, nested records, and model inputs become more complicated.

Methods: changing the collection intentionally

List methods are named operations for managing a collection. .append() adds one item at the end. .extend() adds each item from another iterable. .copy() makes a shallow copy. .reverse() reverses the list in place. The phrase in place matters: some methods change the existing list rather than returning a new one. That is the central behavioral difference from string methods.

For the official instruction manual, use Python's tutorial section More on Lists. The chapter explains the conceptual progression; the official page is the precise reference for available list methods and their exact behavior.

In [12]:
# Lists are ordered and mutable collections.
# They use the same indexing and slicing grammar introduced with strings.

squares = [1, 4, 9, 16, 25]
print("squares       =", squares)
print("squares[0]    =", squares[0])
print("squares[-1]   =", squares[-1])
print("squares[-3:]  =", squares[-3:])
print("combined      =", squares + [36, 49, 64])
squares       = [1, 4, 9, 16, 25]
squares[0]    = 1
squares[-1]   = 25
squares[-3:]  = [9, 16, 25]
concatenation = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
In [13]:
# A list can be corrected and extended in place.

cubes = [1, 8, 27, 65, 125]
print("before fix:", cubes)

cubes[3] = 64
cubes.append(216)
cubes.append(7 ** 3)

print("after fix: ", cubes)
Before fix: [1, 8, 27, 65, 125]
After fix:  [1, 8, 27, 64, 125]
Extended:   [1, 8, 27, 64, 125, 216, 343]
In [14]:
# Assignment gives a second name to the same list.
# Copying creates a separate list object.

rgb = ["Red", "Green", "Blue"]
rgba = rgb
rgba.append("Alpha")

print("rgb after rgba.append:", rgb)
print("same object?:", id(rgb) == id(rgba))

original = ["Red", "Green", "Blue"]
copy1 = original[:]
copy2 = original.copy()
copy3 = list(original)

copy1.append("Alpha")
print("original:", original)
print("copy1:   ", copy1)
print("copies are separate?:", original is not copy1 and original is not copy2 and original is not copy3)
Same object? True
rgb after rgba.append: ['Red', 'Green', 'Blue', 'Alpha']
Original: ['Red', 'Green', 'Blue']
Copy:     ['Red', 'Green', 'Blue', 'Alpha']

List methods: adding, extending, reversing, and nesting

The most important beginner distinction is .append() versus .extend(). .append(x) places x into the list as one item. If x is itself a list, the whole list becomes a nested item. .extend(x) walks through x and adds its elements one by one. The difference is not cosmetic; it changes the shape of the data.

This is the same kind of shape discipline that later matters in NumPy arrays, pandas tables, tensors, and model inputs. A flat list and a nested list may print similarly at a glance, but they behave differently. A careful programmer learns to ask not only what values are present, but also what structure holds them.

In [15]:
# append() adds one item; extend() adds each item from another iterable.

a = [1, 2, 3]
b = [1, 2, 3]

a.extend([4, 5])
b.append([4, 5])

print("extend result:", a)
print("append result:", b)

names = ["Alice", "Bob", "Charlie"]
names.reverse()
print("reversed names:", names)

grid = [[0] * 3] * 3
grid[0][0] = 99
print("shared-row grid:", grid)

grid_ok = [[0] * 3 for _ in range(3)]
grid_ok[0][0] = 99
print("independent rows:", grid_ok)
extend → [1, 2, 3, 4, 5]
append → [1, 2, 3, [4, 5]]
zeros: [0, 0, 0, 0, 0]
grid trap: [[99, 0, 0], [99, 0, 0], [99, 0, 0]]
grid ok:   [[99, 0, 0], [0, 0, 0], [0, 0, 0]]
Common elements (loop): 5
Common elements (set):  5 → {33, 66, 99, 55, 88}
reversed: ['Charlie', 'Bob', 'Alice']
Before swap: 12 7
After swap:  7 12
In [16]:
# Lists can contain other lists, creating nested structures.

letters = ['a', 'b', 'c']
numbers = [1, 2, 3]
nested = [letters, numbers]

print("nested       =", nested)
print("nested[0]    =", nested[0])
print("nested[0][1] =", nested[0][1])
print("len(letters) =", len(letters))
x      = [['a', 'b', 'c'], [1, 2, 3]]
x[0]   = ['a', 'b', 'c']
x[0][1]= b
len(a) = 3

Section 3.2: Fibonacci as the First Control-Flow Story

The Fibonacci example is not here merely because the sequence is famous. It is here because it brings together the ideas introduced so far and prepares the reader for control flow. A program now has numbers, names, assignment, and collections. The next question is how a program decides what to do repeatedly.

The Fibonacci sequence begins with 0 and 1. Each later number is the sum of the two numbers before it. In mathematical notation, that idea is compact. In a program, it becomes a small working process: remember two values, print or store the current one, advance both values, and continue while a condition remains true.

This example introduces three ideas that will reappear throughout Python. First, multiple assignment lets a, b = b, a + b update two names as one coordinated step. Second, a while loop repeats as long as a condition is true. Third, indentation is not decoration in Python; it defines which statements belong to the loop.

The important lesson is sequence of thought. We are no longer asking Python to evaluate one expression. We are asking Python to maintain state across repeated steps. That is the beginning of algorithmic thinking.

In [17]:
# Each Fibonacci number is the sum of the two before it.
# The loop keeps state in a and b, then advances both names together.

a, b = 0, 1
while a < 100:
    print(a, end=' ')
    a, b = b, a + b
print()
0 1 1 2 3 5 8 13 21 34 55 89 

Section 4: Control Flow, Decisions, and Repetition

A program becomes useful when it can choose and repeat. Up to this point, most examples have moved from top to bottom in a straight line. Control flow changes that. It lets a program ask a question, take one branch instead of another, loop through a collection, stop early, skip one iteration, or match a value against a structured pattern.

Control flow is the grammar of computational judgment. In clinical and scientific work, this is where rules begin to look like reasoning: if a value is missing, handle it differently; for each record, clean the field; while a process is unfinished, continue; if a pattern is found, take the appropriate action.

The official Python tutorial chapter on control-flow tools is the technical reference for if, for, range(), break, continue, pass, match, and function definitions. This chapter explains how those tools fit into a beginner's mental model.

4.1 Conditional logic with if

An if statement asks whether a condition is true. If it is true, Python runs the indented block below it. If it is false, Python skips that block and may test an elif condition or fall through to else. The word elif means else-if; it gives a sequence of mutually exclusive tests without making the code drift into excessive nesting.

The central habit is to read the condition as a question. Is x negative? Is the status inactive? Did the search find a match? A good condition is not just legal Python. It is a sentence the reader can understand.

Indentation is part of the language. The indented lines are the body of the branch. When the indentation returns to the previous level, the branch is over. This is why a Python program can often be read visually: the shape of the code is the shape of the logic.

In [18]:
# if, elif, and else express a chain of choices.

x = 42
if x < 0:
    x = 0
    print('Negative changed to zero')
elif x == 0:
    print('Zero')
elif x == 1:
    print('Single')
else:
    print('More')
More

4.2 Iteration with for

A for loop walks through an iterable one item at a time. An iterable is any object that can provide its elements in sequence: a list, a string, a tuple, a dictionary view, a file, a range, or many objects introduced later.

The simplest reading is: for each item in this collection, run the following block. That wording matters because Python usually gives you the item itself, not just a numeric position. When you need both the position and the item, enumerate() provides them together.

A loop should not usually change the collection it is walking through. If items must be removed or transformed, iterate over a copy or build a new collection. That habit prevents subtle bugs where the loop's target changes underneath it.

In [19]:
# A for loop gives each item in the collection to the loop body.

words = ['cat', 'window', 'defenestrate']
for word in words:
    print(word, len(word))
cat 3
window 6
defenestrate 12
In [20]:
# If you need to remove items, iterate over a copy.

users = {'Hans': 'active', 'Eleanor': 'inactive', 'Keitaro': 'active'}
for user, status in users.copy().items():
    if status == 'inactive':
        del users[user]

print(users)
{'Hans': 'active', '景太郎': 'active'}

4.3 Numbered iteration with range()

range() represents a sequence of integers. It is most often used when the number of repetitions matters, when an index is needed, or when a simple arithmetic progression is the natural description of the task.

The endpoint rule is the key: range(stop) begins at 0 and stops before stop. Therefore range(5) gives five values, 0 through 4. This half-open interval is consistent with slicing. It is one of Python's quiet unifying ideas: the stop boundary is excluded.

A range is lazy. It represents the sequence without building the whole list in memory. That makes it suitable for large loops and explains why list(range(...)) is used only when the actual list display is needed.

In [21]:
# range(stop), range(start, stop), and range(start, stop, step).

for i in range(5):
    print(i, end=' ')
print()

print(list(range(5, 10)))
print(list(range(0, 10, 3)))
print(list(range(-10, -100, -30)))
print('sum(range(4)) =', sum(range(4)))
0 1 2 3 4 
[5, 6, 7, 8, 9]
[0, 3, 6, 9]
[-10, -40, -70]
sum(range(4)) = 6
In [22]:
# enumerate() gives both the index and the value.

words = ['Mary', 'had', 'a', 'little', 'lamb']
for index, word in enumerate(words):
    print(index, word)
0 Mary
1 had
2 a
3 little
4 lamb

4.4 break, continue, and loop else

Loops sometimes need more than ordinary repetition. break exits the nearest loop immediately. continue skips the rest of the current iteration and moves to the next one. A loop else block runs only if the loop finishes without hitting break.

The loop else feature is unusual, but its logic is clear when read as a search pattern. Search through the candidates. If the target is found, break. If the loop ends without break, the else block means the search failed. This can be elegant when used sparingly and confusing when overused.

In [23]:
# Search for factors. If no factor is found, the loop else marks a prime.

for n in range(2, 10):
    for x in range(2, n):
        if n % x == 0:
            print(f'{n} equals {x} * {n // x}')
            break
    else:
        print(f'{n} is a prime number')
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3
In [24]:
# continue skips the rest of one iteration.

for number in range(2, 10):
    if number % 2 == 0:
        print(f'Found an even number {number}')
        continue
    print(f'Found an odd number {number}')
Found an even number 2
Found an odd number 3
Found an even number 4
Found an odd number 5
Found an even number 6
Found an odd number 7
Found an even number 8
Found an odd number 9

Practical loop patterns

The examples above introduce the grammar. Real programs combine the grammar into patterns: stepping through positions, collecting selected values, nesting loops to cover a grid, and accumulating a result under a condition. The goal is not to memorize a catalog. The goal is to recognize the shape of a problem and choose the loop that expresses it clearly.

In [25]:
# A few recurring loop shapes.

print('Even indices:', list(range(0, 10, 2)))
print('Countdown:  ', list(range(5, 0, -1)))

url = 'analyticsvidhya.com'
even_chars = [url[index] for index in range(len(url)) if index % 2 == 0]
print('Even-index chars:', ''.join(even_chars))

print('3 by 3 multiplication table:')
for row in range(1, 4):
    for col in range(1, 4):
        print(f'{row * col:4d}', end='')
    print()
Even indices: [0, 2, 4, 6, 8]
Countdown:   [5, 4, 3, 2, 1]
Even-index chars: aayisihacm

3 × 3 Multiplication Table:
   1   2   3
   2   4   6
   3   6   9

Marks 67 → Average

While loops

A for loop is best when there is a collection to traverse or a known progression to follow. A while loop is best when continuation depends on a condition whose ending may not be known in advance. Keep going while the process is unfinished. Stop when the condition becomes false or when a deliberate break is reached.

This makes while loops powerful, but also risky. If the condition never changes, the loop never ends. A professional while loop should make its exit condition visible. The reader should be able to see what will eventually make the loop stop.

In [26]:
# A sentinel value tells the loop when to stop.

total = 0
values = [10, 20, 30, -1, 40, 50]
index = 0

while index < len(values) and values[index] != -1:
    total += values[index]
    index += 1

print(f'Sum before sentinel: {total}')
Collatz(27) reached 1 in 111 steps
Sum before sentinel: 60

4.6 The pass placeholder

pass does nothing. That sounds trivial, but it has a practical use: it lets the author reserve a syntactically valid block while leaving implementation for later. Python requires an indented body after class, def, if, for, and while. When the body is intentionally empty for the moment, pass says so explicitly.

class EmptyForNow:
    pass

def not_yet_implemented():
    pass

4.7 Structural pattern matching with match

match compares one value against a set of patterns. At its simplest, it can replace a long chain of equality checks. At its most powerful, it can unpack structured values such as tuples, lists, dictionaries, or objects and bind parts of the matched value to names.

The wildcard pattern _ means anything not already matched. The vertical bar | means or. A pattern can also capture part of a structure, as in (0, y), which matches a point whose first coordinate is zero and gives the second coordinate the name y.

Pattern matching should be used when the shape of the data is part of the decision. For simple yes-or-no logic, if is usually clearer. For structured cases, match can make the branches read like the data itself.

In [27]:
# match compares a value against patterns.

def http_error(status):
    match status:
        case 400:
            return 'Bad request'
        case 404:
            return 'Not found'
        case 401 | 403:
            return 'Not allowed'
        case _:
            return 'Unhandled status'

for code in [200, 400, 401, 404]:
    print(f'HTTP {code}: {http_error(code)}')
HTTP 200: Something's wrong with the internet
HTTP 400: Bad request
HTTP 401: Not allowed
HTTP 404: Not found
HTTP 418: I'm a teapot
In [28]:
# Pattern matching can unpack structure and capture values.

points = [(0, 0), (0, 5), (3, 0), (2, 7)]
for point in points:
    match point:
        case (0, 0):
            print(f'{point} -> Origin')
        case (0, y):
            print(f'{point} -> Y={y}')
        case (x, 0):
            print(f'{point} -> X={x}')
        case (x, y):
            print(f'{point} -> X={x}, Y={y}')
(0, 0) → Origin
(0, 5) → Y=5
(3, 0) → X=3
(2, 7) → X=2, Y=7

Section 5: Functions as Named, Reusable Thought

Control flow gave the program movement: choose this branch, repeat that block, stop when the condition changes. Functions give that movement a name. A function packages a sequence of operations so the program can call it again with new inputs. This is how a notebook grows from isolated cells into reusable software.

A function definition begins with def, followed by the function name, parameters in parentheses, a colon, and an indented body. The definition stores the instructions; it does not run them immediately. The function runs only when it is called. This distinction matters because it lets the author separate design from execution.

A well-named function is also a sentence in the program's argument. calculate_bmi(), clean_patient_name(), and retrieve_documents() tell the reader what idea has been packaged. Good functions reduce repetition, make testing possible, and give later chapters a way to build pipelines from understandable parts.

The official Python reference for this topic begins at Defining Functions. The related sections on default argument values, keyword arguments, lambda expressions, and function annotations are useful technical companions to the explanation here.

Returning values versus printing values

A function can print information for a human reader, return a value to the program, or do both. Printing is communication. Returning is computation. When a function returns a value, the caller can store it, pass it to another function, test it, or combine it with other values. Most serious programs depend on returned values because returned values allow one piece of logic to feed the next.

Parameters, arguments, and defaults

A parameter is the name used inside the function definition. An argument is the actual value supplied when the function is called. Defaults let the author choose ordinary behavior while still allowing callers to override it. That makes a function easier to call in the common case without hiding its flexibility.

Default values have one important rule: they are evaluated once, when the function is defined. That is usually harmless for immutable defaults such as numbers, strings, or None. It is dangerous for mutable defaults such as lists or dictionaries, because the same object can be shared across calls. The professional habit is to use None as a sentinel and create a fresh list or dictionary inside the function body.

Argument styles and readable calls

Python supports positional arguments, keyword arguments, flexible *args and **kwargs, positional-only markers, keyword-only markers, and unpacking with * or **. These tools are not meant to be clever decorations. They exist so a function's interface can say how it wants to be called. A small function may need only ordinary positional arguments. A public library function may need a stricter or more flexible interface.

Lambdas and annotations

A lambda expression creates a small unnamed function from a single expression. It is most useful when another function expects a function as an argument, as with sorted(..., key=...). If the logic needs a name, multiple statements, or explanation, use def instead.

Function annotations, often called type hints, document expected parameter and return types. Python does not enforce these hints at runtime by default, but editors, type checkers, and readers use them to understand intent. In a large scientific or clinical codebase, that communication value is substantial.

In [29]:
# A function can package a repeated procedure under one name.

def fib(n):
    """Print a Fibonacci series less than n."""
    a, b = 0, 1
    while a < n:
        print(a, end=' ')
        a, b = b, a + b
    print()

fib(2000)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 
In [30]:
# Returning a value lets the program reuse the result.

def fib2(n):
    """Return a list containing the Fibonacci series up to n."""
    result = []
    a, b = 0, 1
    while a < n:
        result.append(a)
        a, b = b, a + b
    return result

f100 = fib2(100)
print(f100)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
In [31]:
# Defaults let callers omit ordinary values.

def ask_ok(prompt, retries=4, reminder='Please try again!'):
    for _ in range(retries):
        reply = 'y'
        if reply in {'y', 'ye', 'yes'}:
            return True
        if reply in {'n', 'no', 'nop', 'nope'}:
            return False
        print(reminder)
    raise ValueError('invalid user response')

print(ask_ok('Continue?'))
print(ask_ok('Continue?', 2))
True
True

Mutable default arguments

A mutable default argument is one of Python's most important beginner traps. The default object is created once when the function is defined, not freshly each time the function is called. If that object is a list, later calls can inherit values appended by earlier calls.

The safe pattern is to use None as the default and create the list inside the function when needed. That makes each call independent unless the caller deliberately passes in an existing list.

In [32]:
# Broken: the same default list is reused across calls.

def f_broken(value, items=[]):
    items.append(value)
    return items

print(f_broken(1))
print(f_broken(2))

# Correct: create a fresh list when the caller did not provide one.
def f_correct(value, items=None):
    if items is None:
        items = []
    items.append(value)
    return items

print(f_correct(1))
print(f_correct(2))
[1]
[1, 2]
[1, 2, 3]
[1]
[2]
In [33]:
# Keyword arguments make the call site explicit.

def describe_patient(age, diagnosis='unspecified', stage='unknown'):
    return f'age={age}, diagnosis={diagnosis}, stage={stage}'

print(describe_patient(64))
print(describe_patient(64, diagnosis='lymphoma', stage='II'))
-- This parrot wouldn't voom if you put 1000 volts through it.
-- Lovely plumage, the Norwegian Blue
-- It's a stiff!

-- This parrot wouldn't VOOOOOM if you put 1000000 volts through it.
-- Lovely plumage, the Norwegian Blue
-- It's a stiff!

-- This parrot wouldn't voom if you put a thousand volts through it.
-- Lovely plumage, the Norwegian Blue
-- It's pushing up the daisies!

In [34]:
# *args collects extra positional arguments.
# **kwargs collects extra keyword arguments.

def summarize_request(kind, *details, **metadata):
    print('kind:', kind)
    print('details:', details)
    print('metadata:', metadata)

summarize_request(
    'consult',
    'new patient',
    'urgent review',
    service='medical oncology',
    priority='high',
)
-- Do you have any Limburger?
It's very runny, sir.
It's really very VERY runny.
----------------------------------------
shopkeeper: Michael Palin
client: John Cleese
sketch: Cheese Shop Sketch
In [35]:
# / marks positional-only parameters. * marks keyword-only parameters.

def f(pos_only, /, normal, *, kw_only):
    print(pos_only, normal, kw_only)

f(1, 2, kw_only=3)
f(1, normal=2, kw_only=3)
1 2 3
1 2 3
In [36]:
# * and ** can unpack existing containers when calling a function.

bounds = [3, 6]
print(list(range(*bounds)))

patient = {'age': 64, 'diagnosis': 'lymphoma', 'stage': 'II'}
print(describe_patient(**patient))
[3, 4, 5]
-- This parrot wouldn't VOOM if you put 4000000 volts through it.
-- Lovely plumage, the Norwegian Blue
-- It's bleedin' demised!

Lambda expressions

A lambda expression defines a small anonymous function in one expression. It is useful when a function is needed briefly as an argument, especially for a sorting key or a simple transformation. It should not be used to hide complex logic. If the operation deserves explanation, it deserves a named function.

In [37]:
# sorted() can use a small function to choose the sort key.

pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
pairs.sort(key=lambda pair: pair[1])
print(pairs)
[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]
In [38]:
# Type hints document expected inputs and outputs for readers and tools.

def combine_words(first: str, second: str = 'eggs') -> str:
    return first + ' and ' + second

print('Annotations:', combine_words.__annotations__)
print(combine_words('spam'))
Annotations: {'ham': <class 'str'>, 'eggs': <class 'str'>, 'return': <class 'str'>}
spam and eggs

Section 6: Data Structures as Memory Shapes

Numbers, strings, lists, control flow, and functions are enough to write small programs. The next question is how a program should remember related information. Python's core data structures answer that question by giving memory different shapes. A list remembers an ordered, changeable sequence. A tuple remembers a fixed record. A set remembers unique membership. A dictionary remembers relationships between keys and values.

The choice of structure is not cosmetic. It tells later readers what kind of meaning the data carries and what operations should be cheap or natural. A patient's chronological labs belong in a list because order matters and new values arrive over time. A coordinate or immutable configuration pair may be a tuple. A set is ideal when the question is membership or deduplication. A dictionary is the ordinary Python shape for named facts, configuration, metadata, and JSON-like records.

The official Python tutorial collects these ideas in Data Structures. This section keeps the main story in prose and uses small examples to make each memory shape concrete.

6.1 Lists, files, stacks, and queues

Lists remain the most flexible everyday container. Their methods mutate the list in place when the point is to change the collection itself. That is why append(), reverse(), sort(), and pop() are operations on the existing object rather than expressions that always return a new object.

The same sequence idea appears in file I/O. A text file can be read as one string, as a list of lines, or line by line. The with statement is the professional habit because it closes the file even if an error occurs. The example below uses a temporary file so the chapter demonstrates the pattern without leaving project debris. Python's tutorial covers this under Reading and Writing Files, and Path comes from the standard-library pathlib module.

A list can also act as a stack: append() pushes onto the end and pop() removes from the same end. A queue is different because the first item in should be the first item out. Removing from the front of a large list is inefficient because the remaining elements must shift. For true queue behavior, Python provides collections.deque, documented at deque objects.

In [39]:
fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple']

print('apple count:', fruits.count('apple'))
print('first banana index:', fruits.index('banana'))

fruits.append('grape')
fruits.sort()
last = fruits.pop()

print('sorted fruits:', fruits)
print('removed last item:', last)
count('apple'): 2
index('banana'): 3
index('banana', 4): 6
After reverse: ['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange']
After append: ['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange', 'grape']
After sort:   ['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi', 'orange', 'pear']
Popped: pear → Remaining: ['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi', 'orange']

File input and output

File operations are not separate from data structures; they are how data enters and leaves a program. A file is often read into strings, lists of strings, dictionaries, or records that later become tabular data. The goal is not to memorize every method immediately. The goal is to recognize the lifetime of data: create it, write it, read it back, transform it, and clean up.

In [40]:
from pathlib import Path
import tempfile

tmp = Path(tempfile.gettempdir()) / 'ch01_demo.txt'

with open(tmp, 'w', encoding='utf-8') as file:
    file.write('Line 1: Python file I/O\n')
    file.write('Line 2: use with for automatic cleanup\n')

with open(tmp, encoding='utf-8') as file:
    all_text = file.read()
print('characters:', len(all_text))

with open(tmp, encoding='utf-8') as file:
    for line_number, line in enumerate(file, 1):
        print(line_number, line.rstrip())

tmp.unlink()
Height: 175 cm, Weight: 72 kg → BMI: 23.5
read(): 'Line 1: Python file I/O\nLine 2: always u'...
readlines(): 3 lines
  line 1: Line 1: Python file I/O
  line 2: Line 2: always use 'with' (context manager) — it auto-closes.
  line 3: Line 3: the manuscript originally used .close() manually.

Current dir: /mnt/c/DevShared/virtual-cancer-center/publishing/vol1_foundations/docs/chapter_drafts
Temp file:   /tmp/ch01_demo.txt
Exists?      True
Size:        146 bytes
Deleted:     True
In [41]:
stack = [3, 4, 5]
stack.append(6)
stack.append(7)

print('stack:', stack)
print('pop:', stack.pop())
print('after pop:', stack)
Stack: [3, 4, 5, 6, 7]
Pop: 7
Stack: [3, 4, 5, 6]
In [42]:
from collections import deque

queue = deque(['Eric', 'John', 'Michael'])
queue.append('Terry')
queue.append('Graham')

print('first out:', queue.popleft())
print('next out:', queue.popleft())
print('remaining:', queue)
First out: Eric
Next out:  John
Queue:     deque(['Michael', 'Terry', 'Graham'])

6.2 Comprehensions: compact construction of collections

A comprehension is a compact way to build a collection from an iterable. Read it from left to right as a sentence: produce this expression for each item in that source, optionally only when a condition is true. The power is not that it saves lines. The power is that it puts the construction rule in one visible place.

Use comprehensions when the transformation is simple enough to read at sight. Use an ordinary loop when the operation has several steps, side effects, exception handling, or explanation-worthy decisions. Readability remains the governing principle.

In [43]:
squares = [x**2 for x in range(10)]
evens = [x for x in range(20) if x % 2 == 0]
clean = [fruit.strip() for fruit in ['  banana', '  pear ', 'kiwi  ']]

print('squares:', squares)
print('evens:', evens)
print('clean:', clean)
Squares: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Evens:   [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Cleaned: ['banana', 'loganberry', 'passion fruit']
In [44]:
matrix = [
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12],
]

transposed = [[row[i] for row in matrix] for i in range(4)]
via_zip = list(map(list, zip(*matrix)))

print('transposed:', transposed)
print('via zip:', via_zip)
Transposed: [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
Via zip:    [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]

6.3 Tuples: fixed records and unpacking

A tuple is an immutable sequence. It is often used when the position of each element has meaning and the record should not be resized in ordinary use. Coordinates, RGB color triples, database rows, and small return values are common examples. Parentheses help readability, but commas are what create the tuple.

Tuple unpacking is the reverse operation: Python assigns each element of the tuple to a separate name. This is one reason tuples appear naturally in loops, function returns, and dictionary iteration.

In [45]:
record = (12345, 54321, 'hello')
patient_id, visit_id, label = record

print('record:', record)
print('patient:', patient_id, 'visit:', visit_id, 'label:', label)

nested = ([1, 2, 3], [4, 5, 6])
nested[0].append(99)
print('tuple holding mutable lists:', nested)
Tuple: (12345, 54321, 'hello!')
t[0]:  12345
Unpacked: x=12345, y=54321, z=hello!
Mixed: ([1, 2, 3, 99], [4, 5, 6])

Tuple methods and Boolean arithmetic

Because tuples are immutable, they have only a small method surface: count() and index(). The nearby Boolean example is included because it explains a Python idiom used often with collections: conditions evaluate to True or False, and those values can be summed as 1 and 0 when counting how many items satisfy a predicate.

In [46]:
values = (1, 4, 2, 4, 4, 3)
print('count 4:', values.count(4))
print('index 2:', values.index(2))

numbers = [3, -1, 7, -4, 2, -8, 5]
n_positive = sum(number > 0 for number in numbers)
print('positive numbers:', n_positive, 'of', len(numbers))
count(4): 3
index(2): 2
x=3.5, y=7.2, label=label
swapped: a=20, b=10

True + True  = 2
True + False = 1
False + False= 0
True == 1    ? True
True is 1    ? False

Positive numbers: 4 out of 7
<>:28: SyntaxWarning: "is" with 'int' literal. Did you mean "=="?
<>:28: SyntaxWarning: "is" with 'int' literal. Did you mean "=="?
/tmp/ipykernel_608/1366527472.py:28: SyntaxWarning: "is" with 'int' literal. Did you mean "=="?
  print("True is 1    ?", True is 1)       # → False (same value, different object!)

6.4 Sets: uniqueness and membership

A set is an unordered collection of unique elements. If lists are for ordered sequences, sets are for membership questions: have we seen this value before, is this code in the allowed group, which terms appear in both collections, and what remains after exclusions?

The empty set is created with set(). Empty braces create an empty dictionary, not an empty set. That small syntactic fact prevents many confusing beginner bugs.

In [47]:
basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print('unique fruit:', basket)
print('orange present:', 'orange' in basket)

letters_a = set('abracadabra')
letters_b = set('alacazam')

print('a - b:', letters_a - letters_b)
print('a | b:', letters_a | letters_b)
print('a & b:', letters_a & letters_b)
print('a ^ b:', letters_a ^ letters_b)
Set: {'orange', 'banana', 'apple', 'pear'}
'orange' in basket: True
a:     {'c', 'a', 'b', 'r', 'd'}
a - b: {'r', 'b', 'd'}
a | b: {'c', 'l', 'a', 'b', 'm', 'r', 'z', 'd'}
a & b: {'c', 'a'}
a ^ b: {'r', 'z', 'm', 'l', 'b', 'd'}
Even set: {0, 2, 4, 6, 8}

Set operations in practice

Union, intersection, difference, and symmetric difference are the mathematical language of comparison. In practical programming, they are tools for reconciling lists, checking overlap, removing duplicates, and finding what changed between two collections.

In [48]:
raw = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
seen = set()
unique_in_order = []

for value in raw:
    if value not in seen:
        seen.add(value)
        unique_in_order.append(value)

print('deduplicated:', unique_in_order)
set1: {2, 3, 4, 12}
set2: {True, 2, 3.8, 'a'}

Union:               {True, 2, 3, 4, 3.8, 'a', 12}
Intersection:        {2}
set1 - set2:         {3, 4, 12}
Symmetric diff:      {True, 3, 3.8, 4, 'a', 12}

{2,3} ⊆ set1?        True
set1 ⊇ {2,3}?        True

After add/discard:   {72, 11, -2}

Deduplicated list:   [3, 1, 4, 5, 9, 2, 6]

6.5 Dictionaries: named relationships

A dictionary maps keys to values. This is one of the most important structures in Python because it resembles the shape of real metadata: name to value, setting to configuration, patient field to observation, JSON property to content. Keys must be unique and hashable, while values can be any object.

Dictionary order is insertion order in modern Python. That makes dictionaries convenient for readable records, but their conceptual purpose is still mapping: use a dictionary when lookup by name is the central operation.

In [49]:
phone = {'jack': 4098, 'sape': 4139}
phone['guido'] = 4127
del phone['sape']

print('phone:', phone)
print('jack:', phone['jack'])
print('keys:', list(phone))
print('has guido:', 'guido' in phone)
Dict: {'jack': 4098, 'sape': 4139, 'guido': 4127}
tel['jack']: 4098
After del: {'jack': 4098, 'guido': 4127}
Keys: ['jack', 'guido']
Sorted: ['guido', 'jack']
'guido' in tel: True

Dictionary methods and construction patterns

The most common dictionary methods express ordinary record work: safe lookup with get(), merging with update() or |, creating templates with fromkeys(), counting items by incrementing values, and building a dictionary from pairs or a comprehension.

In [50]:
config = {'host': 'localhost', 'port': 8080}
print('timeout:', config.get('timeout', 30))

base = {'a': 1, 'b': 2}
base.update({'b': 99, 'c': 3})
print('updated:', base)

word_counts = {}
for word in 'the cat sat on the mat'.split():
    word_counts[word] = word_counts.get(word, 0) + 1
print('word counts:', word_counts)
host:    localhost
timeout: 30

fromkeys: {'A': 0, 'B': 0, 'C': 0, 'D': 0}
updated:  {'a': 1, 'b': 99, 'c': 3}

word counts: {'the': 2, 'cat': 1, 'sat': 1, 'on': 1, 'mat': 1}

I bought a Apple iPhone X
Updated: {'brand': 'Apple', 'product': 'iPhone', 'model': '16 Pro', 'color': 'Space Black'}

squares: {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
In [51]:
from_keywords = dict(sape=4139, guido=4127, jack=4098)
from_pairs = dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
squares = {number: number**2 for number in (2, 4, 6)}

print('from keywords:', from_keywords)
print('from pairs:', from_pairs)
print('squares:', squares)
From kwargs: {'sape': 4139, 'guido': 4127, 'jack': 4098}
From tuples: {'sape': 4139, 'guido': 4127, 'jack': 4098}
Comprehension: {2: 4, 4: 16, 6: 36}

6.6 Looping techniques: reading structures naturally

Python provides loop helpers that make the structure of data visible at the call site. Use dict.items() when both keys and values matter. Use enumerate() when the position matters. Use zip() when two sequences should be read together. Use reversed() or sorted() when the iteration order needs to change without obscuring the original data.

In [52]:
knights = {'gallahad': 'the pure', 'robin': 'the brave'}
for name, description in knights.items():
    print(name, description)

for index, value in enumerate(['tic', 'tac', 'toe']):
    print(index, value)

questions = ['name', 'quest', 'favorite color']
answers = ['lancelot', 'the holy grail', 'blue']
for question, answer in zip(questions, answers):
    print(f'What is your {question}? It is {answer}.')

print('odds descending:', list(reversed(range(1, 10, 2))))
gallahad the pure
robin the brave
0 tic
1 tac
2 toe
What is your name?  It is lancelot.
What is your quest?  It is the holy grail.
What is your favorite color?  It is blue.
9 7 5 3 1 

Section 7: Modules, Packages, and Reusable Namespaces

A function gives a block of logic a name. A module gives a file of related definitions a name. This is the next step in moving from a notebook demonstration toward reusable software: useful functions, constants, and classes can live in a .py file, and another program can import them instead of copying their source.

An import is also a namespace decision. import math keeps names grouped under math, so the call site says math.sqrt(2). from math import sqrt brings one name directly into the current namespace. import math as m creates a local alias. These forms are not interchangeable decoration; they control how visible, explicit, and collision-prone the names are.

Packages extend the same idea across directories. A package groups modules under dotted names so a large project can stay navigable. Later, the Virtual Cancer Center codebase depends on this habit: authentication, retrieval, safety, agent coordination, and public serving each live behind importable module boundaries.

The official tutorial explains these mechanics in Modules. The broader Python Standard Library is the catalog of batteries included with Python, including modules such as math and os.

Import styles and standard-library modules

For beginning code, prefer explicit imports that keep the reader oriented. Avoid from module import * in ordinary programs because it hides where names came from and makes collisions easier. The goal is not merely to make code shorter. The goal is to make dependencies visible.

The __name__ guard

Python sets a special variable named __name__ when it runs a file. If the file is executed directly, __name__ is '__main__'. If the file is imported, __name__ is the module's import name. This is why scripts often place command-line behavior under if __name__ == '__main__':. It lets the file provide reusable definitions when imported while still doing useful work when run directly.

In [53]:
import math
from math import log, e
import os

print('pi:', math.pi)
print('sqrt(2):', math.sqrt(2))
print('ln(e):', log(e))
print('current directory:', os.getcwd())
π ≈ 3.141592653589793
√2 ≈ 1.4142135623730951
e ≈ 2.718281828459045
ln(e) = 1.0
Current dir: /mnt/c/DevShared/virtual-cancer-center/publishing/vol1_foundations/docs/chapter_drafts
In [54]:
if __name__ == '__main__':
    print('This file is running as the main program.')
else:
    print('This file was imported as a module.')

print('__name__ =', __name__)
This code runs because we're executing directly (not importing).
__name__ = '__main__'

Section 8: Errors, Exceptions, and Trustworthy Failure

Every serious programmer learns that failure is information. Python errors are not insults from the interpreter; they are structured reports about where the program and reality disagreed. A syntax error means Python could not even parse the statement. An exception means the code was valid Python, but execution reached a state the operation could not handle.

A traceback is the story of that failure. The last line names the exception type and message. The lines above it show the path of function calls that led there. Reading tracebacks calmly is therefore a clinical skill for software: start with the final diagnosis, then walk upward through the history until the cause is visible.

The official Python tutorial covers this in Errors and Exceptions, including try, except, else, finally, raise, and exception chaining. The goal here is to understand the logic before memorizing the syntax.

8.1 Handling expected failure

An exception handler should be specific enough to say what failure was expected. Catching every possible exception hides defects. Catching ZeroDivisionError around a division operation or ValueError around a conversion tells the reader what uncertainty the code is prepared to manage.

In [ ]:
examples = [
    ('10 * (1 / 0)', 'division by zero'),
    ("'2' + 2", 'incompatible types'),
    ("int('hello')", 'invalid integer text'),
]

for expression, meaning in examples:
    try:
        eval(expression)
    except Exception as error:
        print(expression, '->', type(error).__name__, '-', meaning)
In [ ]:
def divide(numerator, denominator):
    try:
        result = numerator / denominator
    except ZeroDivisionError:
        return 'division by zero is undefined'
    else:
        return f'result = {result}'
    finally:
        print('division attempt complete')

print(divide(8, 2))
print(divide(8, 0))

8.2 Raising domain-specific exceptions

A program should sometimes refuse to continue. The raise statement deliberately creates an exception when a rule has been violated. Custom exception classes let a program name failures in the language of its domain: an invalid dose, a missing feature column, a failed validation gate, or an unauthorized workflow step.

In [ ]:
class ValidationError(Exception):
    pass

def require_positive(value, label):
    if value <= 0:
        raise ValidationError(f'{label} must be positive')
    return value

try:
    require_positive(-3, 'dose')
except ValidationError as error:
    print('validation failed:', error)
In [ ]:
from pathlib import Path
import tempfile

path = Path(tempfile.gettempdir()) / 'ch01_context_manager.txt'
with open(path, 'w', encoding='utf-8') as handle:
    handle.write('context managers close resources safely\n')

with open(path, encoding='utf-8') as handle:
    print(handle.read().strip())

path.unlink()

Section 9: Classes, Objects, Iterators, and Generators

Functions package actions. Classes package state and behavior together. A class defines the shape of a kind of object: what data it carries, what operations it can perform, and how it should present itself to the rest of the program. This is useful when a concept has identity over time, not merely a single calculation.

Object-oriented programming is not required for every task. A small data-cleaning script may be clearer with functions and dictionaries. But when a system has durable entities, such as patients, documents, model evaluations, agent runs, or deployment gates, classes can make the code's vocabulary match the problem domain.

The official tutorial introduces these ideas in Classes, including scope, instance attributes, inheritance, iterators, and generators.

9.1 Instance state and methods

The first parameter of an instance method is conventionally named self. It is the specific object receiving the method call. When consult.add_note('...') runs, Python passes the consult object into the method as self.

In [ ]:
class Consult:
    def __init__(self, patient_id, question):
        self.patient_id = patient_id
        self.question = question
        self.notes = []

    def add_note(self, note):
        self.notes.append(note)

    def summary(self):
        return f'{self.patient_id}: {self.question} ({len(self.notes)} notes)'

consult = Consult('P001', 'Evaluate anemia')
consult.add_note('CBC reviewed')
print(consult.summary())

9.2 Inheritance and dataclasses

Inheritance lets one class specialize another. It is best used when the child class truly is a more specific version of the parent. Overuse creates brittle hierarchies; careful use removes duplication and makes polymorphism possible. Polymorphism means different object types can answer the same method call in their own way.

For data-holding classes, Python's dataclasses module removes boilerplate by generating common methods such as __init__ and __repr__ from annotated fields.

In [ ]:
class Task:
    def __init__(self, name):
        self.name = name

    def run(self):
        return f'running {self.name}'

class ValidationTask(Task):
    def run(self):
        return f'validating {self.name}'

for task in [Task('import'), ValidationTask('schema')]:
    print(task.run())
In [ ]:
from dataclasses import dataclass

@dataclass
class Measurement:
    name: str
    value: float
    unit: str

hemoglobin = Measurement('hemoglobin', 10.2, 'g/dL')
print(hemoglobin)

9.3 Iterators and generators

Iteration is the protocol behind for loops. An iterable can produce an iterator; an iterator produces values one at a time until it is exhausted. This is why Python can loop over lists, strings, dictionaries, files, generator expressions, and many custom objects using the same syntax.

A generator is a function that uses yield to produce a sequence lazily. It saves its local state between values. Laziness matters when the data stream is large, expensive, or conceptually unbounded. The itertools module extends this style with efficient iterator building blocks.

In [ ]:
iterator = iter('abc')
print(next(iterator))
print(next(iterator))
print(next(iterator))

def reverse_text(text):
    for index in range(len(text) - 1, -1, -1):
        yield text[index]

print(''.join(reverse_text('clinical')))
In [ ]:
from itertools import islice

def fibonacci_stream():
    a, b = 0, 1
    while True:
        yield a
        a, b = b, a + b

print(list(islice(fibonacci_stream(), 12)))
print(sum(number * number for number in range(10)))

Professional Style: PEP 8 and Readable Code

Style is not mere decoration. In a serious project, style is part of how programmers protect one another from confusion. Consistent indentation, names, spacing, import order, and comments reduce the amount of mental translation required before the real logic can be evaluated.

The standard reference is PEP 8. A beginner does not need to memorize every rule on first reading, but should adopt the spirit immediately: write code that another careful person can read without guessing. Comments should explain why a decision was made, not narrate what the next token already says.

This completes the core Python language arc. The remaining sections bridge those core ideas into the habits of data science and AI/ML: debugging, text patterns, arrays, tables, and a first model pipeline.


Section 10: Debugging as a Method of Thought

Debugging is not random trying. It is the disciplined act of comparing what the program actually did with what the author believed it would do. The traceback, the current variable values, the input data, and the smallest reproducible example are evidence. Good debugging narrows uncertainty.

The common beginner mistake is to change several things at once. The professional habit is to isolate one hypothesis, make the smallest test that could disprove it, and then move one step closer to the root cause. This is the same habit used later for model failures, retrieval failures, deployment failures, and clinical safety failures.

10.1 Common error types

A NameError usually means a name was misspelled or never defined. A TypeError means the operation and the object type do not fit. An IndexError or KeyError means a lookup expected something that was not present. A ValueError means the object type was acceptable but the content was not. These distinctions are clues, not trivia.

In [ ]:
records = {'hemoglobin': 10.2, 'platelets': 180}

for key in ['hemoglobin', 'creatinine']:
    value = records.get(key)
    if value is None:
        print(key, 'is missing')
    else:
        print(key, '=', value)

age = 25
print(f'I am {age} years old')
In [ ]:
import math
from decimal import Decimal

print(0.1 + 0.2)
print((0.1 + 0.2) == 0.3)
print(math.isclose(0.1 + 0.2, 0.3))
print(Decimal('0.1') + Decimal('0.2'))

Section 11: Regular Expressions and Patterned Text

Much of real-world data arrives as text. Regular expressions, usually called regex, are a compact language for finding patterns in that text. They are powerful enough to extract phone numbers, dates, identifiers, simple labels, and formatting irregularities. They are also easy to overuse. If the structure is rich and nested, a parser is usually safer than a heroic regex.

Python's re module provides the standard regex engine. The central habit is to write a pattern, test it on representative examples, and keep the pattern readable enough that a later maintainer can understand what it was meant to capture.

In [ ]:
import re

text = 'Call 555-123-4567 or 555-987-6543.'
phone_pattern = re.compile(r'\d{3}-\d{3}-\d{4}')
print(phone_pattern.findall(text))

for match in phone_pattern.finditer(text):
    print(match.group(), 'starts at', match.start())
In [ ]:
raw = '  Patient:   anemia;   status: active   '
clean = re.sub(r'\s+', ' ', raw).strip()
print(clean)

email_text = 'Contact alice@example.com or bob.smith@university.edu'
email_pattern = re.compile(r'[A-Za-z0-9_.+-]+@[A-Za-z0-9-]+\.[A-Za-z0-9-.]+')
print(email_pattern.findall(email_text))

Section 12: NumPy and Numerical Arrays

Python lists are flexible, but scientific computing needs a structure that is compact, typed, multidimensional, and fast. NumPy supplies that structure through the ndarray. It is the foundation under much of the scientific Python ecosystem because it lets numerical operations run in optimized compiled code while the author writes clear Python expressions.

The important conceptual shift is vectorization. Instead of writing a Python loop that visits one element at a time, the author asks NumPy to apply an operation to an entire array. Broadcasting extends this idea by letting arrays with compatible shapes interact without manual repetition.

Use NumPy's absolute beginner guide and array object reference for deeper instruction.

In [ ]:
import numpy as np

values = np.array([1, 2, 3, 4, 5])
matrix = np.arange(12).reshape(3, 4)

print('values:', values)
print('dtype:', values.dtype, 'shape:', values.shape)
print('matrix:')
print(matrix)
In [ ]:
values = np.array([10, 20, 30, 40, 50])

print('add:', values + 100)
print('square:', values ** 2)
print('mean:', values.mean())

matrix = np.arange(9).reshape(3, 3)
print('column sums:', matrix.sum(axis=0))
print('row sums:', matrix.sum(axis=1))
print('greater than 4:', matrix[matrix > 4])

Section 13: Pandas and Labeled Tables

If NumPy gives Python fast numerical arrays, Pandas gives Python labeled tables. A Series is a labeled one-dimensional array. A DataFrame is a two-dimensional table with named columns and an index. This is the shape most clinicians, scientists, and analysts already recognize from spreadsheets, registries, and SQL query results.

The key advantage is semantic labeling. A column named Score carries more meaning than column number 2. Operations such as filtering, grouping, merging, and pivoting preserve that labeled structure while transforming the table.

For a full introduction, use Pandas 10 minutes to pandas and the DataFrame reference.

In [ ]:
import pandas as pd

data = {
    'Name': ['Alice', 'Bob', 'Carol', 'Dave', 'Eve'],
    'Age': [28, 34, 29, 42, 31],
    'Score': [88.5, 72.0, 95.2, 88.5, 91.0],
    'Subject': ['Math', 'Physics', 'Math', 'Physics', 'Math'],
}

df = pd.DataFrame(data)
print(df)
print(df.dtypes)
print(df.shape)
In [ ]:
print(df[df['Score'] > 85])
print(df.groupby('Subject')['Score'].mean())

df['Rank'] = df['Score'].rank(ascending=False, method='dense')
print(df[['Name', 'Score', 'Rank']])
In [ ]:
patients = pd.DataFrame({
    'Group': ['A', 'A', 'B', 'B', 'A', 'B'],
    'Outcome': ['Good', 'Fair', 'Good', 'Fair', 'Good', 'Good'],
    'Score': [91, 75, 84, 79, 95, 88],
})

print(pd.crosstab(patients['Group'], patients['Outcome']))
print(pd.pivot_table(patients, values='Score', index='Group', aggfunc='mean'))

Section 14: A First Machine Learning Pipeline

The chapter now has enough machinery to preview a complete AI/ML workflow. A machine learning pipeline is an ordered set of operations that turns raw features into a fitted model and an evaluation. The important principle is separation: learn transformations from the training data, apply them consistently to held-out data, and judge performance on examples the model did not see during fitting.

Scikit-learn formalizes this idea with Pipeline. The example uses the built-in breast cancer Wisconsin dataset, not because this chapter is teaching oncology modeling yet, but because it gives a realistic tabular classification problem with no external download.

In [ ]:
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import MinMaxScaler
from sklearn.svm import SVC
from sklearn.metrics import classification_report
import numpy as np

cancer = load_breast_cancer()
X, y = cancer.data, cancer.target

print('samples:', X.shape[0])
print('features:', X.shape[1])
print('classes:', dict(zip(cancer.target_names, np.bincount(y))))

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42, stratify=y
)
In [ ]:
pipeline = Pipeline([
    ('scale', MinMaxScaler()),
    ('model', SVC(kernel='rbf')),
])

pipeline.fit(X_train, y_train)
train_score = pipeline.score(X_train, y_train)
test_score = pipeline.score(X_test, y_test)

print(f'training accuracy: {train_score:.3f}')
print(f'test accuracy: {test_score:.3f}')
print(classification_report(
    y_test,
    pipeline.predict(X_test),
    target_names=cancer.target_names,
))

14.1 What the pipeline accomplished

The pipeline did four things in a disciplined order. It loaded a feature matrix and label vector. It split the data into training and test partitions. It scaled numeric features using parameters learned only from the training data. It trained a classifier and evaluated the fitted pipeline on held-out data.

That order prevents data leakage. Leakage occurs when information from the test set influences training, even indirectly. A model can then appear better than it really is. This is one of the first examples of a principle that will recur throughout the book: methodology is part of truth. A correct result is not only a number. It is a number produced by a defensible process.


Section 15: Chapter Summary and Forward Link

This chapter began with the working environment because code is never abstractly floating in space. It runs in an editor, a notebook kernel, a project folder, an interpreter, and a dependency environment. Once that execution context was visible, the chapter built Python from first principles: numbers, strings, lists, control flow, functions, data structures, modules, exceptions, classes, iterators, generators, style, debugging, regular expressions, arrays, tables, and a first model pipeline.

The sequence matters. Numbers teach computation. Strings teach symbolic representation. Lists teach mutable ordered collections. Control flow teaches decision and repetition. Functions teach named reusable thought. Data structures teach memory shape. Modules teach project-scale reuse. Exceptions teach trustworthy failure. Classes teach objects with identity. NumPy and Pandas teach scientific data representation. Scikit-learn shows how these pieces become a reproducible modeling workflow.

The full reference shelf remains in Appendix A, and the official Python documentation remains the authoritative instruction manual. The next chapter can now assume that the reader has enough Python vocabulary to study descriptive statistics as executable reasoning rather than as disconnected formulas.

References