Recommended resources
The freely available resources this lesson will direct you to.
Good to see you again. In the previous lesson, you used units to check whether a quantitative formula made sense. Now we begin using Python to evaluate such formulas reliably. Python will not understand that a number represents USD, tonnes, or months, so the disciplined habits you have just built—clear definitions, compatible units, and explicit parentheses—remain essential.
In this lesson, you will store numerical quantities in Python variables and combine them with arithmetic operators. By the end, you will be able to translate a small market calculation into readable Python code, run it, and interpret the result. Plan for about 30 minutes.
From an algebraic formula to Python code
Suppose a commodity has world price and an ad valorem tariff rate . The consumer price is
In Python, we can evaluate this by assigning values to named variables:
world_price = 80.0
tariff_rate = 0.15
consumer_price = world_price * (1 + tariff_rate)
print(consumer_price)
The output is:
92.0
The calculation is the same as
but the Python version has an important advantage: world_price and tariff_rate make the meaning of each number visible. If you later change the price or the tariff, Python can recalculate the result without your rewriting the formula.
Keep the unit meaning in mind even though Python does not display it:
world_priceis in USD per unit;tariff_rateis a dimensionless proportion;consumer_priceis in USD per unit.
A useful coding habit is to make the unit clear in a nearby comment when it is not obvious from the name:
quantity_tonnes = 250
price_usd_per_tonne = 120.0

Assignment is not algebraic equality
The symbol = has a different job in Python from its job in algebra.
In algebra,
is impossible: no number is equal to itself plus one.
In Python, however, this is valid:
inventory = 100
inventory = inventory + 25
print(inventory)
The output is:
125
Python handles the second line in two stages:
- It reads the current value of
inventory, which is100, and evaluates the expression on the right:100 + 25. - It stores the resulting value,
125, under the nameinventory, replacing the old value.
So = means assign the evaluated value on the right to the name on the left. It is an instruction, not a claim that two mathematical objects are equal.
A Python assignment has one variable name on the left and an expression on the right:
variable_name = expression
For example:
fixed_cost = 12000
unit_cost = 18.50
quantity = 900
total_cost = fixed_cost + unit_cost * quantity
The right-hand side is evaluated first. Only then is the result stored in total_cost.
Choosing useful variable names
Use names that indicate what the number means:
import_price = 75.0
import_quantity = 400
transport_cost_per_unit = 4.50
Avoid vague names such as x, a, or thing when the calculation represents an economic quantity. Short names are acceptable in pure algebra, but descriptive names make a program easier to audit.
A few basic rules:
- Names cannot contain spaces; use underscores, as in
fixed_cost. - Python distinguishes capital and lower-case letters:
Priceandpriceare different variables. - A variable must receive a value before you use it.
- Do not use commas inside numeric literals: write
12000, not12,000.
If you try to use a variable that has not yet been assigned, Python raises a NameError. This is usually a spelling mistake or an indication that code was run in the wrong order.
Arithmetic operators: Python’s calculator notation
Python’s basic arithmetic symbols are close to ordinary mathematical notation, with two important differences: multiplication uses *, and powers use **.
| Python operator | Meaning | Example | Result |
|---|---|---|---|
+ |
addition | 8 + 3 |
11 |
- |
subtraction | 8 - 3 |
5 |
* |
multiplication | 8 * 3 |
24 |
/ |
division | 8 / 3 |
2.6666666666666665 |
** |
exponentiation | 8 ** 3 |
512 |
// |
floor division | 8 // 3 |
2 |
% |
remainder after division | 8 % 3 |
2 |
The four basic operators are the ones you will use most frequently in quantitative models: +, -, *, and /.
The other three are also useful:
**calculates powers. For example, compound growth over periods often includes an expression such as , written in Python as(1 + growth_rate) ** periods.//gives the number of complete groups. For nonnegative values,250 // 24gives10.%gives what remains after forming complete groups. In the same example,250 % 24gives10.
For instance, if material is shipped in containers holding 24 tonnes:
material_tonnes = 250
container_capacity = 24
full_containers = material_tonnes // container_capacity
remaining_tonnes = material_tonnes % container_capacity
print(full_containers)
print(remaining_tonnes)
This produces:
10
10
That means 10 completely filled containers can be sent, with 10 tonnes left over.
Python Tutorial for Beginners 3: Integers and Floats - Working with Numeric Data
Watch “Python Tutorial for Beginners 3: Integers and Floats - Working with Numeric Data” by Corey Schafer for a compact demonstration of numerical types, arithmetic operators, and parentheses in a Python session.
Watch number types to distinguish whole-number integers from decimal floats. Then watch arithmetic operators for division, powers, floor division, and remainders. Finish with precedence, focusing on how parentheses make the intended calculation unambiguous.
Integers, floats, and division
Python represents whole numbers as integers and decimal numbers as floats:
containers = 10 # integer
price = 18.50 # float
For most introductory market calculations, Python will choose a sensible numeric result automatically. In particular, ordinary division using / produces a float:
print(3 / 2)
Output:
1.5
This is usually what you want when calculating averages, prices, ratios, growth rates, or elasticities.
Use // only when you deliberately want the number of complete groups, not a fractional answer. For example, if a firm produces 125 units per day and each pallet holds 40 units, 125 / 40 is 3.125, while 125 // 40 is 3: three full pallets.
Computers store many decimal fractions approximately. This can occasionally produce a result such as:
print(0.1 + 0.2)
0.30000000000000004
This is a normal limitation of binary floating-point representation, not a failure of arithmetic. At this stage, the practical response is to retain full precision while calculating and round only when presenting a result:
share = 1 / 3
print(round(share, 3))
0.333
Order of operations still matters
Python follows the familiar order of operations:
- Parentheses are evaluated first.
- Powers using
**are evaluated next. - Multiplication, division, floor division, and remainder operations follow, working from left to right.
- Addition and subtraction are evaluated last, working from left to right.
Consider a tariff calculation. These two expressions are not the same:
world_price = 80
tariff_rate = 0.15
wrong_price = world_price * 1 + tariff_rate
right_price = world_price * (1 + tariff_rate)
print(wrong_price)
print(right_price)
Output:
80.15
92.0
The first expression means
which incorrectly adds a dimensionless tariff rate directly to a price. The second means
which is both mathematically and dimensionally appropriate.
Use parentheses not only to alter precedence, but also to communicate your intended formula. In quantitative code, extra parentheses are usually a virtue.
Be especially explicit with negative values and powers:
print((-2) ** 2)
The parentheses make clear that the base is negative two, giving .
3. An Informal Introduction to Python
Read the Python documentation’s introduction to using Python as a calculator. It confirms the operator notation used here and introduces the distinction between integers and floats.
In Section 3.1, “Using Python as a Calculator,” and Subsection 3.1.1, “Numbers,” read the calculator introduction. Notice that Python uses parentheses for grouping and distinguishes integers from decimal-valued floats. Then continue into the following material beginning “Division” through the paragraph on variable assignment. Focus particularly on the facts that / returns a float, ** calculates powers, and = assigns a value to a variable.
A complete market calculation
Let us combine these ideas in a small profit model. Suppose a company imports and sells a recycled material. It sells tonnes at price , pays a fixed monthly cost , and incurs a unit cost per tonne.
The model is:
Here is a direct Python translation:
price_per_tonne = 160.0
quantity_tonnes = 900
fixed_cost = 12000.0
variable_cost_per_tonne = 135.0
revenue = price_per_tonne * quantity_tonnes
total_cost = fixed_cost + variable_cost_per_tonne * quantity_tonnes
profit = revenue - total_cost
print(revenue)
print(total_cost)
print(profit)
Output:
144000.0
133500.0
10500.0
The numerical result has an economic interpretation:
- revenue is USD per month, if the price and quantity refer to a month;
- total cost is USD per month;
- profit is USD per month.
Notice that the code mirrors the structure of the formulas rather than compressing everything into one long line. This is good practice. It lets you inspect revenue and total_cost separately before trusting the final profit value.
Variables retain values until you reassign them
A variable stores the result that was calculated at the time of assignment. It does not automatically track later changes in related variables.
world_price = 80.0
tariff_rate = 0.15
consumer_price = world_price * (1 + tariff_rate)
tariff_rate = 0.20
print(consumer_price)
The output remains:
92.0
Changing tariff_rate does not revise the already stored value of consumer_price. Recalculate it explicitly:
consumer_price = world_price * (1 + tariff_rate)
print(consumer_price)
Now the output is:
96.0
This behavior is fundamental to programming. A model is a sequence of instructions, so the order in which you run calculations matters.
A dependable routine for quantitative Python
When turning a formula into Python, use this sequence:
- Define each input clearly. Assign numerical values to named variables.
- Record units in your reasoning or comments. Python computes numbers but does not enforce dimensions.
- Translate multiplication and powers correctly. Use
*and**. - Use parentheses around conceptual groups. For example, write
price * (1 + tax_rate). - Store intermediate quantities. Calculate revenue and costs before profit.
- Print and inspect results. An implausible result often reveals a typo, missing parenthesis, or incompatible unit.
- Recalculate dependent quantities after changing an input. Python does not update earlier results automatically.
Key takeaways
Python variables let you give names to quantities and reuse them in calculations. The statement name = expression evaluates the expression on the right and stores its value under the name on the left.
- Use
+,-,*, and/for the basic arithmetic operations. - Use
**for powers,//for complete groups, and%for remainders. - Ordinary division produces a float, even when the answer is mathematically whole.
- Parentheses make the intended order of calculation explicit.
- Python does not know economic units, so retain the dimensional checks from the previous lesson.
- A stored result does not automatically update when an input variable changes.
Next, you will combine algebra and Python directly: checking an algebraic solution by substitution, first manually and then with code.