Learn Without Walls
← Lesson 3 Lesson 4 of 4 Practice Problems →

Lesson 4: Using the Python Interpreter

⏱️ Estimated time: 20-25 minutes

📚 Learning Objectives

By the end of this lesson, you will be able to:

What is the Python Interpreter?

REPL (Read-Eval-Print Loop) stands for Read (take your input), Eval (evaluate/run it), Print (show the result), Loop (repeat). It's an interactive environment where you type Python code one line at a time and immediately see the results.

The Python interpreter is like having a conversation with Python. You type something, Python responds, and you can type something else based on what you learned. It's the best way to experiment, test ideas, and learn new concepts.

Starting the Interpreter

Open your terminal and type:

python

(On Mac/Linux, you may need to type python3)

You'll see something like:

Python 3.12.1 (main, Dec  7 2023, 20:45:44)
Type "help", "copyright", "credits" or "license" for more information.
>>>

The >>> prompt means Python is waiting for your input. You can also access the REPL by opening IDLE.

Exiting the Interpreter

To exit, type any of these:

>>> exit()
# or
>>> quit()
# or press Ctrl+D (Mac/Linux) or Ctrl+Z then Enter (Windows)

Python as a Calculator

The interpreter is an excellent calculator. Python supports all the standard arithmetic operations:

>>> 2 + 3          # Addition
5
>>> 10 - 4         # Subtraction
6
>>> 6 * 7          # Multiplication
42
>>> 15 / 4         # Division (always returns a decimal)
3.75
>>> 15 // 4        # Integer division (rounds down)
3
>>> 15 % 4         # Modulo (remainder)
3
>>> 2 ** 10        # Exponentiation (2 to the power of 10)
1024

📈 Operator Summary

Operator Name Example Result
+Addition5 + 38
-Subtraction10 - 46
*Multiplication6 * 742
/Division15 / 43.75
//Integer Division15 // 43
%Modulo15 % 43
**Exponentiation2 ** 38

Order of Operations

Python follows standard math rules (PEMDAS):

>>> 2 + 3 * 4       # Multiplication first
14
>>> (2 + 3) * 4     # Parentheses change the order
20

✍️ Try It Yourself!

Open the Python interpreter and try these calculations:

  • How many minutes are in a week? (60 * 24 * 7)
  • If a pizza has 8 slices and 3 people share equally, how many slices does each get and how many are left over? (Use // and %)
  • What is 2 to the power of 16? (2 ** 16)

Variables in the Interpreter

You can create variables right in the interpreter to store values and reuse them:

>>> name = "Alice"
>>> age = 25
>>> print("Hello,", name)
Hello, Alice
>>> print(name, "is", age, "years old")
Alice is 25 years old

In the interpreter, you can also just type a variable name to see its value:

>>> x = 42
>>> x
42
>>> x + 8
50

Variable is a name that refers to a value stored in the computer's memory. Think of it as a labeled box where you can put data. We'll explore variables in depth in Module 2.

💡 Interpreter Shortcut

In the interpreter (but not in script files), you can see a value by just typing its name without print(). This is because the REPL automatically displays the result of the last expression.

>>> 5 + 3        # In the interpreter, this shows 8
8
# In a .py file, you'd need: print(5 + 3)

Exploring with type() and help()

Two very useful built-in functions for exploration:

The type() Function

type() tells you what kind of data you're working with:

>>> type(42)
<class 'int'>
>>> type(3.14)
<class 'float'>
>>> type("hello")
<class 'str'>
>>> type(True)
<class 'bool'>

🔎 Python's Basic Data Types (Preview)

Type Name Examples
intInteger42, -7, 0, 1000
floatDecimal number3.14, -0.5, 2.0
strString (text)"hello", 'Python'
boolBooleanTrue, False

We'll study all of these in detail in Module 2!

The help() Function

help() provides documentation about any Python function or object:

>>> help(print)
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    Prints the values to a stream, or to sys.stdout by default.
    ...

Press q to exit the help viewer when you're done reading.

✍️ Try It Yourself!

In the interpreter, try these explorations:

  • What type is 100? What about 100.0? (Use type())
  • What type is "100"? (Hint: It has quotes!)
  • Try help(type) to learn about the type function itself

Multi-Line Code in the Interpreter

You can write multi-line code in the interpreter. When Python expects more code (like after an if statement or a for loop), it shows ... instead of >>>:

>>> for i in range(5):
...     print(i)
...
0
1
2
3
4

After typing the indented code, press Enter on an empty line to execute it.

>>> if 10 > 5:
...     print("Ten is greater than five!")
...
Ten is greater than five!

Interpreter vs. Script: When to Use Each

💬 Use the Interpreter When...

  • Testing a quick idea or calculation
  • Learning a new function or concept
  • Debugging—checking what a value or expression returns
  • Exploring data types with type()
  • Using Python as a calculator
  • Reading documentation with help()

📄 Use a Script File When...

  • Writing a program you want to save and reuse
  • Your code is longer than a few lines
  • Building a complete application or project
  • Sharing code with others
  • Writing code you'll run multiple times
  • Working on homework or assignments

Pro Tip: Use Both Together!

Experienced programmers often have the interpreter open alongside their script editor. They test ideas in the interpreter, and once the code works, they add it to their script file. This workflow speeds up development and reduces errors.

✅ Check Your Understanding

1. What does REPL stand for?

Answer: REPL stands for Read-Eval-Print Loop. Read (take input), Eval (evaluate/execute it), Print (display the result), Loop (repeat the process).

2. What is the result of 17 // 5 and 17 % 5?

Answer: 17 // 5 gives 3 (integer division, rounds down). 17 % 5 gives 2 (the remainder when 17 is divided by 5, since 5 goes into 17 three times with 2 left over).

3. In the interpreter, you type x = 10 and then x on the next line. What happens?

Answer: The interpreter displays 10. In the REPL, typing a variable name or expression shows its value automatically. In a script file, you would need to use print(x) to see the value.

4. What function tells you the data type of a value?

Answer: The type() function. For example, type(42) returns <class 'int'>, and type("hello") returns <class 'str'>.

🎯 Key Takeaways

Module 1 Complete!

Congratulations! You've finished all four lessons in Module 1. You now know what Python is, have it installed, can write and run programs, and can use the interactive interpreter. That's a solid foundation!

📝 Practice Problems

Put your new skills to the test with 10 practice problems covering everything from this module.

Practice Problems

🎯 Module Quiz

Ready to prove your knowledge? Take the 10-question Module 1 quiz!

Take the Quiz

📊 Module Home

Review all module materials, study guides, and quick reference cards.

Module 1 Home