# Your first program

> Understand what a program is, install Python and an editor, and write, run and change your first programs.

Learn to Code roadmap · Getting started · 15 min read · https://levelcode.org/roadmaps/learn-to-code/first-program

A program is a list of instructions that a computer follows exactly, in order. Computers are fast and tireless but completely literal: they do what you wrote, not what you meant. Learning to program is learning to write instructions precise enough for something that literal. This lesson explains how code becomes something a computer runs, sets up Python and a code editor, and has you write and run your first programs.

## What a programming language is

Processors only understand **machine code**: long sequences of numbers that mean things like "add these two values" or "jump to this instruction". Writing that by hand is miserable, so we write in **programming languages** designed for people, and let a program translate.

There are two main ways that translation happens:

- A **compiler** translates the whole program into machine code ahead of time, producing a file you can run. Go, Rust and C work this way.
- An **interpreter** reads your program and carries it out step by step as it runs. Python works this way (strictly, it first compiles to an intermediate form, but you never have to think about that).

This course uses **Python** because its syntax is short and readable, it gives helpful error messages, and it's used for real work everywhere: websites, data analysis, automation, scientific computing and AI. Everything you learn here (variables, conditions, loops, functions, data structures, debugging) carries over to every other language.

## Installing Python

You need Python 3.12 or newer.

- **macOS**: install from python.org (download the macOS installer) or, if you use Homebrew, `brew install python`.
- **Windows**: install from python.org. On the first installer screen, tick **"Add python.exe to PATH"**. Alternatively, install "Python 3.13" from the Microsoft Store.
- **Linux**: Python 3 is usually installed. Check the version below; if it's older than 3.12, install a newer one with your package manager (for example `sudo apt install python3.12` on Ubuntu).

Then check it worked, in a new terminal window:

```bash
python3 --version
```

```text
Python 3.13.1
```

On Windows, the command is usually `python` (or `py`) instead of `python3`. Use whichever prints a version, and substitute it everywhere this course writes `python3`.

## Talking to Python directly: the REPL

Type `python3` on its own and you enter Python's interactive mode, called the **REPL** (read, evaluate, print, loop). It reads a line, runs it, prints the result and waits for the next one:

```text
$ python3
>>> 2 + 3
5
>>> 7 * 6
42
>>> "hello" + " world"
'hello world'
>>> exit()
```

The `>>>` is Python's prompt, different from your shell's. The REPL is perfect for trying small things out. To leave, type `exit()` or press Ctrl+D (Ctrl+Z then Enter on Windows).

## Writing a program in a file

Real programs live in files so you can save, edit and rerun them. You need a **code editor**: a text editor that understands code, highlights it in colour and catches mistakes. Visual Studio Code (VS Code) is free and the most popular; install it and its Python extension. Word processors such as Word or Pages won't work, because they add formatting the computer can't read.

Create a folder for this course and a file inside it:

```bash
mkdir -p ~/code/learn
cd ~/code/learn
code hello.py      # opens the file in VS Code, if the 'code' command is installed
```

If `code` isn't found, open VS Code, choose *File → Open Folder* and pick `~/code/learn`, then create `hello.py`. The `.py` ending tells everyone, including the editor, that this is Python.

Type this into the file and save it:

```python
print("Hello! This is my first program.")
print("2 + 3 =", 2 + 3)
```

Run it from the terminal, in the same folder:

```bash
python3 hello.py
```

```text
Hello! This is my first program.
2 + 3 = 5
```

That's the whole cycle you'll repeat thousands of times: **edit, save, run, read the output**.

`print` is a **function**: a named piece of behaviour. You *call* it by writing its name followed by parentheses, and whatever you put inside the parentheses (the **arguments**) is what it works with. When you give `print` several arguments separated by commas, it prints them with spaces between.

## Text and numbers

Notice that `"2 + 3 ="` was printed exactly as written, but `2 + 3` was calculated. Anything in quotes is a **string**: a piece of text that Python treats as characters, not instructions. Without quotes, Python reads it as code.

```python
print("2 + 3")   # prints the text: 2 + 3
print(2 + 3)     # prints the result: 5
```

Python is a capable calculator:

```python
print(10 / 4)    # 2.5    division
print(10 // 4)   # 2      whole-number division
print(10 % 4)    # 2      remainder
print(2 ** 10)   # 1024   power
```

Lines starting with `#` are **comments**. Python ignores them; they're notes for the people reading your code, including you next month.

## Asking the user for input

Programs become interesting when they react to the person using them. `input` shows a message, waits for the user to type something and press Enter, and gives back what they typed:

```python
name = input("What's your name? ")
print("Nice to meet you,", name)
```

```text
What's your name? Sam
Nice to meet you, Sam
```

`name = ...` stores what `input` returned so you can use it later. That's a **variable**, and the next lesson is all about them.

## When it goes wrong

You'll make mistakes constantly; every programmer does. Try deleting the closing quote in `hello.py`:

```python
print("Hello! This is my first program.)
```

```text
  File "/Users/ana/code/learn/hello.py", line 1
    print("Hello! This is my first program.)
          ^
SyntaxError: unterminated string literal (detected at line 1)
```

Python tells you the file, the line, points at roughly where the problem is, and names it. A **syntax error** means the code isn't valid Python, so nothing runs at all. Read error messages from the bottom line up: the last line says what went wrong, and the lines above say where. A later lesson is dedicated to errors and debugging.

> [!TIP]
> Change one thing at a time and rerun. If you change five lines and something breaks, you don't know which change caused it. Small steps make every mistake easy to find.

## Practice

Write a program `about_me.py` that:

1. Asks for the user's name and the year they were born.
2. Prints a greeting using their name.
3. Prints how old they'll be at the end of 2030.

You'll need one thing this lesson hasn't covered: `input` always gives back text, even if the user types digits. To do maths with it, convert it to a whole number with `int(...)`, for example `int("1998")` gives the number `1998`.

Run it twice with different answers, then deliberately type `nineteen ninety` as the year and read the error message.

**Solution**

```python
# about_me.py
name = input("What's your name? ")
birth_year = int(input("What year were you born? "))

print("Hello,", name + "!")
print("At the end of 2030 you'll be", 2030 - birth_year, "years old.")
```

```text
$ python3 about_me.py
What's your name? Sam
What year were you born? 1998
Hello, Sam!
At the end of 2030 you'll be 32 years old.
```

Typing `nineteen ninety` gives:

```text
ValueError: invalid literal for int() with base 10: 'nineteen ninety'
```

`int` can only turn text that looks like a whole number into a number. This error happens while the program is running (unlike the syntax error, which stopped it before it started), and you'll learn to handle it gracefully in the errors lesson.

`name + "!"` joins two strings with no space between them, which is why the greeting reads "Sam!" rather than "Sam !".

## Key takeaways

- A program is a precise list of instructions; programming languages are translated into something the processor runs, by a compiler or an interpreter.
- Check your setup with `python3 --version`, experiment in the REPL, and write real programs in `.py` files with a code editor.
- The core loop is edit, save, run (`python3 file.py`), read the output.
- Text in quotes is a string; code outside quotes is evaluated. `print` shows output, `input` reads a line of text from the user.
- Error messages name the file, line and problem: read them from the bottom up, and change one thing at a time.

## Quiz

1. What does print("4 * 2") output?

   A. 8
   B. 4 * 2
   C. An error
   D. "8"

   **Answer: B.** The quotes make it a string, so Python prints the characters exactly as written instead of calculating them.

2. A user types 25 at an input() prompt. What does input give back?

   A. The number 25
   B. The string "25"
   C. Nothing until you call int()
   D. An error

   **Answer: B.** input always returns text. Convert it with int() before doing arithmetic with it.

3. Which file can you run with python3 to execute a program?

   A. A .docx file containing Python code
   B. Any file, as long as it's saved
   C. A plain-text file containing Python code, conventionally ending in .py
   D. Only files created in the REPL

   **Answer: C.** Python reads plain text. Word processors add formatting that isn't valid code, which is why you use a code editor and the .py extension.

4. Python shows 'SyntaxError' when you try to run a file. What does that mean?

   A. The code isn't valid Python, so nothing in the file ran
   B. The program crashed halfway through
   C. Python isn't installed
   D. The computer is out of memory

   **Answer: A.** A syntax error is found before the program starts, so no line runs. Errors that happen during running, such as ValueError, come later.
