The Forge · Computer Science & Programming
Programming I: First Programs in Python
Variables, loops, and functions — enough Python to make the machine do your errands. · QA 76.73 · ~24 h
A program is an ordered list of statements the computer carries out exactly, one after another from top to bottom.
fol. 2 A Name Bound to a ValueA variable is a name bound to a value, and every value carries a type — int, float, str, or bool — that fixes what can be done with it.
fol. 3 Print Out, Input Inprint() sends text to the screen while input() reads one line back from the person, always handing it over as a string.
An expression combines values and operators into a single computed value, evaluated under fixed precedence rules.
fol. 5 True, False, and the Tests BetweenComparison operators and the connectives and, or, and not reduce values to True or False — the two answers every decision is built on.
fol. 6 Choosing Which Lines RunAn if / elif / else statement runs exactly one branch, chosen by the first boolean test that comes out True.
A while loop repeats its body as long as a condition stays true, so the body must eventually make that condition false.
fol. 8 Once for Each ItemA for loop binds a variable to each item a sequence like range() hands it, running the body exactly once per item.
fol. 9 Following the Counter by HandTracing a loop line by line in a table reveals how the counter grows and where an off-by-one boundary error slips in.
Defining a function stores a named block of steps, and calling that name runs those steps wherever you invoke it.
fol. 11 Values In, One Value OutParameters carry values into a function and the return statement hands one value back out to the caller.
fol. 12 Private Names, Separate PartsA function's local names are private to its own call, which lets you break a large problem into small, independently testable parts.
A list is an ordered, index-addressed, mutable collection of values you can read, grow, change, and loop across.
fol. 14 A Sequence of CharactersA string is an immutable indexed sequence of characters you can slice, search, and rebuild into new strings.
fol. 15 One Pass, One AccumulatorA single pass through a sequence with an accumulator variable can find the maximum, count matches, or search for a value.
fol. 16 Reading the TracebackDebugging is a repeatable method: read the traceback, locate the failing line, form a single hypothesis, and test it.