Python WASM Terminal

Chapter 0: Python WASM Terminal

Getting Started

Welcome to your first hands-on Python experience! In this chapter, you'll learn to use an interactive Python terminal that runs directly in your web browser—no installation required. This terminal is powered by Pyodide, a version of Python compiled to WebAssembly (WASM) that executes natively in modern browsers.

The beauty of this approach is simplicity. Traditional Python development requires installing Python on your computer, configuring your environment, and managing dependencies. With our browser-based terminal, you can start writing and executing Python code immediately. Everything runs locally in your browser, providing a genuine Python experience without any setup barriers.

This chapter focuses on three essential skills: navigating the terminal interface, writing your first Python statements, and understanding how the terminal responds to your code. You'll make mistakes—that's not just okay, it's essential. Learning to read error messages and debug simple problems is a fundamental programming skill that starts here.

Using the Terminal

The Python terminal operates on a simple read-eval-print loop (REPL). You type a Python statement, press Enter, and the terminal immediately evaluates your code and displays the result. This interactive feedback makes the terminal perfect for learning, experimenting, and testing ideas quickly.

When you see the prompt (>>>), the terminal is ready for input. Type your code and press Enter to execute it. The terminal will display any output your code produces, then show another prompt. If you're typing a multi-line statement like a function or loop, you'll see a continuation prompt (...) indicating the terminal expects more input before executing.

The terminal maintains state between executions. Variables you create remain available for subsequent commands, letting you build up code incrementally. This persistence is ideal for exploration—you can define a variable, use it in a calculation, modify it, and use it again, all in separate commands.

One important note: the WASM terminal has some limitations compared to desktop Python. File system access is restricted, some C-extension libraries aren't available, and performance may be slower for compute-intensive tasks. For learning fundamental Python concepts, these limitations don't matter. Focus on understanding Python's core features; you'll have plenty of time later to explore advanced capabilities.

Your First Python Code

Let's start with the traditional first program—displaying text on screen. In Python, the print() function outputs text:

When you run this code, Python displays: Hello, Python!. The print() function takes whatever you put inside the parentheses and shows it in the terminal. Text (called "strings" in programming) must be wrapped in quotes—either single (') or double (").

Try printing different messages. Experiment with quotes:

print('Welcome to Python')
print("Python is fun!")
print("She said, 'This is amazing!'")

Notice the third example uses double quotes for the string and single quotes inside. This is how Python handles quotes within text. You can also use escape sequences like \" to include double quotes inside a double-quoted string.

Now let's use Python as a calculator. Python can perform arithmetic directly:

42 + 8
150 - 75
12 * 5
100 / 4

Type each line and observe the results. Python displays the calculated value immediately. The operators work as expected: + for addition, - for subtraction, * for multiplication, and / for division.

You can combine operations and use parentheses to control order:

(10 + 5) * 2
100 / (5 + 5)
20 + 3 * 4

The last example demonstrates operator precedence—multiplication happens before addition, just like in mathematics. Use parentheses when you need to override the default order.

Variables and Simple Operations

Variables store values for later use. Think of a variable as a labeled box where you can put data. Create a variable by writing its name, an equals sign, and the value:

age = 25
name = "Python Learner"
pi = 3.14159

Variable names should be descriptive. Use lowercase letters and underscores for readability: user_age, total_count, max_value. Avoid starting names with numbers or using Python keywords like print, if, or for.

Once you've created variables, you can use them in calculations:

price = 19.99
quantity = 3
total = price * quantity
print(f"Price: ${price}")
print(f"Quantity: {quantity}")
print(f"Total: ${total}")

Variables can be reassigned new values at any time. The old value is replaced:

count = 5
print(count)
count = count + 1
print(count)

This pattern—taking a variable's current value, modifying it, and storing the result back—is extremely common in programming. The second count = count + 1 reads the current value (5), adds 1, and stores the result (6) back in count.

Understanding Errors

Errors are learning opportunities, not failures. When you make a mistake, Python provides an error message explaining what went wrong. Learning to read these messages is crucial.

A syntax error means Python doesn't understand your code's structure:

print("Missing closing quote)

This produces a syntax error because the string isn't closed properly. Python points to where it got confused, helping you locate the problem.

A name error occurs when you reference a variable that doesn't exist:

print(undefined_variable)

Python can't find undefined_variable, so it raises a NameError. Check for typos in variable names or ensure you've created the variable before using it.

Type errors happen when you try to perform invalid operations:

"text" + 5

You can't add a string and a number directly. Python needs both values to be the same type. Convert the number to a string first: "text" + str(5).

When you encounter errors, read the message carefully. Python tells you the error type and often points to the problematic line. Don't feel discouraged—professional programmers deal with errors constantly. The difference is experience reading and fixing them quickly.

Practice Exercises

Now it's time to apply what you've learned. Work through these exercises in the terminal:

Exercise 1: Personal Introduction
Create variables for your name, age, and favorite hobby. Use print() to display a sentence introducing yourself.

Exercise 2: Temperature Conversion
Create a variable celsius with value 25. Calculate the Fahrenheit equivalent using the formula: fahrenheit = celsius * 9/5 + 32. Print the result.

Exercise 3: Circle Calculations
Create a variable radius with value 7. Calculate the circle's area using the formula: area = 3.14159 * radius * radius. Print the area.

Exercise 4: String Combinations
Create two variables: first_name and last_name. Combine them with a space in between and print the full name. (Hint: Use + to concatenate strings)

Exercise 5: Experiment with Errors
Intentionally create a syntax error by forgetting to close a string. Read the error message. Then fix it and run the corrected code. This practice helps you become comfortable with errors.

Take your time with these exercises. Experiment with variations. Try different values, different variable names, different ways of combining operations. The more you experiment, the more intuitive Python becomes.

Next Steps

Congratulations on completing your first hands-on Python chapter! You've learned to use the Python terminal, execute code, work with variables, and understand error messages. These foundational skills support everything that follows.

In the next chapter, we'll explore Python's data types in depth—strings, numbers, booleans, and more. You'll learn how Python categorizes data and what operations work with each type.

Ready to continue? Move on to Chapter 1: Variables and Data Types.