A bike rental shop charges 15 dollars plus 4 dollars per hour, so the cost is . To make a price list for 1 to 12 hours you would substitute into that expression 12 times. A computer can do it in an instant, but only if you tell it exactly what to do in a language it understands. Python is one of those languages, and many of its ideas come straight from algebra: a Python variable is a name for a value, an expression is worked out with the same order of operations you already know, and a formula becomes a single line of code. A program is a list of instructions that Python carries out from the top down, one line at a time. The simplest instruction is print, which shows a result on the screen. This unit uses Python 3; every program here runs in IDLE (the editor that comes with Python) or in an online Python editor.
print("Hello")
print(3 + 4)
print("3 + 4")
print("Total:", 3 + 4)Each print starts a new line of output. Text inside quotation marks is a string: Python shows it exactly as written and does not calculate anything inside it, which is why print("3 + 4") shows 3 + 4. Without quotation marks, Python evaluates the expression first and prints the answer, 7. When you separate several items with commas, print shows them on one line with a single space between them, so print("Total:", 3 + 4) shows Total: 7.
Variables: named boxes for values
h = 3
cost = 15 + 4 * h
print(cost)
h = 5
print(h, cost)Think of a variable as a labelled box. The line h = 3 is an assignment: it means "put 3 in the box named h". Python always works out the right side of = first, using the values the boxes hold at that moment, and then stores the result in the variable on the left. So line 2 calculates and stores 27 in cost. Line 4 replaces the 3 in h with 5, but cost keeps 27: it was calculated once, back when h was 3, and nothing tells Python to calculate it again. That is a real difference from algebra, where is a relationship that stays true for every value of . A variable name can use letters, digits and underscores (like bike_cost), cannot start with a digit, contain spaces or be one of Python's own keywords (such as for or if), and is case-sensitive, so Cost and cost are two different boxes.
x = 5 is an instruction (store 5 in x), not a statement that two things are equal. To ask whether x equals 5, write x == 5; Python answers True or False, so print(x == 5) prints True when x holds 5. This is why x = x + 1 makes sense in code even though no number equals itself plus 1: it means "take the value in x, add 1, and store the result back in x". The left side of a single = must be a variable name, not a calculation, so 15 + 4 * h = cost stops the program with a SyntaxError.n = 2
n = n + 5
n = n - 1
print(n)Arithmetic and the order of operations
| Operator | Meaning | Example | Output |
|---|---|---|---|
+ | addition | 7 + 2 | 9 |
- | subtraction | 7 - 2 | 5 |
* | multiplication | 7 * 2 | 14 |
/ | division (the answer is a decimal, even when it divides evenly) | 7 / 2 and 6 / 2 | 3.5 and 3.0 |
// | division, rounded down to an integer | 7 // 2 | 3 |
% | remainder after whole-number division | 7 % 2 | 1 |
** | power (exponent) | 7 ** 2 | 49 |
print(2 + 3 * 4)
print((2 + 3) * 4)
print(-3 ** 2)
print((-3) ** 2)
print(7 / 2)
print(6 / 2)Python follows the same order of operations as math class (BEDMAS): brackets first, then powers, then multiplication and the three kinds of division from left to right, then addition and subtraction from left to right. So the first line is , while the brackets in the second line force . The power is done before the negative sign, exactly like in math; to square negative 3 you need brackets: . Two habits from algebra do not carry over. Python has no implied multiplication, so must be typed with a * between the 4 and the h, and needs a * before the bracket too. And a fraction bar groups its whole numerator, so must be typed (a + b) / 2 with brackets.
print(10 - 2 ** 3) print?Parameters: change one value and rerun
In the line , the letters and are variables: they change as you move along the line. The letters and are parameters: they stay fixed for one particular line, but changing them gives a different line. Programs use the same idea. Put each parameter on its own line at the top of the program; then to model a new situation you edit one line and run the program again, instead of rewriting the formula.
m = 3
b = 2
x = 5
y = m * x + b
print("When x =", x, "y =", y)fee = 15
rate = 4
h = 3
cost = fee + rate * h
print(cost)
If only the second line is changed to rate = 6, what does the program print?