Classroom Glossary Public page

Week 1: Python Syntax

1,446 words

The setup week and the first real Python you write. You install Python 3, open a REPL, write variables and strings and if statements, and ship a temperature converter as a command-line tool. By the end of the week your terminal is a place you reach for the way you reach for a hex editor.


Theme

Python is not magic. It is a small set of rules, applied consistently. Most of what makes Python pleasant to read (no semicolons, indentation as syntax, multiple ways to format strings) is convention rather than computer-science invention. You will spend the first three weeks of FND-102 learning the rules, and the remaining eleven weeks applying them to real work.

This week the rules are: variables, types, operators, strings, input and print. Nothing about functions, control flow, files, or modules yet. The discipline of week 1 is to type Python in a REPL, see what happens, type it again with a small change, see what is different. The lab at the end of the week turns those small experiments into a working CLI tool.

By the end of week 1 you can: open a Python REPL, write and run a .py file, distinguish the four primitive types you care about (int, float, str, bool), use string formatting (f-strings) confidently, and accept input from the user via input() and produce output via print(). The temperature-converter lab demonstrates all of this in ~50 lines.

Reading list (~1 hour)

  1. Matthes, Python Crash Course 2nd ed., Ch 1 ("Getting Started") and Ch 2 ("Variables and Simple Data Types"). The setup walk-through in Ch 1 mirrors the FND-102 SETUP.md. Read Ch 2 carefully; variables, strings, and numeric types are this week's lecture material in book form.
  2. Sweigart, Automate the Boring Stuff with Python 2nd ed., Ch 1 ("Python Basics") at https://automatetheboringstuff.com/2e/chapter1/. Free online. Sweigart's "Why automate?" motivation in the book's introduction is the framing FND-102 borrows for the entire course.
  3. Allen B. Downey, Think Python 2nd ed., Ch 1 ("The Way of the Program") and Ch 2 ("Variables, Expressions, and Statements") at https://greenteapress.com/thinkpython2/html/thinkpython2002.html. Free online. Downey's distinction between a "value" and a "variable" is the clearest in print; read his §2.3.

The reading is intentionally redundant: three sources cover the same beginner material from three angles. Read whichever you find clearest. If you have prior Python experience, skim and confirm your mental model is intact.

Lecture outline (~1.5 hours, 2 sessions of ~50 min)

Session 1: The REPL, values, and variables

Section 1.1: The REPL

  • Open a terminal. Type python3. You should see a >>> prompt.
  • The REPL (Read-Eval-Print Loop) is Python's interactive shell. Type an expression; Python evaluates it and prints the result.
  • 2 + 2 returns 4. 'hello' + ' world' returns 'hello world'. The REPL prints whatever the expression evaluates to.
  • print('hello') prints hello and returns None. Notice the difference between "evaluates to a value" and "performs an action."
  • Exit the REPL with Ctrl-D (macOS / Linux) or exit() (any platform). You will use it dozens of times this week.

Section 1.2: The four primitive types you care about

  • int: whole numbers. 42, -7, 0. Python ints have unlimited size; 2**1000 is a real number, not an overflow.
  • float: numbers with a decimal point. 3.14, -0.5, 1.0. Floats follow IEEE 754 with the usual gotchas: 0.1 + 0.2 is not exactly 0.3. Try it in the REPL.
  • str: text. 'hello' or "hello" (both are valid; pick one and be consistent within a file). Strings are immutable: s[0] = 'H' is an error.
  • bool: True or False. Note the capitalization. 1 == True is True; bools are a subset of ints. (Trivia: True + True is 2. Do not rely on this.)

Three more primitive types exist (bytes, complex, NoneType); FND-102 introduces them as needed and you can read about them in week 5.

Section 1.3: Variables

  • A variable is a name that refers to a value: name = 'jamie'.
  • Variable assignment is not the same as algebraic equality; it is binding a name to a value.
  • Variables can be rebound: x = 5; x = 6 is fine; the second line replaces the binding.
  • Naming conventions (PEP 8 style guide, at https://peps.python.org/pep-0008/#naming-conventions): lowercase with underscores for variables and functions (first_name, not firstName or FirstName).
  • Python is dynamically typed: the type lives with the value, not with the variable. x = 5; x = 'hello' is fine; x is now a string.

Section 1.4: The type() and id() functions

  • type(42) returns <class 'int'>. Use it in the REPL to inspect any value.
  • id(x) returns the memory address of the object x refers to. Useful for understanding "is this the same object?" later. Quick demo: a = [1, 2]; b = a; id(a) == id(b) is True.

Session 2: Operators, strings, and I/O

Section 2.1: Numeric operators

  • +, -, *, / work as expected.
  • ** is exponentiation: 2 ** 8 is 256.
  • // is integer division (floor): 7 // 2 is 3. % is modulo: 7 % 3 is 1.
  • / always returns a float: 4 / 2 is 2.0, not 2. Use // when you want an int.
  • Operator precedence is the math you learned in school (PEMDAS). Use parentheses when in doubt.

Section 2.2: String formatting

  • The modern Python way: f-strings. name = 'jamie'; greeting = f'hello, {name}'. The {name} is replaced with the value.
  • F-strings support expressions: f'{2 + 2}' is '4'. They support format specifiers: f'{3.14159:.2f}' is '3.14'.
  • Older formatting styles exist ('%s' % name, '{} {}'.format(a, b)). You will see them in older code; prefer f-strings in new code.
  • Multi-line strings: triple-quote: """this string\nspans multiple lines""". Useful for docstrings (week 3) and CLI help text.

Section 2.3: User input

  • input('Enter a number: ') prints the prompt, waits for the user to type, and returns the string the user entered.
  • The return value is ALWAYS a string. input() does not parse; if you want a number, convert: n = int(input('n: ')).
  • int('42') returns 42. int('not a number') raises ValueError. The error happens at conversion; the input call itself does not error.

Section 2.4: Comments and code structure

  • Lines starting with # are comments; the interpreter ignores them.
  • Comments answer "why" not "what." Code should be self-explanatory enough that "what does this do" is obvious from reading; "why is this here" belongs in a comment.
  • Convention: comments are full sentences ("Discard the trailing newline.") not telegrams ("trim trailing").

Browser tool: the Python tutor visualizer at https://pythontutor.com/. Paste a few lines of Python, click "Visualize Execution," step through. Useful for seeing what x = 5; x = 'hello' does to the variable binding. Use it AFTER you have run code in the REPL; the visualizer is for intuition, not for skipping the practice.

Labs (~90 minutes)

Lab 1: Temperature Converter (labs/lab-1-temperature-converter.md)

  • Goal: build a CLI tool that converts between Fahrenheit, Celsius, and Kelvin
  • Time: ~90 minutes
  • Artifact: lab-1-temp.py in your ~/fnd-102/lab-1/ directory, committed to Git

Independent practice (~4 hours)

  1. REPL drills (30 min). Open the REPL and type the following expressions. Predict the answer before pressing Enter; check against what Python prints.

    • 5 + 3
    • 5 / 2
    • 5 // 2
    • 5 % 2
    • '5' + '3'
    • '5' * 3
    • '-' * 40 (a horizontal rule)
    • type(5), type(5.0), type('5'), type(True)
    • int('5') + 2
    • str(5) + '!'
  2. Variable binding exercises (30 min). Type each block in the REPL. Predict the final value of every variable; check with print().

    a = 5
    b = a
    a = 10
    # what is b?
    
    x = [1, 2]
    y = x
    y.append(3)
    # what is x?
    

    (The second one is a forward-pointer to week 4; expect surprise.)

  3. F-string practice (45 min). Write a Python program that takes a name (via input()) and prints "Hello, {name}! Today you will write {n} lines of Python." where n is computed from the length of the name (e.g., len(name) * 10). Run it three times with different names.

  4. Read a real script (30 min). Find a small open-source Python tool on GitHub (the Real Python "Awesome Python" list is a good starting point; pick a tool tagged "CLI" with fewer than 200 stars). Open one of its .py files. Try to read the first 30 lines. Write down 3 things you do not yet recognize. You will recognize them by week 7.

  5. Optional stretch (90 min). Re-do Lab 1 with a different conversion: kilometers to miles, gallons to liters, or your own choice. Same shape (argparse later; for now just input()). The goal is to internalize the "ask for a number, convert it, print the result" loop.

Reflection prompts (~30 minutes)

Journal these at week's end. There are no wrong answers.

  1. The lecture said input() always returns a string. Before this week, would you have predicted that? Why or why not?
  2. F-strings replaced two older string-formatting styles. Try writing the same string with all three styles (f-string, .format(), %-formatting). Which feels most natural? Why do you think Python introduced f-strings?
  3. The REPL feedback loop is much shorter than the edit-save-run-read-error loop. When was the REPL useful to you this week? When was a .py file better?
  4. Your Lab 1 temperature converter probably has at least one bug-prone area (negative numbers, decimal input, non-numeric input). What did you do about it? What did you leave for week 9 (error handling)?
  5. One thing from this week you want to know more about?

Tool journal (week 1)

  • python3: the interpreter and the REPL
  • int(), float(), str(), bool(): type conversion functions
  • type(): inspect the type of a value
  • input(), print(): standard input and output
  • f-strings: the modern way to format strings (f'hello, {name}')
  • VS Code (or your chosen editor): write Python in a file and save it

What comes next

Week 2 introduces control flow: if, while, for. Your temperature converter from this week runs once and exits; next week's guess-the-number game runs in a loop and decides what to do based on conditions. The same primitive types are at play; the new ingredient is "the program makes decisions based on the input."