Programming in Python / Quantities and text
Quantities and text
All 5 lessons, in order.
The unit at a glance
- Choose an arithmetic operator
A volunteer is measuring ribbon for four display frames.
- Group a calculation
Four supply kits sit on a table.
- Keep a remainder
You have a stack of 38 postcards and display sleeves that hold six cards each.
- Index and slice a label
A shelf label reads
RACK27. - Format a line of output
A volunteer writes shelf lengths on small bin cards.
Rules (5)
- Choose an arithmetic operator
Use
+to add an amount,-to take an amount away,*to find the total of equal groups and/to share an amount equally. In4 * 6, the operands are4and6, the numbers that*works on.Start with whole numbers such as
4and20. 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.5and5.0floats. In these examples,/gives a float even when the share is a whole number of meters. - Group a calculation
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.
- Keep a remainder
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. - Index and slice a label
With
labelas the name of the string,label[1]picks the character at index 1. Inlabel[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. - Format a line of output
Put
fright 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:.2fafter the number’s name inside its braces. For the lengths on these cards,.2fwrites 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.
Recipes (5)
- Choose an arithmetic operator
- 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.
- Group a calculation
- 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.
- Write
- Keep a remainder
- 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.
- Index and slice a label
- 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.
- Format a line of output
- 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.
- Give each value a name. Put the shelf name in quotes and write the length as a number. On each line,
Traps (5)
- Choose an arithmetic operator
If you reach for the letter
xwhen you mean “times,” type the asterisk*. That is Python’s multiplication sign.Both numbers in
20 / 4are whole numbers. The length per frame is exactly 5 m, and Python displays that result as5.0. The.0is part of how Python writes this float. - Group a calculation
Reading
6 + 3 * 4strictly from left to right would give 36. Python does3 * 4first, 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. - Keep a remainder
If you catch yourself reading
%as “percent,” read38 % 6aloud 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. - Index and slice a label
If you start numbering at 1, you’ll pick
Rfor index 1. Label that first box 0 and the next box 1 to findA. If you include box 5 in the slice, you’ll getCK27. Place your pencil on the line before box 5 and leave the7on the far side. - Format a line of output
If you see braces in the printed line, check for the
fbefore 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.lengthstill holds3.5after the program prints3.50.
Lesson 1 of 5
Choose an arithmetic operator
Open this lessonLeans on 1Comes back in 0
Leans on
Comes back in
No lesson links recorded.
1 In plain words
A volunteer is measuring ribbon for four display frames. Each roll holds 6 meters. Here, m stands for meters. The volunteer lays out four rolls, adds a spare piece, cuts off some ribbon and shares what is left equally. Each frame needs the same length.
Read 4 * 6 as “four times six.” The * is an operator, a symbol that tells Python what calculation to do. Remember to name the action first. Look up its symbol in The rule below.
2 Picture it
Four 6 m rolls give 24 m. The spare 5 m brings it to 29 m. Cutting 9 m leaves 20 m. Dividing that among four frames gives 5 m per frame.
3 Why
Four rolls of the same length give you four groups of 6 m. Multiplication finds their total in one calculation. Adding the spare makes the total longer. Taking away the cut piece is subtraction, and it tells you how much ribbon is left.
Dividing the ribbon into equal shares gives you the length for one frame. Put all four shares end to end to check that they add up to the length you divided.
4 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.
5 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.
6 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.
Trace
| Action | Calculation | Output | Explanation |
|---|---|---|---|
| Count equal rolls | 4 * 6 | 24 | Four groups of 6 m make 24 m. |
| Add the spare | 24 + 5 | 29 | The extra piece adds 5 m. |
| Cut a piece | 29 - 9 | 20 | The cut removes 9 m. |
| Share equally | 20 / 4 | 5.0 | Four pieces of 5 m recover the 20 m you divided. |
7 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.
Quick checks
Quick check 1 of 2
You have 18 m of ribbon and cut off 7 m. Which calculation gives the length left, and what is the result?
Show the steps and the answer
Use 18 - 7. There are 11 m left.
Why: Cutting takes part of the ribbon away, so use -.
Source: Python Software Foundation, Python 3.13 Tutorial, 3.1.1. Numbers
Quick check 2 of 2
Share 15 m of ribbon equally across six signs. What calculation would you put inside print in a new editor file? What would the program display?
Show the steps and the answer
Type print(15 / 6). The program uses no keyboard input and prints 2.5 on one line.
Why: Use / to share the total equally. Six lengths of 2.5 m give 15 m, the full length you started with.
Source: Python Software Foundation, Python 3.13 Tutorial, 3.1.1. Numbers
Sources
- Python 3.13 Tutorial, 3.1.1. Numbers. Python Software Foundation. Python Software Foundation License Version 2. Read 26 Nov 2026.
Lesson 2 of 5
Group a calculation
Open this lessonLeans on 0Comes back in 0
Leans on
No lesson links recorded.
Comes back in
No lesson links recorded.
1 In plain words
Four supply kits sit on a table. Each kit needs six clips and three rings. How many pieces do you need for all four kits?
When you calculate with numbers in Python, + means add and * means multiply. The curved marks ( and ) are called parentheses. Read (6 + 3) * 4 as “six clips plus three rings for one kit, four times.” Remember to put parentheses around the counts that go together.
2 Picture it
Four kit trays each contain six filled marks for clips and three open marks for rings. That is nine marks per kit and 36 marks in all.
3 Why
You need the same set of pieces for each kit. Add six clips and three rings to get nine pieces for one kit. Multiplying that nine by four counts one set of pieces for each of the four kits.
4 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.
5 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.
6 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.
Trace
| Expression | First value | Output | Explanation |
|---|---|---|---|
(6 + 3) * 4 | 6 + 3 = 9 | 36 | Make a kit, then count four kits. Separately, 24 clips plus 12 rings also give 36. |
6 + 3 * 4 | 3 * 4 = 12 | 18 | Without the parentheses, multiply the rings first and count the clips once. |
7 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.
Quick checks
Quick check 1 of 2
In 6 + 3 * 4, how many clips and how many rings does Python count?
Show the steps and the answer
6 clips and 12 rings, for 18 pieces.
Why: Python multiplies 3 * 4 first, then adds the six clips once.
Source: Python Software Foundation, An Informal Introduction to Python, 3.1.1. Numbers
Quick check 2 of 2
Three party bags each get seven cards and two stickers. Write a calculation with 3 first and parentheses around the two counts for one bag. How many pieces go in all three bags?
Show the steps and the answer
3 * (7 + 2) gives 27 pieces.
Why: Each bag gets nine pieces. Counting separately also gives 21 cards and 6 stickers, which add up to 27.
Source: Python Software Foundation, An Informal Introduction to Python, 3.1.1. Numbers
Sources
- An Informal Introduction to Python, 3.1.1. Numbers. Python Software Foundation. Python Software Foundation License Version 2. Read 26 Nov 2026.
Lesson 3 of 5
Keep a remainder
Open this lessonLeans on 0Comes back in 0
Leans on
No lesson links recorded.
Comes back in
No lesson links recorded.
1 In plain words
You have a stack of 38 postcards and display sleeves that hold six cards each. Fill as many sleeves as you can. The number of full sleeves is your group count. The number of loose cards is your remainder.
In Python 3.13, read 38 // 6 as “How many full sleeves?” and 38 % 6 as “How many cards stay loose?” Remember // for full groups and % for leftovers. To look them up, find floor division and remainder in the Python tutorial’s Numbers section.
2 Picture it
Six sleeves each contain six postcard marks. Two more marks sit outside the sleeves. The 36 packed cards and two loose cards account for all 38.
3 Why
Six full sleeves use 36 cards. Adding the two loose cards gives the whole stack again. On paper, write 6 * 6 + 2 = 38. Read * as “times,” + as “plus” and = as “equals.”
Two is smaller than six. If six or more cards were loose, you could fill another sleeve.
4 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.
5 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.
6 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.
Trace
| Instruction | Value or output | Explanation |
|---|---|---|
cards = 38 | 38 | Give the name cards the postcard count. |
cards_per_sleeve = 6 | 6 | Save the capacity of one sleeve. |
cards // cards_per_sleeve | 6 | Six full sleeves use 36 cards. A seventh would require 42. |
cards % cards_per_sleeve | 2 | Two cards remain. Six groups of six and two loose cards account for all 38 cards. |
7 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.
Quick checks
Quick check 1 of 2
You have 26 gift tags. Each pocket holds 8. What do 26 // 8 and 26 % 8 give? Label each answer.
Show the steps and the answer
26 // 8 gives 3 full pockets. 26 % 8 gives 2 loose tags.
Why: Three pockets use 24 tags. The 2 left cannot fill a pocket of 8.
Source: Python Software Foundation, Python 3.13 Tutorial, Numbers
Quick check 2 of 2
A greenhouse has 319 plant tags. Each tray fits 29 tags. Predict the outputs of 319 // 29 and 319 % 29. What do they tell you?
Show the steps and the answer
319 // 29 gives 11 full trays and 319 % 29 gives 0 loose tags.
Why: Eleven trays of 29 use all 319 tags.
Source: Python Software Foundation, Python 3.13 Tutorial, Numbers
Sources
- Python 3.13 Tutorial, Numbers. Python Software Foundation. Python Software Foundation License Version 2. Read 26 Nov 2026.
Lesson 4 of 5
Index and slice a label
Open this lessonLeans on 0Comes back in 0
Leans on
No lesson links recorded.
Comes back in
No lesson links recorded.
1 In plain words
A shelf label reads RACK27. To copy it onto a stock card, write from left to right and fit each letter or digit into its own box. Use the box numbers to read back one letter or digit, or several in a row.
Each letter or digit on the label is a character. Python calls a piece of text a string. Write "RACK27" with double quotes to tell Python 3.13 that it is text.
The marks [ and ] are square brackets. They go after the name for the text and hold the numbers that tell Python what to pick. The : mark is a colon. Between two numbers in the brackets, read it as “start at the first number and stop before the second.”
An index is a position number. Remember that Python starts at 0 on the left. Look up where to put the numbers and the colon in The rule.
2 Picture it
Six tiles read R, A, C, K, 2, 7. Their indexes are 0 through 5. The shaded slice covers C, K and 2. A stop line sits before the 7, at index 5.
3 Why
One number inside square brackets picks the character in that box as a string with one character. Two numbers separated by a colon pick a slice, a piece of the string. For the slices here, start at the box named by the first number. If the second number names a box, stop immediately before it. If it equals the character count, stop at the right edge of the last box. The slice is text, even if it contains only digits.
4 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.
5 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.
6 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.
Trace
| Instruction | Output | Explanation |
|---|---|---|
label = "RACK27" | — | Give the whole tag the name label. |
print(label[1]) | A | Index 1 is the second character when the first is at 0. |
print(label[2:5]) | CK2 | Take positions 2, 3 and 4. Subtract 2 from 5 to check the three-character length. |
7 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.
Quick checks
Quick check 1 of 2
With label holding "RACK27", what text does label[0:2] pick?
Show the steps and the answer
"RA"
Why: Read R at position 0 and A at position 1. Stop before box 2.
Source: Python Software Foundation, Python 3.13 Tutorial, 3.1.2. Text
Quick check 2 of 2
When label holds "MUG84", what do print(label[0]) and print(label[3:5]) display? Which positions does the slice use?
Show the steps and the answer
The first line is M. The second is 84. The slice uses positions 3 and 4.
Why: Give the label MUG84 five boxes. The first print line reads M from box 0. The second reads 8 and 4 from boxes 3 and 4, then stops at the right edge. A slice of a string gives a string.
Source: Python Software Foundation, Python 3.13 Tutorial, 3.1.2. Text
Sources
- Python 3.13 Tutorial, 3.1.2. Text. Python Software Foundation. Python Software Foundation License Version 2. Read 26 Nov 2026.
Lesson 5 of 5
Format a line of output
Open this lessonLeans on 0Comes back in 0
Leans on
No lesson links recorded.
Comes back in
No lesson links recorded.
1 In plain words
A volunteer writes shelf lengths on small bin cards. Every card follows the same pattern. Write the shelf name, a colon and a space, the length with two digits after the decimal point, then a space and m. On these cards, m stands for meters. Read Oak: 3.50 m as “Oak, three point five zero meters.”
The text a program displays is one kind of output. To format output is to choose how it looks. In Python 3.13, put f or F immediately before the opening quote to start an f-string. That first letter is the prefix. This lesson uses f. Think of the f-string as a template, a pattern with places to fill in. Remember the prefix and the braces {}. For these lengths, look up .2f when you need two digits after the decimal point.
2 Picture it
In the f-string, the name field connects to Oak, the text colon and space stays where it is, and the length field with .2f connects to 3.50. A space and m finish the printed line Oak: 3.50 m.
3 Why
name and length are names for stored values. Python uses the text held by name to fill {name}. In {length:.2f}, Python takes the number held by length and uses .2f to write it. The colon inside those braces starts the format instruction. The colon after {name} is part of the card text, along with the space after it.
4 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.
5 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.
6 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.
Trace
| Part of template | Output | Explanation |
|---|---|---|
{name} | Oak | Use the text stored in name. |
| Colon and space | : | Copy both literal characters. |
{length:.2f} | 3.50 | Write the number with two decimal places. |
| Space and m | m | Copy these two literal characters. Then print ends the line. |
7 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.
Quick checks
Quick check 1 of 2
Keep the Oak values. In the editor, replace the last line with print("{name}: {length:.2f} m"). What line appears in the shell when you run the file?
Show the steps and the answer
{name}: {length:.2f} m
Why: The opening quote has no f before it, so the text inside the quotes appears as typed. The line ends with a newline.
Source: Python Software Foundation, Python 3.13 Tutorial, Formatted String Literals
Quick check 2 of 2
Use the supplied values name = "Elm" and length = 2.3125 in the editor. Use the original print line with its f prefix. What line appears in the shell when you run the file?
Show the steps and the answer
Elm: 2.31 m
Why: The third digit after the decimal point is 2, so rounding to two places keeps 1 as the second digit, in the hundredths place. The line ends with a newline.
Source: Python Software Foundation, Python 3.13 Tutorial, Formatted String Literals
Sources
- Python 3.13 Tutorial, Formatted String Literals. Python Software Foundation. Python Software Foundation License Version 2. Read 26 Nov 2026.