The interpreter, interactive mode, script mode, code editors, the terminal, file paths, and your first complete Python program
Before you write a single meaningful Python program, you need to know how Python works as a piece of software. You need to know what the Python interpreter is, how to install it, how to give it instructions interactively, how to save a complete program as a file, how to run that file, and how to make sense of what happens when something goes wrong. These are not exciting ideas — but they are the tools that make every idea in this book reachable.
This chapter is practical. Its goal is a narrow but critical one: by the time you reach Chapter 3, you should be able to open Python, type expressions and see results, save a script file, run it, read its output, and fix the most common setup problems on your own. Those four skills — open, type, save, run — unlock everything that follows.
Before you can write and run Python programs, you need to know how Python works as a tool. This chapter explains what Python is as a piece of software, how to install it, how to use the interactive shell, how to write and save script files, how to use a code editor, how to work with terminals at a basic level, how to understand file paths well enough to run your programs, and how to troubleshoot the most common setup problems beginners face.
By the end of this chapter you should be able to open a Python interactive session, type a few expressions, save a small Python script, run it, and understand what happened.
By the end of this chapter, you should be able to:
print(), input(), a variable, and a comment.>>> prompt; typing expressions one line at a time; saving programs as .py files; when to use each modecd, ls, dir, pwd; running Python from the terminal; overcoming fear of the command lineprint(), input(), a variable, and a comment; line-by-line reading; what each line does; experiments to tryPython is a programming language, but it is also a piece of software. When you write a Python program, you write plain text in a file. That text is your source code. Python does not magically understand your code on its own. A program called the Python interpreter reads your source code and carries out your instructions.
The interpreter is the bridge between what you write and what the computer does. Without it, a Python file is just text. With it, that text becomes a running program.
Python has a long history. The version that matters for this textbook is Python 3. Python 3 is the current, supported, actively developed version of the language. An older version, Python 2, existed for many years and is now retired. Python 2 and Python 3 are not fully compatible — some syntax and behaviors differ between them in ways that matter.
This textbook uses Python 3 throughout. All examples, exercises, and projects are written for Python 3.
When you download Python, you will see a specific version number such as 3.11.4 or 3.12.0. The first digit is the major version (3). The second digit is the minor version. Minor versions add features and fix bugs. For the topics in this textbook, any Python 3.8 or later version works well. If you already have Python 3 installed, check whether it is reasonably recent before installing a new version.
When you run a Python program, the interpreter goes through your code, translates it into instructions the computer can execute, and runs those instructions. This all happens quickly and automatically — you do not usually see the translation step. You see the results.
The interpreter can work in two different modes: interactive mode and script mode. You will use both. The next section explains the difference.
To check which Python version is installed, open a terminal and type python --version or python3 --version. Python will print something like Python 3.11.4. If you see Python 2.x.x, you need to install Python 3 separately.
In interactive mode, the Python interpreter waits for you to type one line of code at a time, runs it immediately, and shows you the result. This is sometimes called the Python shell or the REPL, which stands for Read, Evaluate, Print, Loop.
When you open interactive mode, you see a prompt that looks like this:
>>>
The three arrows are called the primary prompt. They mean Python is waiting for your input. You type a line of Python code, press Enter, and Python immediately evaluates it and prints the result if there is one.
>>> 2 + 3 — You type an arithmetic expression.5 — Python evaluates it and prints the result immediately.>>> print("Hello") — You call a function.Hello — The function runs and displays its output.>>> name = "Alex" — You store a value in a variable. No output — assignment is silent.>>> name — You type just the variable name.'Alex' — Interactive mode shows you the value. This only works in interactive mode; in a script you would need print().Interactive mode is useful for trying a short expression to see what it produces, testing whether a function works the way you expect, exploring how a data type behaves, or quickly calculating something. It is not a good place to write a full program, because once you close the session, everything you typed is gone.
In script mode, you write your Python code in a plain text file, save the file, and then tell Python to run the entire file. This is how real programs are written and run.
A Python script file has a name that ends in .py. For example: hello.py, calculator.py, grade_checker.py. The .py extension tells operating systems and editors that the file contains Python code.
When you run a script file, Python reads through the entire file from top to bottom and executes all the instructions. Nothing is printed automatically — you have to use print() to display output. Script mode is what you will use for every real program in this textbook.
| Feature | Interactive Mode | Script Mode |
|---|---|---|
| How you use it | Type one line at a time | Write a file, then run it |
| Results | Shown immediately after each line | Shown only when print() is called |
| Saved? | No — lost when you close the session | Yes — saved in a .py file |
| Good for | Exploring, experimenting, testing small ideas | Writing complete programs |
In interactive mode, typing a variable name displays its value automatically. In a script file, it does not — you must use print(variable_name). Many beginners write scripts and wonder why nothing appears. The answer is almost always a missing print().
You write Python scripts in a text editor or a development environment. A plain text editor is enough. A development environment adds extra tools that help you write, run, and debug code more efficiently.
Word processors like Microsoft Word save files with hidden formatting codes. Python needs plain text with no formatting. Always use a tool designed for code, not a word processor. Even a single invisible formatting character can cause Python to fail.
If you are unsure, IDLE is the safest starting point — it is already installed with Python. Thonny is excellent if you want a beginner-friendly experience with visual debugging. VS Code is the right choice if you want to build habits with a tool you will likely use long-term. Any of these will work for everything in this textbook.
For this course, choose a code editor that saves files as plain text, shows Python syntax with color highlighting (called syntax highlighting), uses a monospace font where every character takes the same width, lets you open and save .py files easily, and has a way to run your Python file directly or open a terminal.
A terminal is a text-based window where you type commands and the computer responds with text. Terminals go by different names depending on the operating system: on Windows, the options include Command Prompt, PowerShell, and Windows Terminal; on macOS, the application is called Terminal (found in Applications → Utilities); on Linux, it may be called Terminal, Console, or Bash.
The terminal is where you will often run Python scripts. You do not need to become a terminal expert right now. You need to know enough to navigate to a folder and run a Python file.
| Action | Windows | macOS / Linux |
|---|---|---|
| Print current folder | cd (with no argument) | pwd |
| List files in current folder | dir | ls |
| Move into a folder | cd foldername | cd foldername |
| Move up one folder level | cd .. | cd .. |
| Check Python version | python --version | python3 --version |
| Run a Python script | python myscript.py | python3 myscript.py |
/Users/alexcd Documents/python_projects/chapter_02 and press Enter. You are now inside the chapter_02 folder.ls to list files. You see hello.py in the output, confirming your file is here.python3 hello.py and press Enter. Python runs your script and its output appears immediately below.Many beginners feel uncomfortable with the terminal at first. This is normal. Terminals feel unforgiving because there are no buttons to click. But the terminal will not damage anything if you type an unknown command — it will usually just say the command was not found. Approach terminal practice the same way you approach Python practice: try, observe, adjust, and try again.
Your computer stores files inside folders. Folders can be inside other folders. This creates a file system tree — a nested structure of folders and files. Understanding file paths matters in Python because you need to know where to save your script files, you need to navigate to the right folder before running a script, and later in this course you will write programs that read from and write to files, which require accurate file paths.
A path is the address of a file or folder on your computer. It describes where to find something by listing the folders you pass through to reach it.
C:\Users\Alex\Documents\python_projects\hello.py/Users/alex/Documents/python_projects/hello.pyhello.py inside python_projects, which is inside Documents, which is inside the user's home folder. Windows uses backslash \ as a separator; macOS and Linux use forward slash /.| Path Type | Description | Example (macOS/Linux) |
|---|---|---|
| Absolute path | Gives the full address starting from the root of the file system. Always unambiguous. | /Users/alex/python_projects/hello.py |
| Relative path | Gives the address starting from the current working directory. Shorter and easier to read, but depends on where you are. | hello.py (if you are already in python_projects) |
From the beginning of this course, create a clear folder structure for your work. Give your folders and files meaningful names. Use lowercase letters. Use underscores instead of spaces — spaces in folder names and file names can cause problems in the terminal.
python_projects/ chapter_02/ hello.py first_program.py chapter_03/ variables.py expressions.py chapter_04/ calculator.py converter.py
You create a Python file by opening your code editor, writing Python code, and saving the file with a .py extension. The steps differ slightly between editors, but the result is the same: a plain text file containing your Python source code.
chapter_02 folder. Type a file name ending in .py, for example hello.py. Click Save.python_projects folder..py. Press Enter. The new file opens.| File Name | Assessment | Reason |
|---|---|---|
temperature_converter.py | ✓ Good | Lowercase, underscores, descriptive, ends in .py |
grade_checker.py | ✓ Good | Clear and readable |
my calculator.py | ✗ Avoid | Space in name causes terminal problems |
hw3.py | ✗ Avoid | Not descriptive — unclear months later |
QuizProgram.py | ✗ Avoid | Mixed case is allowed but hard to type consistently |
script.PY | ✗ Avoid | Uppercase extension; some systems may not recognize it |
There are two main ways to run a Python program: from within your code editor, or from the terminal. Both ultimately do the same thing: they give your script file to the Python interpreter and display the output.
In IDLE, open your .py file and press F5 or go to Run → Run Module. IDLE runs your program and shows output in the interactive shell window.
In Thonny, click the green play arrow or press F5. Output appears in the shell panel at the bottom.
In VS Code, click the play button in the top-right corner of the editor, or right-click inside the file and choose Run Python File in Terminal. VS Code opens a terminal panel and runs your script there.
This is the most general method. Open your terminal, navigate to the folder where your Python file is saved, and run the file:
$ cd Documents/python_projects/chapter_02 $ python3 hello.py Hello, world! This is my first Python script.
The $ represents the terminal prompt — you do not type it. Everything after $ is what you type. The lines without $ are the output your program produced.
python3 hello.pyhello.py from disk.print(), the output appears in the terminal.On Windows, double-clicking a .py file opens a terminal window that closes as soon as the program finishes — often too fast to read. Always run scripts from inside your editor or from the terminal you already have open.
Good file organization becomes more important as your programs grow. Forming good habits early saves significant frustration later.
This sounds obvious, but beginners often forget to save after making changes. If you run an old saved version of a file, you will be confused when your latest edits do not appear in the output. Most code editors show a dot or asterisk in the file tab when there are unsaved changes. Watch for this indicator. In most editors, Ctrl+S (Windows) or Cmd+S (macOS) saves quickly.
Your file name should give a clear idea of what the program does. Three months from now, grade_calculator.py will be much easier to find and understand than program3.py. Name files for what they do, not for when you wrote them.
Each Python program should be its own file. Avoid putting two unrelated programs in the same file. This keeps things clear and makes running individual programs easy.
Use folders organized by chapter or topic. For larger projects, give each project its own folder alongside its data files. Back up your work regularly — to a cloud service, a USB drive, or any second location. Programming coursework is difficult to recreate from memory.
python_projects/ chapter_02/ hello.py first_program.py quiz_program/ quiz.py questions.txt gradebook/ gradebook.py data.csv
Setup problems are frustrating but very common. Nearly every programmer has spent time early on fighting installation and configuration issues. The problems below are the most frequently encountered ones, with step-by-step approaches for each.
python3 instead of python.python, but Python 3 is python3. Install Python 3 from python.org and use python3.python3 explicitly instead of python.python3 --version.pwd (macOS/Linux) or cd with no arguments (Windows).cd..py extension and capitalization.ls (macOS/Linux) or dir (Windows) to confirm your file is in the current folder.print() around everything you want to display. In a script, values do not print automatically the way they do in interactive mode.print() calls are not inside an if block or loop that is never reached. (You will learn about these in later chapters.)When Python encounters an error, it prints a message that almost always includes: (1) the file name where the error occurred, (2) the line number, and (3) a description of the problem. Read the error message before doing anything else. It is Python's attempt to help you find the problem. Even if the message is confusing at first, the line number alone will often point you directly to the issue.
Now you have enough background to write and run your first complete Python script. Work through this example carefully — read each line, predict what it does, then run it and verify your prediction. This habit of predicting before running is one of the most valuable you can build.
The program below prints a short greeting, asks the user for their name, and then prints a personalized message. It uses two Python tools: print() to display output, and input() to receive text from the user. You will study both in depth in Chapter 4. For now, treat them as reliable tools you do not need to fully understand yet.
Create a new file called first_program.py in your chapter_02 folder. Type this code exactly:
# My first Python program print("Welcome to Python!") print("Let's get to know each other.") name = input("What is your name? ") print("Hello,", name) print("Nice to meet you.")
Welcome to Python!Let's get to know each other.What is your name? — the program pauses and waits here.Alex then presses Enter.Hello, AlexNice to meet you.# My first…This is a comment. The # symbol tells Python to ignore everything on this line. Comments are notes for human readers — Python skips them entirely.print(…)Calls the print() function with a string (a piece of text) inside the parentheses. Python displays the text in the terminal, followed by a new line.name = input(…)Calls the input() function. The text inside the parentheses is displayed as a prompt. The program then waits for the user to type something and press Enter. Whatever they type is stored in the variable named name.print("Hello,", name)Prints the word Hello, followed by the value stored in name. The comma inside print() places a space between the two pieces of output.1. Sequential execution. Programs run from top to bottom, one line at a time.
2. Output. print() sends text to the terminal.
3. Input. input() pauses and waits for the user to type something.
4. Variables. Variables store values that the program can use later. Here, name holds whatever the user typed.
5. Comments. Lines beginning with # are notes for humans. Python ignores them completely.
After your program runs correctly, try these modifications one at a time. Run the program after each change to see what happens.
print() line after the greeting with a message of your own.input() to ask for something different.print("Goodbye,", name)# from the first comment line. Run the program. What happens? Why?.py file and runs the whole file; output is shown only when print() is called. Interactive mode is for exploration; script mode is for writing real programs.cd to navigate folders, ls or dir to list files, and python3 script.py to run a Python program. The terminal will not damage anything if you type an unknown command.\ as a separator; macOS and Linux use /. Organize files into clearly named folders from the beginning..py extension. Use lowercase names with underscores and no spaces. The file name should describe what the program does.python3 filename.py. Python reads the file from top to bottom, executes each instruction, and stops when it reaches the last line or encounters an error. Save the file before every run.python3 explicitly); file not found (navigate to the correct folder first); no output (add print()); IndentationError (use four spaces consistently); SyntaxError (read the line number and check for missing characters).print(), input with input(), storing values in variables, and adding comments with #. These ideas run through every program in this textbook.# that Python ignores entirely. Used to leave explanatory notes for human readers.>>> symbol in Python's interactive shell, indicating the interpreter is waiting for input..py file. Run by giving the file to the Python interpreter..py file from top to bottom. Output is shown only where print() is called..py files as plain text.Work through this checklist before moving to Chapter 3. Every item here is a foundation for every chapter that follows.
python3 --version in the terminal and saw a Python 3.x version number.2 + 2 at the >>> prompt and saw 4."hello" and saw it echoed back.python_projects folder with a chapter_02 subfolder.first_program.py in my chapter_02 folder.first_program.py from inside my editor and saw the expected output.chapter_02 folder using cd.first_program.py from the terminal using python3 first_program.py.No programming required. These exercises build comfort with file paths and terminal navigation before you need them inside Python programs.
Use this folder structure for Exercises 1 and 2:
home/ alex/ python_projects/ chapter_02/ hello.py calculator.py chapter_03/ variables.py
hello.py? (Use macOS/Linux format with / separators.)
chapter_02, what is the relative path to hello.py?
python_projects, what is the relative path to variables.py?
chapter_03, what is the relative path to hello.py?
cd commands to navigate from home/alex to chapter_02.python_projects folder.
ls (macOS/Linux) or dir (Windows)
cd chapter_02, then ls
cd .. (what folder are you in now?)
pwd after cd chapter_02my calculator.py
temperature_converter.py
QuizProgram.py
hw3.py
grade_report_generator.py
script.PY>>> 10 + 5 >>> 100 - 37 >>> 4 * 8 >>> 15 / 4 >>> 15 // 4 >>> 15 % 4 >>> "hello" >>> "hello" + " " + "world" >>> type(42) >>> type("hello")
three_things.py.144 / 12 = 12.0. The three calculations: 144 divided by 12; 17 multiplied by 6; 1000 minus 237. Save it as math_output.py.first_program.py, write a new program called greeting.py that asks for the user's name, then asks for the user's favorite color, then prints a message using both values.For each scenario, explain what likely went wrong and what steps you would take to fix it.
python hello.py in the terminal and see:
python: can't open file 'hello.py': [Errno 2] No such file or directory
python --version and see Python 2.7.18. You want to use Python 3..py file on Windows. A black window flashes briefly and disappears before you can read anything.File "hello.py", line 3 print("Hello) ^ SyntaxError: EOL while scanning string literal
Answer each question in complete sentences.
# symbol do in a Python program? What would happen if you accidentally removed it from the beginning of a comment line?print() do? How is its behavior in a script file different from how values appear in the interactive shell?.py extension in a file name indicate? What would happen if you saved a Python file as hello.txt?cd command do in the terminal? What does cd .. do?python3 hello.py in the terminal, describe what happens step by step from the moment you press Enter until the terminal prompt returns.Chapter 2 gave you the tools to write and run Python programs. Chapter 3 teaches you what to put inside them. You will learn about the smallest building blocks of Python programs: values, variables, and expressions. You will see how Python stores and works with numbers and text, how to name variables clearly, how arithmetic operators work, and how to trace the way expressions are evaluated step by step. By the end of Chapter 3, you will be writing short programs that calculate results, store them in named variables, and display them using print(). The programs are still small — but the ideas behind them run through every program you will ever write.