The REPL: Python's Interactive Playground
REPL stands for Read-Eval-Print Loop. It's an interactive shell where you type Python code and immediately see the result. Essential for experimenting, testing ideas, and learning.
Launch it by typing python3 in your terminal:
$ python3
Python 3.12.0 (main, Oct 2 2023)
Type "help", "copyright", "credits" or "license" for more information.
>>> The >>> prompt means Python is waiting for your input. Start experimenting:
>>> 2 + 2
4
>>> 10 / 3
3.3333333333333335
>>> 2 ** 10
1024
>>> "Hello" + " " + "World"
'Hello World'
>>> len("Python")
6Math Operations in the REPL
>>> 5 + 3 # Addition โ 8
>>> 5 - 3 # Subtraction โ 2
>>> 5 * 3 # Multiplication โ 15
>>> 5 / 3 # Division (always float) โ 1.666...
>>> 5 // 3 # Floor division (integer) โ 1
>>> 5 % 3 # Modulo (remainder) โ 2
>>> 5 ** 3 # Exponentiation โ 125Variables in the REPL
>>> name = "Alice"
>>> age = 30
>>> name
'Alice'
>>> age + 5
35
>>> f"My name is {name} and I am {age} years old."
'My name is Alice and I am 30 years old.'Notice: just typing a variable name shows its value. In scripts, you'd need print().
Your First Script File
The REPL is for experiments. Real programs live in .py files. Create a file called hello.py:
# hello.py - My first Python script
# Lines starting with # are comments โ Python ignores them
name = input("What's your name? ")
age = int(input("How old are you? "))
print(f"Hello, {name}!")
print(f"In 10 years, you'll be {age + 10} years old.")
print("Welcome to Python.")Run it from your terminal:
$ python3 hello.py
What's your name? Alice
How old are you? 30
Hello, Alice!
In 10 years, you'll be 40 years old.
Welcome to Python.Understanding the Code Line by Line
input("prompt")โ displays the prompt and reads text the user types. Always returns a string.int()โ converts a string to an integer. Needed becauseinput()always gives strings.print()โ outputs to the terminal. Takes any number of arguments.f"...{variable}..."โ an f-string (formatted string literal). Variables inside{}are substituted. Python 3.6+.# commentโ Python ignores everything after#on a line.
The print() Function in Detail
# print() can take multiple arguments (separated by commas)
print("Name:", name, "Age:", age)
# Name: Alice Age: 30
# sep= controls the separator (default is space)
print("a", "b", "c", sep="-")
# a-b-c
# end= controls what's printed at the end (default is newline)
print("Loading", end="...")
print("Done!")
# Loading...Done!
# print multiple lines
print("""
Welcome to Python!
Let's learn together.
""")Comments and Docstrings
# Single line comment
# Multi-line comment โ just use multiple # lines
# This is line 1
# This is line 2
"""
This is a docstring โ a string literal at the top of a file,
function, or class. Used for documentation.
Not executed as code.
"""
print("After the docstring")Common Beginner Errors
| Error | Cause | Fix |
|---|---|---|
IndentationError | Wrong indentation (spaces vs tabs) | Use 4 spaces consistently |
NameError: name 'x' is not defined | Variable used before assignment | Define the variable first |
TypeError: can only concatenate str (not "int") to str | Mixing strings and numbers | Use f-strings or str() |
SyntaxError | Missing colon, bracket, or quote | Read the error line number |
Key Takeaways
- REPL for experiments: instant feedback, great for learning
- .py files for real programs: executed top to bottom
- input() always returns strings: convert with int(), float() for numbers
- f-strings are the modern way:
f"Hello {name}"beats string concatenation - Read error messages: Python tells you the line number and what went wrong
Practice Exercises
- In the REPL, calculate: what is 2 to the power of 32?
- Create a script that asks for two numbers and prints their sum, difference, product, and quotient.
- Modify the script to also handle division by zero gracefully (just print a message if the user enters 0).
- Create a "Mad Libs" script: ask for a noun, verb, adjective, and adverb, then print a funny sentence using all four.