Skip to main content
PROGRAMMING / PYTHON / LESSON 01

Python Fundamentals.

Learn to think through a program, then write it yourself. Start with variables, types and comparisons; finish by building your first useful IT status script.

Foundations40–55 minutesCode execution in browserNo install or account
00

What you'll learn

Programming becomes much easier when you can explain what each line changes. This lesson deliberately focuses on a small set of ideas and revisits them in increasingly realistic examples.

  • Explain what a variable stores and how assignment changes its value.
  • Distinguish str, int, float and bool and use explicit type conversion safely.
  • Predict the order in which assignment, arithmetic and print statements execute.
  • Build a Boolean comparison before introducing if / else logic.
  • Read a traceback, inspect variables and correct a small script independently.
  • Format strings with f-strings, placing values into a readable report.
Our approach: Read a short explanation, predict the result, trace the state, and only then modify working code. The final task asks you to create the answer—not recognize it.
CODE IS A SEQUENCE OF INSTRUCTIONS
01

Your first Python program

Python reads your script from top to bottom. A statement performs an action: assign a value, calculate a result, or print output. You don't need an IDE or a complex app to begin—just a question and a few instructions.

Names and values

device = "SW-01" assigns the string SW-01 to a variable named device. The name points to a value; it is not the value itself.

Print shows a result

print(device) writes the variable's current value to standard output. Writing print("device") prints the literal word device instead.

PYTHON / REASON THROUGH THE OUTPUTRead before you run
device = "SW-01"
print(device)
print("device")
What are the two printed lines? Reveal explanation after you predict.

First line: SW-01 (the variable value). Second line: device (the literal string).

Remember: Start by asking: what information do I have, and what result do I want to produce?
OPTIONAL / REAL PYTHON ON YOUR OWN COMPUTER

Run your first .py file outside JoeLearns

You can use the browser editor for every exercise in this path. But if you'd like to practice as a developer or IT technician would, you can install Python and run the same program on your computer right now. No account, editor subscription, or extra packages are necessary.

  1. Install only if needed. Download Python from the official Python downloads page. For Linux, prefer your distribution's Python package. If Python is already installed, skip straight to the next step.
  2. Confirm the interpreter runs. Open a fresh terminal (PowerShell on Windows or Terminal on macOS/Linux) and enter:
    py --version
    You should see a Python 3 version. The exact version number can differ from the browser's Python runtime.
  3. Create a script. Make a new folder named python-practice. Choose a code-friendly plain-text editor: on Windows, Notepad++ is lightweight and highlights Python syntax; on Linux, Vim is available in many terminals but has a learning curve. VS Code is an approachable option on both, and Python IDLE works too. Windows Notepad can save a .py file, but for learning to program it offers fewer coding aids such as Python-aware syntax highlighting, indentation support and error cues. You may be thinking of Notepad++, rather than WordPad. Open first_script.py inside the folder and save these three lines:
    device = "SW-01"
    print(device)
    print("device")
    Check that the filename ends with .py, not .py.txt. Code-focused editors make indentation, quotes and accidental .txt extensions easier to notice, but none is required.
  4. Run the file. In File Explorer, open the python-practice folder. Right-click an empty area inside the folder (not the .py file), then choose Open in Terminal if available. Alternatively, click File Explorer’s address bar, type powershell, and press Enter. Both open a terminal in that folder. The built-in terminal in a code editor is fine too. Enter:
    py first_script.py
    The output should be two lines: SW-01 and device. If Python reports it cannot find the script, check the terminal's current folder and the filename.
  5. Get real feedback from an error. Change print(device) to print(devce), save, and run the file again. Read the NameError traceback, then correct the typo and rerun it. You've just completed the same debug-and-verify cycle used throughout this path.
Python command not found, script won't run, or unsure what to install?

If py isn't recognized, try python --version and python first_script.py. If neither works, follow the official Windows installation and command troubleshooting guide. For installation and command troubleshooting, see the Official Python Windows guide. If you are on a managed school or work device, ask an administrator before installing software.

Keep both options: JoeLearns runs Python in an isolated browser runtime; your local Python runs on your actual computer and can access files you permit. Practice with fictional data, use a dedicated folder, and never run code you don't understand. Later lessons will suggest safe local experiments too.
STRINGS ARE NOT NUMBERS
02

Types change how expressions behave

Python keeps text and numeric values distinct. A value that looks like a number can still be a string if it comes from a text source. Learn to check and convert types instead of guessing what an expression will do.

Common built-in types

str stores text, int stores whole numbers, float stores decimal numbers, and bool stores True or False. Python variable names don't require type declarations.

Conversion is deliberate

int("443") produces integer 443. int("not-a-number") raises ValueError. Validate uncertain user input before converting; never assume every string is numeric.

What does type() show?

type(value) displays the Python type, such as <class 'str'> or <class 'int'>. You may later see type(value).__name__: it retrieves only the name str or int from that type. The double underscores are part of Python's built-in metadata. You don't need that attribute to solve this lesson.

PYTHON / REASON THROUGH THE OUTPUTRead before you run
port_text = "443"
port_number = int(port_text)
print(type(port_text))
print(type(port_number))
print(port_number + 1)
Why does the last line print 444, but port_number still stores 443? Reveal explanation after you predict.

port_number stores integer 443. type(port_text) prints the type str, and type(port_number) prints int (shown as class names). The final print calculates 443 + 1 without changing port_number.

Remember: '443' is text. 443 is an integer. Explicitly convert input when you need numerical operations.
FORMAT STRINGS WITHOUT GUESSWORK
03

Put variables into readable text

A useful script often needs one sentence containing both text and variable values. In Python, an f-string starts with f immediately before a quoted string. Place an expression inside curly braces to insert its current value. Python evaluates what is inside the braces before printing. This is how we can create device reports without accidentally trying to add text and numbers together.

Read the f-string out loud

f'{device}: {ports} ports online' means: insert device, print a colon and space, insert ports, then write the remaining text. An f-string can contain numbers without manual str() conversion.

Compare literal text with substituted values

print('{device}') outputs the literal braces and name. print(f'{device}') replaces the braces with the variable's value. A missing variable inside braces raises NameError.

PYTHON / REASON THROUGH THE OUTPUTRead before you run
device = "SW-01"
ports_online = 4
print(f"{device}: {ports_online} ports online")
print("{device}")
What prints on each line? Why does only the first string show the hostname? Reveal explanation after you predict.

The first line is SW-01: 4 ports online. The second is the literal text {device}, because it has no leading f. The first f-string evaluates both expressions inside braces before printing.

Remember: The f belongs directly before the opening quote; curly braces contain an expression such as {ports_online}, not a literal word.
VALUES CHANGE ONE LINE AT A TIME
04

Track assignment, not just output

The equals sign assigns a value. It does not assert mathematical equality. Each subsequent assignment updates the variable, and later statements use its most recent value. Trace this sequence line by line to see the difference.

Assignment is not comparison

count = 4 stores 4. count == 4 compares the value with 4 and produces a Boolean.

Augmented assignment

count += 2 is a shorter way to write count = count + 2. It updates count using the previous value.

PYTHON / REASON THROUGH THE OUTPUTRead before you run
ports_online = 4
ports_online += 2
ports_online = ports_online - 1
print(ports_online)
What is the final value? Reveal explanation after you predict.

5: four, then six, then five.

Remember: To understand a program, keep a small table of variable values after each executed statement.
BOOLEAN LOGIC IS AN EXPRESSION
05

Write the condition before the decision

Before writing if, make sure you can form an expression that returns True or False. Comparisons such as >, >=, == and != allow a script to evaluate the evidence rather than relying on a hard-coded answer.

Comparisons yield bool

latency_ms > 150 evaluates to True or False. Comparing strings with numbers raises TypeError; normalize the data type first.

Combine conditions

and requires both conditions to be true; or needs at least one; not reverses a Boolean. Parentheses clarify longer expressions.

PYTHON / REASON THROUGH THE OUTPUTRead before you run
latency_ms = 185
threshold_ms = 150
needs_attention = latency_ms > threshold_ms
print(needs_attention)
What happens when latency_ms becomes 85? Reveal explanation after you predict.

The expression becomes False because 85 is not greater than 150.

Remember: You can store the outcome of a comparison in a Boolean variable and inspect it with print().
MAKE YOUR LOGIC READ LIKE A DECISION
06

Only then add if and else

An if statement executes an indented block only when its condition is truthy. An else block handles the other branch. Colons and indentation are part of Python syntax, not decoration.

Branching

If status != "online", run the investigation branch. Otherwise, print Healthy. Only one branch executes.

Edge cases

Test both sides of your threshold. For >= 5, the value 5 counts; for > 5, it does not. Boundaries cause real automation defects.

PYTHON / REASON THROUGH THE OUTPUTRead before you run
status = "offline"
if status != "online":
    print("Investigate")
else:
    print("Healthy")
Which line prints if status is online? Reveal explanation after you predict.

Healthy; the inequality is False, so else runs.

Remember: A correct condition with incorrect indentation is still an incorrect program. Use the trace to discover which line ran.
TRACEBACKS ARE CLUES
07

When code fails, use the evidence

Errors are feedback about what Python tried to do. A SyntaxError often points to structure; a NameError to a missing name; a TypeError to an unsupported operation. Read the error and test a small hypothesis instead of rewriting everything.

Use the line number

Look for the line inside your own file shown in the traceback. The error often points to the operation that failed, even if the original bad value came earlier.

Output isn't proof of correctness

A script can run without errors yet produce the wrong answer. Compare actual output and variable types with the stated requirement and edge cases.

PYTHON / REASON THROUGH THE OUTPUTRead before you run
ports_text = "4"
# Fix the conversion before doing arithmetic
ports_total = int(ports_text) + 2
print(ports_total)
What would happen if int(ports_text) were removed? Reveal explanation after you predict.

The expression "4" + 2 would raise TypeError because str and int cannot be added together.

Remember: Fix one problem at a time. Run again, read the output, inspect the final variables and check the requirement.
THE SKILL IS NOT MEMORIZING SYNTAX
08

Turn a requirement into Python

A technician often starts with words rather than code: 'Count online ports and print a short report.' Translate that request into named values, an expression, an output format and a small test. Try the first exercise below without opening a solution.

Identify inputs

Separate what is given (device name, port count) from what you must compute or display.

Define success

Know what the output must look like, including spaces, punctuation and the expected data types. Then modify the starter code and run it.

PYTHON / REASON THROUGH THE OUTPUTRead before you run
device_name = "SW-01"
ports_online = 4
print(device_name)
Does this program meet a requirement to print 'SW-01: 4 ports online'? Reveal explanation after you predict.

No. It runs successfully but omits the port count and formatting.

Remember: Start with the requirements. Write only the smallest set of lines needed to prove the result.
09 / APPLY IT YOURSELF
09

Write your first working status report

You've seen the concepts in isolation. Now work from a requirement instead of a completed program. Use the editor, run actual Python, and step through variables until you can explain the result. The starter intentionally does not satisfy the task.

JOELEARNS / PYTHON WORKBENCH

Write it. Run it. Understand it.

Real Python execution, a variable trace and feedback against the requirements. You write the code—answers aren't revealed up front.

Python 3.14 · in-browser
CHALLENGE 01 / 5 · BEGINNER

A readable device report

Northstar needs a one-line status label. Use the existing variables to print the device name followed by a colon, a space, the count, and 'ports online'. Match the punctuation exactly.

EXPECTED OUTPUTSW-01: 4 ports online
script.py editableCtrl / Cmd + Enter to run
Your code is stored only in session storage when enabled. Never paste secrets. Clearing this option clears saved drafts, not the editor.
Code executes only when you choose Run or Check. No account needed.
HINTS · OPTIONAL

Try solving it before asking for help. Hints never show a completed program.

PYTHON OUTPUTReady

Your printed output and errors will appear here.

VARIABLE TRACER

Run the code, then step through the executed lines to watch variable values change.

About this environment: The Python runtime is loaded on demand from a version-pinned CDN and runs inside a sandboxed, opaque-origin browser frame. Runs have a time limit. OS/network automation is outside this introductory workbench. If your code calls input(), a response panel appears to supply text to those calls. Don't paste secrets or untrusted scripts.

Next required step: complete the 5-challenge Practice milestone →
NEXT MILESTONE / PRACTICE

Now complete Python Fundamentals Practice.

This is the next step in the Python Fundamentals learning path—not an optional collection of unrelated activities. Complete the five coding challenges, including the type conversion and troubleshooting tasks, before moving on to the next written lesson.

Start the next milestone: Python Fundamentals Practice →
10

Reference material

You don't need to memorize everything. Learn how to read the Python documentation and use it when a behavior is unfamiliar.

NEXT REQUIRED STEP · PYTHON FUNDAMENTALS

Complete the Practice milestone

You've reached the end of this written lesson. Your next step is practice—complete Python Fundamentals Coding Challenges before proceeding to a new lesson. This is milestone 2 of 18. You can revisit the explanation whenever you need it.