Programming and Computation · Module 1 of 10
Python as a Powerful Calculator
The fastest way into programming is to use Python the way you use a calculator: type an expression, get a value. This module covers arithmetic, operator precedence, the power operator, math functions, and storing results in variables.
Readiness check
This is the first module. Tick only what you can do closed-notes.
- Recall order of operations (brackets, powers, then multiply and add).
- Recall the area of a circle and Newton's second law.
- Open a Python prompt or an online Python console.
- Recall that a variable stores a value for reuse.
- Believe that code is just careful arithmetic written down.
The core idea
Python evaluates an expression and returns a value, so it is a calculator you can program. It follows the usual order of operations, uses ** for powers, provides math functions through the math module, and stores results in named variables so you can reuse them.
You do not need to know "programming" to start computing. At the Python prompt, typing 2 + 3 * 4 returns 14, because Python obeys the same precedence you learned in school: powers first, then multiplication and division, then addition and subtraction, with brackets overriding all of it. Two things trip up engineers coming from calculators or spreadsheets. First, the power operator is **, not the caret: r ** 2 means r squared. Second, real functions like square root, sine, and the constant pi live in the math module, which you make available with import math and then call as math.sqrt(2) or math.pi. The last essential idea is the variable: r = 5 stores the value 5 under the name r, so a later line can compute math.pi * r ** 2 and store that under area. Building a calculation from named variables, instead of one long expression, is what turns arithmetic into readable, reusable, checkable engineering code, and it is the habit the whole course builds on.
** and math functions correctly.import math.The skills, taught in order
Five skills turn the interpreter into an engineering calculator.
1.1 The interpreter and precedence
Typing an expression at the prompt returns its value immediately. Python follows standard precedence: powers, then multiplication and division, then addition and subtraction. Use brackets to make intent explicit, for example (a + b) / 2.
1.2 The power operator
Use ** for exponents: r ** 2 is r squared and 2 ** 0.5 is the square root of 2. The caret is a different, unrelated operator, so never use it for powers.
1.3 Math functions and constants
Square root, trigonometry, logarithms, and the constant pi live in the math module. Write import math once, then call math.sqrt(x), math.sin(x), or use math.pi.
| You want | You write | Note |
|---|---|---|
| r squared | r ** 2 | power operator |
| square root | math.sqrt(x) | needs import math |
| the constant pi | math.pi | a value, not a function |
The three most common calculator moves. Every later module reuses them.
1.4 Variables
An assignment name = value stores a result under a name for reuse. Good names (radius, force) make code read like the engineering it represents and let you change one input and recompute.
1.5 Building a calculation
Break a formula into named steps rather than one long expression: assign the inputs, compute intermediate quantities, then the answer. This is easier to read, to check against a hand calculation, and to reuse.
Engineering connection: every stress, deflection, and energy formula in later courses becomes three lines of Python, inputs, formula, printed result, that you can re-run for any numbers.
Worked example 1: the area of a circle
Compute the area of a circle of radius 5, using Python as a calculator.
import math
r = 5
area = math.pi * r ** 2
print(area)
print(round(area, 2))
Output:
78.53981633974483
78.54
- ProblemFind the area of a circle of radius 5 in Python.
- Given / findr = 5. Find area = pi r squared.
- ModelStore the radius, then compute
math.pi * r ** 2. - Code
import mathmakes pi available;r ** 2squares the radius. - Solvearea = 78.54 (to two decimals), the full value being 78.5398...
- CheckBy hand, pi times 25 is about 3.1416 times 25 = 78.54, matching.
- ConclusionThree lines compute the area for any radius: change
rand re-run.
Worked example 2: Newton's second law
A mass of 2 kg accelerates at 9.81 m/s2. Compute the force with named variables.
m = 2 # mass in kg
a = 9.81 # acceleration in m/s2
force = m * a
print('Force =', force, 'N')
Output:
Force = 19.62 N
- ProblemFind the force from F = m a for the values in the code.
- Given / findm = 2 kg, a = 9.81 m/s2. Find F.
- ModelF = m a, built from two named variables.
- CodeThe
#starts a comment;printcan show text and values together. - Solveforce = 2 × 9.81 = 19.62 N.
- Check2 times about 9.8 is about 19.6 N, consistent.
- ConclusionNaming the inputs makes the code read like the physics and easy to reuse for other masses.
Misconceptions and diagnostics
| Mistake | Symptom | Diagnostic question | Correction |
|---|---|---|---|
| Using the caret for powers | Wrong, huge, or odd results | "Did I write ** for the exponent?" | Use **; the caret is a different operator. |
| Calling math functions without importing | A name error for pi or sqrt | "Did I write import math?" | Add import math once at the top. |
| One giant expression | Unreadable, hard-to-check line | "Can I name the intermediate steps?" | Assign inputs and intermediates to variables. |
| Forgetting precedence | Add-before-multiply errors | "Do I need brackets here?" | Use brackets to force the order you mean. |
Practice ladder
Compute the circumference of a circle of radius 5 (2 pi r) in Python.
Show answer
2 * math.pi * 5 gives 31.4159..., about 31.42.
Store base = 20 and height = 30 (mm) and compute the area of a triangle, one half base times height.
Show answer
0.5 * base * height = 0.5 × 20 × 30 = 300 (square mm).
Compute the hypotenuse of a right triangle with legs 3 and 4 using math.sqrt.
Show answer
math.sqrt(3 ** 2 + 4 ** 2) = math.sqrt(25) = 5.0.
Write three lines that compute the kinetic energy of a 1500 kg car at 20 m/s, one half m v squared, and print it with units.
What good work looks like
m = 1500, v = 20, ke = 0.5 * m * v ** 2 gives 300000 J (300 kJ). A good answer names the inputs, uses **, and prints the value with a unit label.
Working with AI, and proving it yourself
Use AI as a tutor, not a black box
Portfolio task
Take one formula from a past course and turn it into a named-variable Python calculation, verified against your hand result.
** operator or a math function, and a printed result checked by hand.Retrieval and spaced review
Closed notes. Answer out loud, then reveal.
1. What operator raises to a power?
**, for example r ** 2.
2. How do you use pi or sqrt?
Write import math, then math.pi or math.sqrt(x).
3. What does name = value do?
Stores the value under that name for reuse.
4. What is 2 + 3 * 4 in Python?
14, because multiplication happens before addition.
5. Why build from named variables?
Readable, checkable, and reusable for new inputs.
Textbook mapping
This module follows Hans Fangohr, Introduction to Python for Computational Science and Engineering. Use these references to read further.
| Topic in this module | Where to read more |
|---|---|
| The interpreter and calculator | Fangohr, Chapter 2, A Powerful Calculator |
| Math functions and variables | Fangohr, Sections 2.4 and 2.5 |
| Why Python for engineering | Fangohr, Chapter 1, Introduction |
Chapter numbers refer to Fangohr's Introduction to Python for Computational Science and Engineering. Any recent version is equivalent for study.