Work & Development · Python & Automation
Free preview
Run and Explain Your First Python Program
Lesson 1 of Programming Foundations with Python. Anyone can read this lesson with no account. Every lesson after this one is for students.
Run your first programs
Open a terminal, run a small Python file and explain the output one line at a time.
By the end of this lesson you will be able to open a terminal, move into a project folder, run a Python file, and explain each line it printed.
1Start with a program you can understand
A Python file is plain text containing instructions. Saving it stores the instructions; it does not run them. Python normally follows the instructions from top to bottom. You use three tools:
- The editor is where you type and save the code.
- The terminal is a text window where you type commands.
- The interpreter is the Python program that reads and runs the code.
Think of writing a recipe and then asking someone to follow it. Saving the recipe is not the same as cooking.
2Install Python
Install Python 3.11 or newer from python.org. On Windows, tick "Add python.exe to PATH" on the first installer screen. You will check the installation from the terminal in a moment.
3Terminal basics
Open a terminal. On Windows, press the Windows key, type Terminal (or PowerShell on older versions) and press Enter. On a Mac, press Command and Space, type Terminal and press Enter. On Linux, look for Terminal in your applications menu.
See where you are. The terminal always works inside one folder, called the current folder. Type pwd and press Enter on Mac, Linux or Windows PowerShell; it prints the full path of the current folder.
List what is here. Type ls (Mac, Linux, PowerShell) to list the files and folders inside the current folder.
Move into a folder. cd Documents moves into a folder called Documents. cd .. moves up one level. If a folder name contains spaces, put it in quotes: cd "My Projects".
Make a folder. mkdir python-study-practice creates a new folder in the current one.
Try them now:
cd Documents
mkdir python-study-practice
cd python-study-practice
pwd
The last line should print a path ending in python-study-practice. If cd Documents fails, run ls to see which folders exist and pick one.
Check Python. In the same terminal, type python3 --version on Mac or Linux, or py -3 --version on Windows. You should see a version such as Python 3.13.1. If you see "command not found," reinstall Python and, on Windows, tick the PATH box.
4Create a project environment
A virtual environment is a project folder with its own Python entry point and package location. It keeps one project's installed packages apart from another's. This course uses only the standard library, the tools included with Python, so you will not install extra packages. Create the environment now to practice keeping projects separate.
On Mac or Linux, inside python-study-practice, run:
python3 -m venv .venv
On Windows, run py -3 -m venv .venv. Run ls again: you should now see a .venv folder (you may need ls -a on Mac or Linux, because names starting with a dot are hidden). Never put your own code inside .venv.
5Worked example
Open your editor, create a new file in python-study-practice, and save it as hello.py:
student = "Casey"
goal = "organize a fictional study log"
print("Student:", student)
print("Goal:", goal)
print("First practice session:", 20, "minutes")
Back in the terminal, run it with the environment's interpreter. On Mac or Linux: .venv/bin/python hello.py. On Windows: .venv\Scripts\python.exe hello.py.
Expected output:
Student: Casey
Goal: organize a fictional study log
First practice session: 20 minutes
student and goal are variable names. Each refers to a string, a text value. Quotes mark a string's beginning and end in the code. print displays the text without those quotes. Several values passed to print are separated by spaces in the output.
20 has no quotes, so it is a number. Compare the three output lines with the three print instructions. Casey is invented; use no personal details for this practice.
6Your practice and debugging challenge
- Change the goal and add a second session line. Predict the exact output before running it.
- In a copy, remove one closing quote. Read the
SyntaxErrorand its line number, then put the quote back. - Set
session_minutes = 25and print it in a sentence.
If the file is not found, use pwd and ls to check the folder and filename. It should be hello.py, not hello.py.txt. On Windows, use the environment's backslash path shown above.
7Self-check with answer guidance
Question: Why does saving hello.py not run it?
Show answer
Answer: Saving only updates the text. The interpreter runs it when you type the run command.
Question: What is the difference between "20" and 20?
Show answer
Answer: The first is a string of two characters. The second is an integer. They print the same, but only the integer can be added to other numbers.
Question: What belongs inside .venv?
Show answer
Answer: Only files Python generates for the environment. Keep your own code outside it.
8Project milestone
Keep hello.py outside .venv. If you use Git, add .venv to .gitignore. Write down the exact run command that works on your computer; you will use it in every lesson.
Next, you will turn text a person types into numbers your program can add.
Continue with the full course
This free preview is lesson 1 only. Enroll to open the rest of Programming Foundations with Python.