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.

01

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.
0 or 1 weak itemsContinue with this module.
2 weak itemsRevisit arithmetic in Mathematics for Mechanical Engineers.
3 or more weak itemsSkim the study method, then return.
02

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.

area = math.pi * r ** 2** is the power operator (two stars)name = value stores a result

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.

The skill works when: you build a result from named variables and use ** and math functions correctly.
The skill breaks down when: the caret is used for powers, or math functions are called without import math.
The concept. The read-evaluate-print loop: you type an expression, Python evaluates it with the usual precedence, and returns a value you can store in a variable.
03

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 wantYou writeNote
r squaredr ** 2power operator
square rootmath.sqrt(x)needs import math
the constant pimath.pia 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.

04

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
  1. ProblemFind the area of a circle of radius 5 in Python.
  2. Given / findr = 5. Find area = pi r squared.
  3. ModelStore the radius, then compute math.pi * r ** 2.
  4. Codeimport math makes pi available; r ** 2 squares the radius.
  5. Solvearea = 78.54 (to two decimals), the full value being 78.5398...
  6. CheckBy hand, pi times 25 is about 3.1416 times 25 = 78.54, matching.
  7. ConclusionThree lines compute the area for any radius: change r and re-run.
Result. area = 78.54 (square units).
05

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
  1. ProblemFind the force from F = m a for the values in the code.
  2. Given / findm = 2 kg, a = 9.81 m/s2. Find F.
  3. ModelF = m a, built from two named variables.
  4. CodeThe # starts a comment; print can show text and values together.
  5. Solveforce = 2 × 9.81 = 19.62 N.
  6. Check2 times about 9.8 is about 19.6 N, consistent.
  7. ConclusionNaming the inputs makes the code read like the physics and easy to reuse for other masses.
Result. Force = 19.62 N.
06

Misconceptions and diagnostics

MistakeSymptomDiagnostic questionCorrection
Using the caret for powersWrong, huge, or odd results"Did I write ** for the exponent?"Use **; the caret is a different operator.
Calling math functions without importingA name error for pi or sqrt"Did I write import math?"Add import math once at the top.
One giant expressionUnreadable, hard-to-check line"Can I name the intermediate steps?"Assign inputs and intermediates to variables.
Forgetting precedenceAdd-before-multiply errors"Do I need brackets here?"Use brackets to force the order you mean.
07

Practice ladder

Level 1 · Direct skill

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.

Level 2 · Mixed concept

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).

Level 3 · Independent problem

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.

Transfer task | Real engineering

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.

08

Working with AI, and proving it yourself

Use AI as a tutor, not a black box

"Explain why 2 + 3 * 4 is 14 and not 20 in Python."
"Give me three arithmetic expressions; I will predict the value, then run them."
"Write my whole calculation." Type it yourself; that is the skill.
"Just tell me the answer." Predict, run, and compare instead.

Portfolio task

Take one formula from a past course and turn it into a named-variable Python calculation, verified against your hand result.

Must include: named inputs, the ** operator or a math function, and a printed result checked by hand.
09

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.

TodayFinish this quiz and Levels 1 and 2 of the ladder.
+1 dayRe-type both worked examples from memory.
+3 daysTurn one hand calculation into Python.
+7 daysMove on to numbers and types in Module 2.
+30 daysReach for Python whenever you would reach for a calculator.
10

Textbook mapping

This module follows Hans Fangohr, Introduction to Python for Computational Science and Engineering. Use these references to read further.

Topic in this moduleWhere to read more
The interpreter and calculatorFangohr, Chapter 2, A Powerful Calculator
Math functions and variablesFangohr, Sections 2.4 and 2.5
Why Python for engineeringFangohr, Chapter 1, Introduction

Chapter numbers refer to Fangohr's Introduction to Python for Computational Science and Engineering. Any recent version is equivalent for study.