Introduction
You now have Python and an editor/IDE ready from the previous lessons. In this lesson, you will actually run Python code—both interactively and from a script file. You’ll:
- Use the interactive Python interpreter as a quick calculator and testing tool.
- Write, save, and run your first Python script from a terminal.
- Learn how to read and interpret the program output.
- Diagnose and fix some very common first‑run errors.
By the end, you’ll have a complete end‑to‑end workflow: type code → run it → see results → fix issues.
Using the Python Interactive Interpreter (REPL)
The Python interpreter lets you run Python statements interactively, one at a time. This is great for experimentation.
Starting the Interpreter
In your terminal, run:
python
or:
python3
You should see something like:
Python 3.11.6 (main, ...)
[GCC ...] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
The >>> prompt means Python is ready for commands.
Running Simple Commands Interactively
Try a few expressions:
>>> 2 + 3
>>> print("Hello from the interpreter!")
>>> 10 * 5
Expected output:
>>> 2 + 3
5
>>> print("Hello from the interpreter!")
Hello from the interpreter!
>>> 10 * 5
50
Notice:
- Expressions like
2 + 3show their result directly. print()displays text to the screen.
Exiting the Interpreter
To exit:
- On Windows: press
Ctrl + Z, thenEnter - On macOS/Linux: press
Ctrl + D
Alternatively, type:
>>> exit()
Writing Your First Python Script
Interactive mode is great for quick tests, but real programs are saved in files (scripts).
You can use any editor or IDE you configured earlier. The steps are similar regardless of tool.
Step 1: Create a New File
Create a new file named hello.py in a folder you can easily find, for example:
- Windows:
C:\Users\YourName\python-projects\hello.py - macOS/Linux:
/Users/YourName/python-projects/hello.pyor/home/YourName/python-projects/hello.py
Step 2: Add Python Code
In your editor/IDE, add this code:
# hello.py
print("Hello, Python world!")
print("This is your first Python script.")
Explanation and Output
This script:
- Uses
print()twice to display two lines of text. - Does not require any special imports.
Expected output when run:
# output
Hello, Python world!
This is your first Python script.
Save the file before running it.
Running a Python Script from the Terminal
Now you’ll run hello.py from the command line.
Step 1: Navigate to the Script Folder
In your terminal, change directory to where you saved hello.py.
Example (adjust path as needed):
cd C:\Users\YourName\python-projects # Windows
or
cd /Users/YourName/python-projects # macOS
cd /home/YourName/python-projects # Linux
Step 2: Run the Script
Run one of the following commands (whichever matches your installation):
python hello.py
or
python3 hello.py
You should see:
# output
Hello, Python world!
This is your first Python script.
If you see that output, congratulations—you’ve successfully run your first Python program from a file.
A Slightly More Interesting First Program
Let’s create a small program that does a calculation and prints a formatted result.
Edit hello.py or create a new file greeting.py:
# greeting.py
name = "Alice"
age = 25
# Use f-string formatting to combine variables with text
print(f"Hello, {name}!")
print(f"You are {age} years old.")
print(f"Next year, you will be {age + 1}.")
Explanation and Output
nameandageare variables storing a string and a number.f"..."creates an f-string, allowing you to embed{name}and{age}inside the text.age + 1performs a numeric calculation before printing.
Expected output:
# output
Hello, Alice!
You are 25 years old.
Next year, you will be 26.
Run it the same way:
python greeting.py
Debugging Common First‑Run Errors
New Python users often hit a few predictable issues. Learning to recognize them early will save time.
1. Syntax Errors
A syntax error means Python can’t understand your code structure.
Example mistake:
print("Hello world!" # missing closing parenthesis
When you run this, you might see:
File "hello.py", line 1
print("Hello world!" # missing closing parenthesis
^
SyntaxError: '(' was never closed
How to fix:
- Carefully read the error message: it shows the file, line, and a description.
- Correct the code (add the missing
)), then run again.
2. Indentation Errors
Python is sensitive to indentation (spaces at the start of a line).
Example mistake:
print("First line")
print("Second line") # extra spaces at the beginning
Possible error:
IndentationError: unexpected indent
Fix:
- Make sure all top-level lines start at the same column.
- Use consistent indentation (typically 4 spaces) inside code blocks when you learn flow control.
3. File Not Found / Wrong Directory
If you run:
python hello.py
and see:
python: can't open file 'hello.py': [Errno 2] No such file or directory
It means Python can’t find hello.py in the current directory.
Fixes:
- Check that you’re in the correct folder:
pwd(macOS/Linux) orcd(Windows) to see the path. - Use
dir(Windows) orls(macOS/Linux) to list files. - If needed, provide the full path:
python C:\Users\YourName\python-projects\hello.py
4. Using the Wrong Python Command
If python runs Python 2 or fails, but python3 works, always use:
python3 script_name.py
You can confirm which command maps to Python 3 with:
python --version
python3 --version
Summary / Key Takeaways
In this lesson Running Your First Python Program, you have learned:
- How to verify Python installation by checking the version from the command line and ensuring it points to Python 3.
- How to use the interactive interpreter to execute simple expressions and
print()statements for quick experimentation. - How to create a Python script file (
.py), write basicprint()and variable statements, and save it in a known directory. - How to run Python scripts from the terminal using
pythonorpython3, and how to navigate to the correct folder first. - How to interpret program output by matching printed lines with the corresponding lines in your code.
- How to recognize and fix syntax errors, including missing parentheses and inconsistent indentation.
- How to diagnose path-related issues, such as file-not-found errors caused by running Python in the wrong directory.
- How to distinguish between command variants (
pythonvspython3) and choose the correct one on your system.
