Python is a programming language known for being easy to read. Its code looks a little like plain English, which is why it is one of the most popular first languages. It is also used by professionals every day. People use Python to analyse data, automate boring computer tasks, build websites and back ends, work with spreadsheets, write scripts for testing, and train machine learning models. You can learn the basics in a few weeks of regular practice, and the same basics carry over to all of those jobs.
This course has 15 lessons. You need a computer, a free code editor and some patience. You do not need to know any programming. Every lesson ends with a task and a way to check that your answer works. The code samples in the course were all run before they were written down, and the outputs shown are what Python printed.
Install Python
Go to the official website, python.org, open the Downloads page and install the latest stable version of Python 3. This course works with Python 3.9 and newer. Python 2 is very old and no longer supported, so make sure your version starts with 3.
- Windows: run the installer and tick Add python.exe to PATH at the bottom of the first screen before you click Install. This one tick prevents the most common beginner problem.
- macOS: use the installer from python.org. Your Mac may already have a
python3command, and that is fine for learning, but a newer version from python.org is better. - Linux: Python 3 is usually installed. If not, use your package manager, for example
sudo apt install python3 python3-venv python3-pipon Ubuntu and Debian.
Now open a terminal. On Windows, use PowerShell or the Terminal app. Check the version.
python --version
It prints something like Python 3.13.1. On macOS and Linux the command is usually python3, and on Windows you can also use py. In this course we write python. If that command is not found on your computer, use python3 or py instead, everywhere.
Your editor
You can write Python in any text editor. A friendly free choice is Visual Studio Code with its Python extension. Another is Thonny, which was made for beginners and comes with Python built in. Pick one and stay with it. The editor is not the important part. Writing and running code is.
The interactive prompt
Type python on its own and press Enter. You see a prompt that looks like >>>. Here you can type one line of Python and see the result at once. It is perfect for trying small ideas. Type exit() to leave.
>>> 2 + 3
5
>>> "ha" * 3
'hahaha'
>>> exit()
Your first program
Real programs live in files that end in .py. Make a folder for the course, for example python-course, and open it in your editor. Create a file called hello.py with the code below. Words after a # are comments, notes for people that Python ignores.
name = "Asha"
hours = 3
print("Hello,", name)
print(f"You have studied for {hours} hours.")
import sys
print("Python 3 is running:", sys.version_info.major == 3)
print(sys.argv[1:])
Run it from the terminal, in the same folder. We add two extra words after the file name to see how they arrive.
python hello.py one two
Hello, Asha
You have studied for 3 hours.
Python 3 is running: True
['one', 'two']
Look closely at what each line did. name = "Asha" creates a variable, a name that holds a value. print(...) shows something on the screen. Putting an f before a quote makes an f-string, where anything in curly braces is replaced by its value. import sys brings in a built-in tool box. sys.argv is the list of words typed on the command line, where the first item is the file name, so [1:] skips it. The last print shows True because the comparison == 3 is true.
How Python reads your file
- Python runs the file from the top to the bottom, one line at a time.
- Indentation matters. In Python, spaces at the start of a line show which lines belong together. You will see this in the next lessons. Use four spaces and be consistent.
- Capital letters matter:
Nameandnameare different variables. - Text goes inside quotes, either single or double. Numbers do not.
Reading error messages
Every programmer makes errors all day. Python tells you what went wrong, and where. Look at the last line of the message first. It names the type of problem, such as SyntaxError (the code is written wrongly), NameError (a name that does not exist, often a typing mistake) or TypeError (the wrong kind of value). Above it, Python shows the file name and line number. Fix the first error, then run again. Do not be afraid of red text. It is help.
A few habits to start with
- Run your code often, after every few lines, so you find mistakes while they are small.
- Use clear names such as
total_priceand notx. - Type the examples yourself instead of copy and paste. Typing teaches your hands and your eyes.
- When you are stuck, read the error, change one thing and run again.
Your task
- Install Python 3 and run
python --version. - Create
hello.py. Change the name and the number of hours to your own. - Run it with three words after the file name and check that all three are printed.
- Open the interactive prompt, calculate
15 * 4 + 2, and leave withexit().
How to check it worked
python --versionshows a version that starts with 3.- Running
python hello.py one twoprints the greeting, thenPython 3 is running: True, then['one', 'two']. - You can say what the last line of an error message tells you.