Recommended resources
The freely available resources this lesson will direct you to.
- Python for Beginners - Learn Coding with Python in 1 Hourwww.youtube.com
- Python Tutorial for Beginners 3: Integers and Floats - Working with Numeric Datawww.youtube.com
- Arithmetic operations - Python Programming MOOC 2024programming-24.mooc.fi
- Lecture 0 - CS50's Introduction to Programming with Pythoncs50.harvard.edu
Hello! Last time, you created a Python file in VS Code and checked that it ran with your project’s .venv interpreter. Today, you’ll use that setup to calculate results rather than print a fixed greeting. You’ll work with variables, the basic types of values Python stores, and operators for arithmetic and comparisons. These are the ingredients you’ll later use in your command-line unit converter.
Allow about 40–45 minutes, including time to run and edit the examples. Keep your python-first-project folder open in VS Code and create a new file there called calculations.py.
Values have types; variables give them names
A variable lets you keep a value for use later in the program. Watch the True or False.
Python for Beginners - Learn Coding with Python in 1 Hour
Programming with Mosh’s Python for Beginners — Learn Coding with Python in 1 Hour introduces assignment and four useful kinds of values.
Watch variables and types, from the explanation of storing information through the example of a Boolean value. Pay particular attention to quotation marks around text and capitalization in True and False.
Put this in calculations.py, save it with Command–S, and use Run Python File:
distance_m = 2.5
sample_count = 4
label = "Test rig"
is_calibrated = True
print(label)
print(distance_m)
print(sample_count)
print(is_calibrated)
The names on the left of = are variables. The values on the right are assigned to them. Here, distance_m holds a float (a decimal number), sample_count holds an int (a whole number), label holds a str (text, also called a string), and is_calibrated holds a bool (a Boolean value: True or False). Names such as distance_m help a reader understand both the quantity and its unit. Python distinguishes uppercase from lowercase, so True is valid but true is not.
Quotation marks make an important difference. print(label) displays the variable’s value, Test rig; print("label") displays the literal text label. You can inspect a value’s type with type(): for example, print(type(distance_m)) displays <class 'float'>.
Assignment is not a permanent declaration. If you add distance_m = 3.0 before the print statements, subsequent uses of distance_m get 3.0. Python runs these statements in order, so a calculation uses the value assigned most recently.
Arithmetic turns values into results
Watch
Python for Beginners - Learn Coding with Python in 1 Hour
Return to the video for the arithmetic needed to compute and update quantities.
Watch arithmetic and precedence, beginning with the operator examples and ending after the parentheses example. Notice the distinct roles of /, //, and %.
Python’s common arithmetic operators are:
| Expression | Meaning | Result |
|---|---|---|
7 + 2 |
Addition | 9 |
7 - 2 |
Subtraction | 5 |
7 * 2 |
Multiplication | 14 |
7 / 2 |
Division | 3.5 |
7 // 2 |
Floor division | 3 |
7 % 2 |
Remainder | 1 |
7 ** 2 |
Power | 49 |
Use * for multiplication, not x, and ** for powers. Ordinary division / produces a float, even for two whole-number inputs: 8 / 2 gives 4.0. Floor division // rounds the quotient down; it is not simply “remove the decimal part.” For instance, -7 // 2 gives -4. The remainder operator % tells you what is left after division.
Python follows arithmetic precedence. Multiplication is done before addition, so 2 + 3 * 4 gives 14, while (2 + 3) * 4 gives 20. Parentheses make the intended calculation clearer, even when you already know the precedence rule.
Replace the contents of calculations.py with a small unit calculation:
distance_m = 2.5
millimetres_per_metre = 1000
distance_mm = distance_m * millimetres_per_metre
print(distance_mm)
The result is 2500.0. Python stores that result in distance_mm; it does not change distance_m. The .0 appears because one operand, 2.5, is a float. This is the core of a converter: keep the source quantity and conversion factor distinct, then assign the computed result to a descriptively named variable.
An operator can also update an existing variable. sample_count = sample_count + 1 takes the current value, adds one, and assigns the result back to sample_count. The shorthand sample_count += 1 does the same thing. You’ll use updates more often when you reach repeated operations later this week.
Watch for types when calculating. "2" is text, not the number 2: "2" + "3" joins strings to produce "23", whereas 2 + 3 produces 5. A string and a number cannot simply be added together. In this lesson, your quantities are written directly as numbers; the next lesson will show how to turn text entered at the console into numbers safely.
Comparisons compute Boolean results
Operators can answer questions as well as calculate quantities. Watch = assigns a value, while == checks whether two values are equal.
Python for Beginners - Learn Coding with Python in 1 Hour
The video now connects comparisons to Boolean values and shows how to combine checks.
Watch comparisons from the first greater-than example through the list of comparison symbols. Continue with logical operators for and, or, and not. For now, focus on the True or False result; the next lesson will use it to choose what a program does.
Comparisons such as >, <, >=, <=, ==, and != produce a bool. For example, 2.5 <= 3.0 is True, while 2.5 == 3.0 is False. You can combine Boolean checks: and requires both to be true, or requires at least one, and not reverses a Boolean result.
Extend your calculation into a short report about a test rig’s travel:
distance_m = 2.5
time_s = 4.0
millimetres_per_metre = 1000
distance_mm = distance_m * millimetres_per_metre
average_speed_m_s = distance_m / time_s
within_range = distance_m >= 0 and distance_m <= 3.0
print("Travel:", distance_mm, "mm")
print("Average speed:", average_speed_m_s, "m/s")
print("Within 0 to 3 m range:", within_range)
You should see 2500.0 mm, 0.625 m/s, and True. Each computed variable answers a different question: how far in millimetres, how fast on average, and whether the travel falls within the stated range. Notice that within_range records a check but does not yet change the program’s behavior.
Change distance_m to 3.5, save, and rerun. The converted distance and speed change, and within_range becomes False. This is a useful way to verify that each result depends on the value you intended. Decimal float calculations can sometimes display tiny rounding artifacts because many decimal fractions cannot be represented exactly in binary; that does not change the formulas you’ve written here.
What you can now build on
You can assign named values, distinguish int, float, str, and bool, and use arithmetic or comparison operators to store a computed result. You also know why = and == mean different things, why "2" is not 2, and how parentheses clarify a formula.
Keep calculations.py in your project folder. Next, you’ll replace fixed values such as distance_m = 2.5 with console input and use conditional statements to choose a response based on checks like within_range.