- 01 - Housekeeping
- 02 - Python the Language
- 03 - Your Engineering System
- 04 - Jupyter Notebooks 101
- 05 - Basic Syntax
- 06 - Data Structures
- 07 - Control Flow
- 08 - Functions
This is the real beginning. Syntax is the grammar of Python. Get it right and you can express anything. Most of what follows looks simple on paper, but the concepts form the backbone of every project you'll build later. Don't rush, repetition is where fluency comes from.
Python uses indentation to define code blocks. Four spaces is the convention; stick with it and life stays easy. You can use the 'Tab' key to indent four spaces.
if 5 > 2:
print("Five is greater than two!")If you skip the indentation, Python raises an error immediately. That immediate feedback is a feature, not a pain point, it allows you to read code and identify hierarchichal structures.
Comments are the notes you leave for future-you (and everyone else who reads the code). Use # for single-line comments and triple quotes for longer documentation blocks when needed.
# Tributary area in square metres
tributary_area = span * spacingPython creates a variable as soon as you assign a value to it. The interpreter infers the type.
my_integer = 10 # int
my_float = 20.5 # float
is_coding_fun = True # bool
favourite_language = "Python" # strA quick refresher on naming rules:
- Start with a letter or underscore.
- Use letters, numbers, and underscores only.
- Names are case-sensitive.
Python keeps a list of reserved keywords you cannot use as variable names (for, if, return, etc.). Hit help("keywords") in the interpreter to see the latest list.
-
Expressions produce a value.
2 + 3 len("beam")
-
Statements perform an action.
x = 5 print(x)
Many statements contain expressions; understanding the difference helps when we talk about control flow.
Python follows the same rules you learned in school: Parentheses, Exponents, Multiplication, Division, Addition, Subtraction.
Remember PEMDAS:
- Parentheses
- Exponents
- Multiplication
- Division
- Addition
- Subtraction
Operations at the same level (multiplication/division, addition/subtraction) are evaluated left to right.
Examples:
result = 2 + 3 * 4 # 14
result = (2 + 3) * 4 # 20
result = 2 ** 3 * 4 # 32| Operator | Description | Example | Result |
|---|---|---|---|
+ |
Addition | 5 + 3 |
8 |
- |
Subtraction | 5 - 3 |
2 |
* |
Multiplication | 5 * 3 |
15 |
/ |
Division (always float) | 5 / 2 |
2.5 |
// |
Floor division | 5 // 2 |
2 |
% |
Modulus (remainder) | 5 % 2 |
1 |
** |
Exponentiation | 5 ** 3 |
125 |
A few reminders:
- Division always returns a float. Use
//when you need integer results. - Modulus is handy for checking divisibility; for example,
n % 2 == 0checks for even numbers. - Exponentiation uses
**, not^.
Reading and writing text files is a day-one task for most engineers. Use the with statement so files close automatically.
from pathlib import Path
data_path = Path('loads.txt')
with data_path.open('w', encoding='utf-8') as fh:
fh.write('Roof = 12.5\n')
with data_path.open('r', encoding='utf-8') as fh:
for line in fh:
print(line.strip())The first block writes a single line. The second block reads the file back and strips the newline so you get clean output.
Errors are part of the process. You will run into three main categories:
- Syntax errors - Python cannot even run the code. Missing colons or indentation slips cause these.
- Runtime errors - The code runs and then breaks (e.g. dividing by zero, missing files).
- Logical errors - The code runs, but the result is wrong. These require reasoning and instrumentation.
Strategies that work:
- Read the error message. It tells you the line and usually the reason.
- Sprinkle
print()statements or use breakpoints in VS Code to inspect variables. - Reduce the problem to the smallest snippet that still fails.
- Explain the code out loud (rubber duck debugging). It's amazing how often you catch the issue mid-sentence.
Keep your notebooks or scripts tidy, name variables clearly, and errors become easier to hunt down.
If you want to see the ideas in action, load examples/01-basic-syntax-exercises.ipynb.
(c) Flocode, 2025