Step 02 of 10
Your first program
Understand what a program is, install Python and an editor, and write, run and change your first programs.
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.12on Ubuntu).
Then check it worked, in a new terminal window:
python3 --versionPython 3.13.1On 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:
$ 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:
mkdir -p ~/code/learn
cd ~/code/learn
code hello.py # opens the file in VS Code, if the 'code' command is installedIf 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:
print("Hello! This is my first program.")
print("2 + 3 =", 2 + 3)Run it from the terminal, in the same folder:
python3 hello.pyHello! This is my first program.
2 + 3 = 5That'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.
print("2 + 3") # prints the text: 2 + 3
print(2 + 3) # prints the result: 5Python is a capable calculator:
print(10 / 4) # 2.5 division
print(10 // 4) # 2 whole-number division
print(10 % 4) # 2 remainder
print(2 ** 10) # 1024 powerLines 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:
name = input("What's your name? ")
print("Nice to meet you,", name)What's your name? Sam
Nice to meet you, Samname = ... 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:
print("Hello! This is my first program.) 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.
Practice
Write a program about_me.py that:
- Asks for the user's name and the year they were born.
- Prints a greeting using their name.
- 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.
Show solution
# 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.")$ 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:
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.pyfiles 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.
printshows output,inputreads 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.
Check yourself
Quiz
0/4 correct · 4 left
1.What does print("4 * 2") output?
2.A user types 25 at an input() prompt. What does input give back?
3.Which file can you run with python3 to execute a program?
4.Python shows 'SyntaxError' when you try to run a file. What does that mean?
Finished reading?
Mark it complete to fill in your roadmap.