LESSON 1 OF 4
Run a programme and understand its values
Your environment and first execution
Install a supported Python 3 interpreter from the official Python distribution or your approved package manager. On some systems the command is python; on others it is python3. Run the relevant command with --version and record the result. The examples use the standard library; installing a package manager or cloud SDK is unnecessary for the core course.
Create an empty practice directory and open a terminal there. Save a file named hello.py containing print("Inventory practice"). Run python hello.py or python3 hello.py. The expected output is one line: Inventory practice. If the terminal says it cannot open the file, inspect the working directory and filename. If it says the command is unknown, inspect interpreter installation and command selection. Those are different failures.
An interactive prompt executes statements as you enter them. A script stores statements in a file and executes them in order. You can explore a small expression interactively, but put the lab programme in a file so you can reproduce it. Do not paste the prompt marker >>> into a script; that marker belongs to the documentation's presentation.
A Python file is text, not a Word document. Be careful that an editor has not saved hello.py.txt. A visible file label may conceal an extension depending on operating-system settings. Record the interpreter command and actual file path in your notes so another learner can reproduce your run.
Names, values and types
Assignment binds a name to a value. In threshold = 10, the name is threshold and the value is an integer. The equals sign here performs assignment; it is not an equality test. The expression threshold == 10 evaluates whether two values compare equal. Confusing the two operations creates either a syntax error or the wrong logic, depending on the context.
host = "LAB-A"
free_text = "9"
threshold = 10
print(host)
print(type(free_text).__name__)
print(type(threshold).__name__)
print(free_text + "1")
print(int(free_text) + 1)
The outputs are LAB-A, str, int, 91 and 10. The string operation joins characters; it does not add numbers. CSV fields will arrive as strings, so this distinction is central to the report you are building. A number-looking value is not necessarily already a numeric value.
Reassigning threshold later changes what that name refers to. It does not rewrite every earlier calculation. For example, calculate limit_plus_one = threshold + 1, then set threshold = 20. limit_plus_one remains 11 because its assignment already evaluated the expression. A name is not a spreadsheet cell with an automatically recalculated formula.
Choose descriptive names such as free_pct and invalid_count. Short names can be useful in very small expressions, but a reporting programme benefits from names describing meaning and units. free_pct is different from free_gb; comparing a percentage to a threshold expressed in gigabytes is a requirements error even when the code runs.
Strings, formatting and exact observations
A string is a sequence of characters. Quoting marks its boundaries in source code. A printed value does not normally include the source quotes. Leading and trailing spaces can be part of the value even when they are difficult to see in a terminal.
raw_host = " LAB-A "
clean_host = raw_host.strip()
print(repr(raw_host))
print(repr(clean_host))
print(f"Host: {clean_host}; threshold: {10}%")
The first two lines display the difference between the raw and cleaned strings. repr() is helpful when diagnosing whitespace because it gives a representation of the value rather than pretending spaces are irrelevant. strip() returns a new string; it does not mutate the original string.
Use an f-string for readable presentation. It evaluates expressions inside braces and produces a string. Presentation should come after calculation. If your only output is a formatted sentence, another programme may have to parse that sentence to reuse the data. A structured dictionary or CSV report is usually a better boundary between calculation and presentation.
Lab: distinguish valid data from an absent measurement
Predict the result of int("9"), int("0"), int("unknown") and int("") before testing. The first two conversions succeed. The latter two raise ValueError. An unknown measurement is not zero; zero is an actual number. A report that substitutes zero for every conversion failure can wrongly tell an operator that all affected disks are full.
Now test int("9.5"). It also fails. Decide whether the specification permits decimal percentages before changing the programme to float(). A conversion rule is part of the data contract. If decimals are supported, test boundary values such as 9.9 and 10.0, and think about non-finite values separately. Do not quietly change the input contract because one example was inconvenient.
Check your understanding
Write a short explanation of why "9" + "1" produces "91". Then state what int("9") + 1 produces and where the conversion occurs. Explain why a missing measurement cannot safely become zero. If your explanation relies only on “Python knows what I mean,” revisit the types shown by type().
Worked answer: the first expression joins two strings. The second converts the first string to an integer before arithmetic. A missing measurement contains insufficient evidence for a numeric claim. The eventual report must preserve that distinction with an invalid or missing-data classification.
Reasoning model
Separate parsing, validation, calculation and presentation. A pure function receives data and returns a result without reading files or changing external state, making boundary tests inexpensive. Catch the narrow failure you expect rather than hiding every exception. Decide whether invalid rows stop the whole job or become structured rejections; either policy must preserve a visible audit of what was excluded.
YOUR NEXT STEP
Keep building your skills
No directly related CogniSkill book is mapped to this course yet. Continue with the free learning, or compare the publishing catalogue by your next goal.
Explore books by topic and goal →Sign in to save lesson completion. Reading position is separate from completion.
Practice notes — your working space
Save predictions, outputs and explanations as you work. Notes stay on this browser, not your account; people sharing this browser can access them. Avoid passwords and sensitive data. Export to keep a portable copy, or clear your notes before leaving a shared device.
How was this lesson?
Help us improve this lesson.