Quantities and text — quick review
Choose an arithmetic operator
A volunteer is measuring ribbon for four display frames.
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.
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.
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.
Group a calculation
Four supply kits sit on a table.
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.
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.
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.
Keep a remainder
You have a stack of 38 postcards and display sleeves that hold six cards each.
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.
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.
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.
Index and slice a label
A shelf label reads RACK27.
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.
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.
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.
Format a line of output
A volunteer writes shelf lengths on small bin cards.
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.
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.
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.