Saltar a contenido

01 - Setup

What this session is

About 30 minutes. By the end you'll have Python installed, the terminal open, your first program running, and a virtual environment - a small thing that prevents a large category of future pain.

Step 1: Install Python

Most operating systems already have Python somewhere - but often the wrong version, or one used by the system that you shouldn't disturb. The safe move is to install a fresh recent Python yourself.

  • macOS: download the installer from python.org/downloads. Double-click; follow the prompts.
  • Windows: download the installer from python.org. Important: during install, check the box that says "Add python.exe to PATH." Without it, the terminal won't find Python.
  • Linux: your distro has Python in its package manager. sudo apt install python3 python3-pip python3-venv on Debian/Ubuntu; sudo dnf install python3 python3-pip on Fedora.

Once it's done, open a terminal:

  • macOS: press ⌘ Space, type "terminal", hit enter.
  • Windows: press Windows, type "powershell", hit enter.
  • Linux: you know how.

Check:

python3 --version

You should see something like:

Python 3.13.0

The minor version will differ. As long as it's 3.10 or higher, you're fine. (On some systems the command is just python, not python3. Try both.)

If you get command not found, the install didn't work. On Windows, make sure you ticked the PATH box; you may need to reinstall. On macOS, try opening a new terminal window after install.

Step 2: Pick a folder for your code

You're going to write a lot of small programs. They need somewhere to live.

mkdir -p ~/code/python-learning
cd ~/code/python-learning

(~ is your home folder. mkdir -p creates the folder and any missing parents; cd enters it.)

Check you're there:

pwd

You should see the folder's full path.

Step 3: Create a virtual environment

This is the step that surprises beginners and prevents months of "why is my Python broken" pain.

Python projects often need third-party libraries. If you install them globally (system-wide), eventually two projects need different versions of the same library and the whole thing collapses. The solution: a virtual environment - a private, project-local Python install where you can put libraries without affecting anything else.

Create one:

python3 -m venv .venv

That creates a folder called .venv (yes, the dot is intentional - it's a hidden folder by convention). Inside it is a private Python installation.

Activate it so your terminal uses it:

source .venv/bin/activate          # macOS, Linux
.venv\Scripts\activate             # Windows

Your terminal prompt should now have (.venv) at the front. That's how you know you're inside the virtual environment.

From now on: - Always activate the venv before working (source .venv/bin/activate). - When you're done, type deactivate to leave (rare; mostly you stay in). - Each project gets its own .venv. Don't share.

If you forget to activate, your python commands will use the system Python - and pip install will try to install globally, sometimes failing, sometimes succeeding-and-causing-problems-later.

Step 4: The smallest possible Python program

Open your text editor (VS Code, or whatever you chose). Create a new file. Save it as hello.py inside the python-learning folder.

Type this - type it, don't copy-paste:

print("hello, world")

That's the whole file. One line.

Save it.

Step 5: Run it

Back in your terminal, with the venv active:

python hello.py

You should see:

hello, world

That's your first program. Take a moment.

What just happened

You typed one line. print(...) is a built-in function that takes whatever you give it and prints it followed by a newline. "hello, world" is text in quotes - a string.

In a language like Go, the simplest program is ten lines of scaffolding (package, import, func main, braces). In Python it's one. Python trades structure for brevity. You'll feel both sides over the next pages.

Try changing things

The way to learn is to break things on purpose and see what happens. Try each:

  1. Change "hello, world" to your name. Run again.

  2. Add a second line that prints something else:

    print("hello, world")
    print("this is my second line")
    

  3. Try printing a number - no quotes:

    print(42)
    

  4. Now break it on purpose. Remove the closing ) and run. Read the error Python gives you. (Don't worry about understanding all of it - just notice that Python tells you which line.)

  5. Put the ) back. Now mistype print as Print (capital P). Run. Read the error. Python is case-sensitive: print and Print are different names.

Reading errors is most of programming. Get comfortable seeing them.

The REPL - instant gratification

Python has a thing Go doesn't: a REPL (Read-Eval-Print Loop). Type lines, get results instantly. Try it:

python

You'll see a >>> prompt. Type:

>>> 2 + 2
4
>>> "hello" + " " + "world"
'hello world'
>>> print("hi")
hi

Press Ctrl-D (macOS/Linux) or Ctrl-Z then Enter (Windows) to exit.

The REPL is great for testing one-line ideas. "How does X behave?" → open REPL → try → see. Use it constantly.

What you might wonder

"Why do I need a virtual environment for a one-line program?" You don't - yet. The habit pays off the first time you install a third-party library (page 11). Get used to seeing (.venv) in your prompt now, and you'll never get bitten by the cross-project version-conflict bug.

"Do I need to compile?" No. Python reads and runs your file line by line. There's no separate compile step. The trade: Python is slower than compiled languages (like Go or Rust) at runtime, but faster at "edit, run, see result."

"Are tabs or spaces required for the indentation?" You haven't seen indentation matter yet - but in Python, indentation defines the structure of your code. Most editors insert 4 spaces when you press Tab. Stick to that. The first time you mix tabs and spaces, Python will complain and you'll lose 30 minutes finding the issue.

"My terminal says python3 but examples use python. Which one?" Inside an activated venv, python is your venv's Python - use it. Outside the venv, python3 is safer (some systems alias python to Python 2, which you don't want).

Done

You have: - Python installed. - A folder to put your code in. - A virtual environment (activate it before any session). - One working program. - The REPL as a scratchpad.

This was the boring infrastructure step. Next page is where the real learning starts.

Next: First real program →

Comments