{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "365960c9",
   "metadata": {},
   "source": [
    "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/MeiraDavidson/compute_module1/blob/main/notebooks/lab_00_talking_to_the_machine.ipynb)\n\n",
    "# Lab 0: Talking to the Machine\n",
    "\n",
    "*From* **A First Taste of Algorithms — and the Machines That Learn** *(The Curious Family Library). A computational lab: run the cells in order. A few labs end with a deliberately broken “Break it” cell — that one is meant to fail, and seeing how is the point.*"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c95f6987",
   "metadata": {},
   "source": [
    "*Before Chapter 1. Ten minutes, maybe fifteen.*\n",
    "\n",
    "Every lab in this book asks you to run small programs and watch what they do. This one exists so that the first time is now, when nothing is at stake, rather than later, when something is.\n",
    "\n",
    "You need a place to run Python. Any of these works: the editor at python.org, an installed copy of Python, or a notebook environment. It does not matter which. The programs in this book use nothing but Python itself — no packages to install, nothing that will stop working in five years. If a program here fits on one screen, it is meant to be typed, not copied. Typing is slower. That is the point.\n",
    "\n",
    "## 1. The machine repeats what you say\n",
    "\n",
    "Type this and run it:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f7bba47",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"hello\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6238292",
   "metadata": {},
   "source": [
    "The machine prints `hello`. This is the whole transaction: you write an instruction, the machine carries it out, exactly, and nothing more. It does not know what you meant. It knows what you wrote. The entire craft of programming lives in that gap, and this book is partly about learning to feel the gap before the machine shows it to you.\n",
    "\n",
    "## 2. The machine does arithmetic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a740b3e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(3 + 4)\n",
    "print(3 * 4)\n",
    "print(2 ** 10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b28889f",
   "metadata": {},
   "source": [
    "The last line is 2 to the 10th power. Run it. Then change it to `2 ** 100` and run it again. That number has thirty-one digits. The machine did not slow down. Hold on to this: the machine is not smart, but it is *fast*, and almost everything in this book comes from asking what fast-but-not-smart is good for.\n",
    "\n",
    "## 3. The machine remembers what you name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e20fb711",
   "metadata": {},
   "outputs": [],
   "source": [
    "steps = 0\n",
    "steps = steps + 1\n",
    "steps = steps + 1\n",
    "print(steps)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3830d4be",
   "metadata": {},
   "source": [
    "A *variable* is a name attached to a value. The line `steps = steps + 1` looks wrong if you read `=` as it is read in mathematics — no number equals itself plus one. In Python, `=` is not a statement of fact. It is an instruction: *compute the thing on the right, then attach the name on the left to it.* Read it as \"steps becomes steps plus one\" and it is sensible.\n",
    "\n",
    "This little program is a counter. In Chapter 1 we will count something worth counting.\n",
    "\n",
    "## 4. The machine tells you when you've lost it\n",
    "\n",
    "Run this. It is broken on purpose."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b44b1ab3",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"the answer is \" + 42)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b85601c1",
   "metadata": {},
   "source": [
    "Python stops and prints several lines of complaint ending in something like:\n",
    "\n",
    "```\n",
    "TypeError: can only concatenate str (not \"int\") to str\n",
    "```\n",
    "\n",
    "Read the last line. It is not scolding you; it is reporting, precisely, what it could not do: glue a piece of text to a number, because those are different kinds of things. An error message is information. Every programmer who has ever lived reads dozens of them a day. The ones who get good are the ones who read them rather than flinch.\n",
    "\n",
    "Fix the program by making the number into text:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd5d11de",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"the answer is \" + str(42))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cafab7cc",
   "metadata": {},
   "source": [
    "## 5. The machine is fast\n",
    "\n",
    "One more, and this one needs a construct we will not explain until Chapter 1 — run it anyway:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4f9c17fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "total = 0\n",
    "for n in range(1, 1000001):\n",
    "    total = total + n\n",
    "print(total)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55c7c9c2",
   "metadata": {},
   "source": [
    "This adds every whole number from 1 to one million. Run it and notice two things. First, the answer appears before your finger leaves the key. A million additions cost the machine nothing you can perceive. Second — check the answer. Gauss, as a schoolboy, is said to have computed this sum (to one hundred, but the trick scales) in seconds, by *thinking* instead of adding: pair 1 with 1,000,000, pair 2 with 999,999, and so on; 500,000 pairs, each summing to 1,000,001.\n",
    "\n",
    "So there are two ways to be fast: do the dumb thing very quickly, or find a way to not do most of the work at all. The machine gives you the first for free. This book is about the second. An *algorithm* is, more than anything else, a way of not doing most of the work — and the strange, wonderful fact is that the not-doing can be written down, studied, and proved correct, like any other piece of mathematics.\n",
    "\n",
    "That is where Chapter 1 begins.\n",
    "\n",
    "## Break it\n",
    "\n",
    "End every lab this way: change one thing, *predict what will happen before you run it*, run it, and if the prediction was wrong, figure out why. The prediction is the exercise. Running the code only grades it.\n",
    "\n",
    "1. In §3, swap the last two lines, so `print(steps)` comes before the second `steps = steps + 1`. What prints?\n",
    "2. In §5, change `range(1, 1000001)` to `range(1000001)`. Predict whether the answer changes, then check. (Something here starts at 0. Does it matter?)\n",
    "3. In §5, change one million to one billion: `range(1, 1000000001)`. Predict first: instant, a few seconds, a minute, or never finishes? Then run it and time it. You have just taken your first measurement of the thing this whole book is about — what computation *costs*.\n",
    "\n",
    "\n",
    "\n",
    "<div style=\"page-break-after: always;\"></div>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}