Environment & First Programs
Lesson 1 of 8
1Environment & First Programs
The big pictureHow the REPL differs from a script file, and why every project gets its own virtual environment instead of polluting the system Python.
What the picture shows: A line of code in a file, read and run by the Python interpreter, and the result 5 appears on the screen.
Map of this part
The concepts in order, click any one to jump to it:
Two ways into the same idea
Python is like an interpreter standing between you and the computer. You can chat with it line by line and it answers at once, or write a whole letter in a file and it carries it out in one go. A virtual environment is a toolbox per project, so one project's tools never mix with another's.
The python interpreter runs interactively as a REPL or runs a script with python app.py. A variable is a name bound to a value, print prints, and f"..." puts values into text. python -m venv .venv creates an isolated environment where pip installs packages without touching the system Python.
Before You Start
🧭The toolkit track
You know the terminal: type a command and press Enter.
📄The idea of a file
Code is an ordinary text file whose name ends in .py.
The Core Idea
If this is your first Python, start here. Three small steps, each with code you run yourself, before you read about environments and files.
1Print something
print means "show on screen". Whatever sits between the brackets is shown. Text goes between quotes; a number is written as it is.
print("Hello, Darb")
print(2 + 3)
print("2 + 3")
Notice the last two lines: 2 + 3 without quotes was computed into 5, while with quotes it stayed text exactly as typed.
2Keep a value under a name
A variable is a name you attach to a value so you can use it later. Write the name, then =, then the value. Every value has a type: text, a whole number, or a decimal.
name = "Sara" age = 21 height = 1.68 print(name) print(age + 1) print(type(name), type(age), type(height))
type tells you a value's type. str is text, int a whole number, float a number with a decimal point. And age + 1 was computed because age is a number.
3Mix text and values
To show a sentence containing a variable, put the letter f before the opening quote and the variable name inside curly braces. That is an f-string, and you will use it in nearly every program.
name = "Sara"
age = 21
print(f"{name} is {age} years old")
print(f"In five years: {age + 5}")
print("Letters in the name:", len(name))
Inside the braces you can compute too, as in {age + 5}. And len gives the length of a text in characters.
print(nam) instead of print(name). Python answers with NameError, which literally means: I know no name spelled like that. Read the message; it names the thing you mistyped.How It Works, Step by Step: What happens when you type python hello.py
The shell finds the python program, runs it and hands it your file name.
The interpreter reads the file as text and checks the writing is valid.
It runs the lines from top to bottom, one after another.
After the last line the program ends and the terminal is yours again.
Going deeper
Before writing any program logic, you need to understand where and when code executes. Python supports two execution modes, each with a different use.
Interactive Mode (REPL)
You type a line and press Enter, and it executes immediately, automatically printing its result if it's an Expression. Ideal for quick experimentation, but nothing is saved after the session closes.
Script Execution
Code executes from the first line to the last in one pass. Nothing prints automatically, you must use print() explicitly. This is the shareable, repeatable mode.
pip and Extensibility
pip is the package manager, it installs tools like pycodestyle with one command. But it installs the package into whichever environment is currently active, global or isolated.
Virtual Environments (venv)
A folder containing an independent copy of the interpreter and its own libraries. Activating it temporarily changes which python3 your system points to, this prevents library version conflicts between different projects.
| Comparison | REPL | Script |
|---|---|---|
| Auto-printing | Yes, for any expression with a value | No, needs print() |
| Persistence | Not saved | A permanent, repeatably-runnable file |
| Typical use | Experimentation and quick checks | Real programs and automation |
Example: Why Isolate Environments?
# Without isolation: installing a new library version might "break" an old project that depends on an older version of it pip install requests==2.31 # installed globally for all your projects at once # With venv: each project gets its own completely separate copy python3 -m venv project_a_env python3 -m venv project_b_env source project_a_env/bin/activate pip install requests==2.31 # this version is confined to project_a_env only deactivate source project_b_env/bin/activate pip install requests==2.13 # doesn't conflict with project_a_env at all
Check your understanding: if you activate environment B then run which python3, what would you expect?
A path pointing inside environment B's folder (containing B/bin/python3), not the system's global Python path, this is the practical proof that activation changes which interpreter is actually used.
Worked example
requests library without touching the machine's Python. The answer: python -m venv .venv, then source .venv/bin/activate, then pip install requests. Everything you installed lives in the project's .venv folder, and deleting it leaves no trace.Myth vs Reality
| Myth | Reality |
|---|---|
| Python turns the whole file into a program before running it, like C. | Python reads the file and runs it straight away through an interpreter, which is why there is no separate compile step. |
| pip install puts the library in all my projects, which is good. | Without a virtual environment every project shares one copy, so an update breaks another project. A virtual environment keeps each project apart. |
| The interactive prompt is enough for real work. | Great for a quick try, but what you type there is gone when you close it. A real program is a file you save and run again. |
Try it yourself
You have count = "7" and then write print(count + 1). What happens?
"7" is in quotes, so it is text, not a number, and Python refuses to add text to a number with a TypeError. To compute, convert first: int(count) + 1 gives 8. This difference between the two types is the first thing that trips beginners.
Quick Review Flashcards
Check Your Understanding
What is the difference between running python alone and python hello.py?
The first opens the interactive prompt line by line; the second runs the whole file and ends.
Why make a virtual environment per project?
So each project's libraries and versions stay separate and an update in one does not break another.
What does print("2 + 3") print?
It prints the text 2 + 3 as it is, because the quotes make it text, not a sum.
Resources for this part
Sorted by kind: start with the official docs when you want the precise reference, or with an article or video when you want the explanation first.
Resources for this lesson
Grouped by type. Start with the official docs for precision, or an article or video for the explanation.
Official docs2
Done with this lesson?
Sign in to save your progress.