Python REPL
Python REPL
Section titled “Python REPL”Introduction
Section titled “Introduction”REPL = Read → Evaluate → Print → Loop
It’s an interactive shell where you type Python code and see results immediately.
Starting the REPL
Section titled “Starting the REPL”python3 # Linux/macOSpython # WindowsYou’ll see:
Python 3.12.0 (main, ...)Type "help", "copyright", "credits" or "license" for more information.>>>REPL Examples
Section titled “REPL Examples”>>> 2 + 35>>> name = "Alice">>> f"Hello, {name}!"'Hello, Alice!'>>> [x**2 for x in range(5)][0, 1, 4, 9, 16]>>> import math>>> math.pi3.141592653589793Special Commands
Section titled “Special Commands”>>> help(str) # Get help for str class>>> dir(str) # List all methods of str>>> type(42) # Check type<class 'int'>>>> _ # Last result>>> quit() or exit() # Exit REPLIPython: Enhanced REPL
Section titled “IPython: Enhanced REPL”pip install ipythonipythonFeatures: tab completion, syntax highlighting, magic commands, better errors.
In [1]: %timeit [x**2 for x in range(1000)]# 78.8 µs ± 1.42 µs per loopWhy It Matters
Section titled “Why It Matters”The REPL is perfect for experimenting, testing snippets, learning, and debugging.
Practice Exercises
Section titled “Practice Exercises”- Use the REPL to calculate compound interest for a $10,000 investment at 7% for 10 years.
- Explore the
mathmodule usingdir(math)andhelp(math.sqrt). - Install IPython and use
%timeitto compare two different approaches to a problem.