From a problem to an executable program
Programming foundations and reliable decisions

Programming as a precise agreement
A program is a sequence of instructions that transforms inputs into outputs while following explicit rules. Before writing syntax, describe the problem in ordinary language. What information arrives? What result is required? Which conditions make the input unacceptable? A small shop calculating the cost of notebooks needs quantity and unit price, multiplies them, and presents a labeled total. It does not need a database, a website, or artificial intelligence to perform that calculation.
Python source code is text. The Python interpreter reads that text and executes its instructions. A text editor helps you write the file; a terminal lets you start the interpreter. These are different tools with different jobs. An interactive interpreter is useful for brief experiments, while a saved script is easier to repeat and share. If a command such as python --version is unavailable, try your installation's documented launcher, commonly python3 on some systems. Use a supported Python 3 release rather than an obsolete Python 2 installation.
Your first worked program
Create a file named notebook_cost.py. Type the following exactly, save it, and run it with your Python launcher from the folder containing the file.
quantity = 6
unit_price_tzs = 2500
total_tzs = quantity * unit_price_tzs
print("Notebooks:", quantity)
print("Total TZS:", total_tzs)The first two lines associate names with integer values. The third line evaluates the expression on its right, then associates the result with total_tzs. The last lines call the built-in print function. You should see a quantity of six and a total of 15000. The names describe the meaning of the values, and the currency suffix prevents a later reader from confusing shillings with dollars or item counts.
Execution normally proceeds from top to bottom. Moving the calculation before the assignments causes an error because the names do not yet refer to values. Changing quantity after the calculation does not retroactively change the total: an assignment stores a result at a particular moment. To calculate a new total, execute the expression again. This simple idea explains many apparently mysterious results in larger programs.
Observe state instead of guessing
Trace the program using a small table on paper. After line one, quantity is six. After line two, the unit price is 2500. After line three, the total is 15000. A trace records state after each relevant instruction. It is more dependable than guessing what the program intended. Comments begin with # and explain decisions for humans; they do not execute. Prefer a comment explaining why a price excludes delivery to one merely repeating that multiplication happens.
A syntax error means Python could not parse the instructions. A runtime error occurs after execution begins. A logical error produces an output that is allowed by the language but wrong for your problem. For example, addition instead of multiplication may execute successfully while reporting an incorrect total. Successful execution therefore does not establish correctness.
Practice and self-check
- Change the program to calculate eight pens costing TZS 1200 each.
- Add a delivery charge of TZS 3000 using a separately named variable.
- Print the item subtotal and the final amount on separate labeled lines.
- Explain what happens if the quantity changes after the subtotal assignment.
The expected subtotal is TZS 9600 and the final amount is TZS 12600. The stored subtotal remains unchanged until it is recalculated. Check your result by doing the arithmetic independently; comparing a program only with your expectation can preserve the same mistake twice.
Common mistake and extension
A frequent mistake is editing a file without saving it, then running an older version. Confirm the file path and save before each run. Another is including a currency label inside an arithmetic value, such as "2500 TZS"; that is text, not an integer. Keep values separate from presentation. As an extension, write a three-sentence specification for a transport-cost calculator, identifying its inputs, computation, and output before writing any code.
- Specify
Define inputs, rules, and expected outputs.
- Write
Save Python instructions in a source file.
- Execute
The interpreter evaluates instructions and updates state.
- Check
Compare the output with an independently calculated result.
Course notes are provided for reading in your learning workspace.
Open your B-tech learning workspace to read this topic.
Ready for the full course?
Unlock the remaining topics, practical projects, quizzes and saved progress.
TZS 50,000one-time course access
View course ↗