Module 1 ยท Lesson 3

The Python REPL and Your First Script

๐Ÿ Pythonโฑ 10 min read๐Ÿ“– Hands-On

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") 6

Math 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 โ†’ 125

Variables 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

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

ErrorCauseFix
IndentationErrorWrong indentation (spaces vs tabs)Use 4 spaces consistently
NameError: name 'x' is not definedVariable used before assignmentDefine the variable first
TypeError: can only concatenate str (not "int") to strMixing strings and numbersUse f-strings or str()
SyntaxErrorMissing colon, bracket, or quoteRead the error line number

Key Takeaways

Practice Exercises

  1. In the REPL, calculate: what is 2 to the power of 32?
  2. Create a script that asks for two numbers and prints their sum, difference, product, and quotient.
  3. Modify the script to also handle division by zero gracefully (just print a message if the user enters 0).
  4. Create a "Mad Libs" script: ask for a noun, verb, adjective, and adverb, then print a funny sentence using all four.
โ† Installing Python