Quantities and text: crash course
about 5 min to listen5 lessons0 questions to tryUnit 2 of 6
Short view. Each lesson's rule, its steps and its trap. Switch to Full for a worked example and a question to try in every lesson.Full view. Each lesson's rule, its steps, one worked example with Watch it solved, the trap, and one question to try.
Starts here no skill from another unit needed first
1 Choose an arithmetic operator
The rule
Use + to add an amount, - to take an amount away, * to find the total of equal groups and / to share an amount equally.
In 4 * 6, the operands are 4 and 6, the numbers that * works on.
Start with whole numbers such as 4 and 20. To subtract, put the starting amount first and the amount you remove second. To divide, put the amount to share first and the number of shares second. The number to the right of / is the divisor. In this example, it counts the frames, so it must be at least one.
Python calls numbers such as 2.5 and 5.0 floats. In these examples, / gives a float even when the share is a whole number of meters.
How
- Say what is happening. Are you adding a piece, taking one away, finding the total of equal groups or sharing equally?
- Choose the operator for that action. Put it between the two numbers in the order described above.
- To show the result, type
printand put the calculation inside the parentheses(and). Type the source lines below in a file in your editor. Save it asribbon.pyand run it with Python 3.13. You can also follow along on paper with the printed trace, which shows the results in order.
Worked example
Ribbon for four display frames
print(4 * 6)
print(24 + 5)
print(29 - 9)
print(20 / 4)
Standard input
Empty
Standard output
24
29
20
5.0
Save the code as ribbon.py and run the file in Python 3.13. The first three lines show whole meter counts. The last line shows the share as a float. Four shares of 5 m give the 20 m you divided.
The trap
If you reach for the letter x when you mean “times,” type the asterisk *. That is Python’s multiplication sign.
Both numbers in 20 / 4 are whole numbers. The length per frame is exactly 5 m, and Python displays that result as 5.0. The .0 is part of how Python writes this float.
2 Group a calculation
The rule
When parentheses group an addition, do the addition inside them before multiplying by the number outside.
In a calculation with addition and multiplication and no grouping parentheses, Python does the multiplication first.
How
- Write
6 + 3to count the pieces for one kit. - Put parentheses around that addition to get
(6 + 3). - Put
* 4after the group to count four kits.
Worked example
Count the pieces in four kits
print((6 + 3) * 4)
print(6 + 3 * 4)
Standard input
Empty
Standard output
36
18
Type both lines into an editor and run the file with Python 3.13. The first line counts all parts of all four kits. The second counts the clips once and the rings four times.
The trap
Reading 6 + 3 * 4 strictly from left to right would give 36. Python does 3 * 4 first, which counts 12 rings, then adds six clips once, for 18 pieces. To count six clips in every kit, group the two counts as (6 + 3) * 4.
3 Keep a remainder
The rule
The total is how many items you start with.
It can be any whole number from zero up. The group size is how many items fit in one full group. It can be any whole number from one up. Here, whole numbers are the counts 0, 1, 2 and so on.
Put the total on the left and the group size on the right. With // between them, Python gives the number of full groups. With % between them, it gives the remainder. The remainder is smaller than the group size. When it is zero, the full groups use all the items.
How
- Write down the total and the group size.
- Use
//between those numbers to find the number of full groups. - Use
%between the same numbers to find the remainder. - Multiply the group count by the group size and add the remainder. Check that you get the total and that the remainder is smaller than one full group.
- When you type the program,
cards = 38gives the namecardsthe value 38. The linecards_per_sleeve = 6gives the group size its own name, with underscores between the words.
Worked example
Pack 38 postcards
cards = 38
cards_per_sleeve = 6
print(cards // cards_per_sleeve)
print(cards % cards_per_sleeve)
Standard input
Empty
Standard output
6
2
Save the code as postcards.py and run the file. Count full sleeves on the first output line and loose cards on the second. As a second check, subtract 6 repeatedly from 38 to reach 32, 26, 20, 14, 8 and 2.
The trap
If you catch yourself reading % as “percent,” read 38 % 6 aloud as “How many cards are left?” Its answer counts two loose cards. If you swap the two results, you get two full sleeves and six loose cards. Six loose cards would fill one more sleeve, so the swap fails the remainder check.
4 Index and slice a label
The rule
With label as the name of the string, label[1] picks the character at index 1.
In label[2:5], 2 is the start and 5 is the stop. That takes positions 2, 3, and 4. When the start is smaller than the stop and both numbers are in the range from zero through the total number of characters, subtract the start from the stop to get the slice’s character count.
How
- Number the boxes from left to right, starting at 0.
- Give the text the name
labelon the first line. The equals sign=tells Python to use the name on the left for the text on the right. - After
label, put the position in square brackets for one character. For a slice, put the start and stop in the brackets with a colon between them. - To display the result, put your selection inside the parentheses of
print(). The textprint()displays is the output. For these labels,print()displays the text without the surrounding quotes. - In a text editor, type the three nonblank lines under Code in the worked example. Keep the blank line and start each line at the left edge. Run the file with Python 3.13. If you use the Python shell, where you can run one line at a time, type each nonblank source line after the
>>>shown on screen and press Enter.
Worked example
Read the shelf tag
label = "RACK27"
print(label[1])
print(label[2:5])
Standard input
Empty
Standard output
A
CK2
Type the program in an editor and keep the blank line. Run the file. To check the slice on paper, cover RA before the start and 7 after the stop. The uncovered characters read CK2.
The trap
If you start numbering at 1, you’ll pick R for index 1. Label that first box 0 and the next box 1 to find A. If you include box 5 in the slice, you’ll get CK27. Place your pencil on the line before box 5 and leave the 7 on the far side.
5 Format a line of output
The rule
Put f right before the opening quote.
Inside the quotes, put the names whose values you want to use inside braces. Type the literal pieces where they should appear. Write :.2f after the number’s name inside its braces. For the lengths on these cards, .2f writes out two places after the decimal point, adding a zero or rounding the displayed number as needed.
In these examples, print(...) displays the finished text and then adds a newline, which ends the line.
How
- Give each value a name. Put the shelf name in quotes and write the length as a number. On each line,
=gives the name on the left the value on the right. - Inside
print(...), start the template withf". Write{name}where the shelf name goes. Type a colon and one space after it. - Add
{length:.2f}, then one space andm. Type the closing quote and the closing parenthesis). - Predict the printed line. Read the name, the colon and space, the number with two digits after the decimal point, then the space and unit.
Worked example
Fill the Oak shelf card
name = "Oak"
length = 3.5
print(f"{name}: {length:.2f} m")
Standard input
Empty
Standard output
Oak: 3.50 m
Type the program into an editor and run the file. In the output, count two digits after the decimal point. Check the space after the colon and the space before m. The number assigned to length remains 3.5.
The trap
If you see braces in the printed line, check for the f before the opening quote. In this example, leaving it out makes Python print the text inside the quotes as written, braces and all. With the prefix in place, the format instruction controls the number’s appearance on the card. length still holds 3.5 after the program prints 3.50.