After this lesson you can say what Python is, describe what people build with it, and picture what happens between writing a line of code and seeing its result. Nothing here needs to be installed yet. It is the map before the journey.
What Python is
Python is a programming language: a way of writing instructions that a computer can follow. It was designed to be readable, so a short Python program often looks close to a description of what you want done. That is the main reason it is so often chosen for teaching.
Python is also a general-purpose language. It is not built for one narrow job. The same language that checks a list of prices can also run a website, process thousands of files or analyse a spreadsheet.
Here is a small taste. Do not worry about every detail yet, because later lessons explain each piece. Just read it and see how much you can follow:
prices = [120, 80, 45, 200]
total = 0
for price in prices:
total = total + price
print("Total:", total)Total: 445You can probably guess the idea: start a total at zero, add every price to it, then show the result. That is the readability people talk about.
Here is another, using a sentence from an imaginary shop:
sentence = "Fresh bread is baked every morning"
words = sentence.split()
print(len(words))
print(words[0])6
FreshThe sentence is split into separate words, then the program shows how many there are and the first one.
How your code actually runs
A Python program is just a text file. By convention its name ends in .py, for example shop.py. You write it in any text editor, although a code editor makes life easier.
To run the file you need the Python interpreter. This is a program, installed on your computer, that reads your text file and carries out the instructions one by one. When you "run Python", you are starting the interpreter and telling it which file to read.
This is different from languages where you first turn the whole program into a separate application. With Python you normally keep the text file and run it again each time. If you change the file and run it again, you see the new behaviour immediately. That quick loop of change, run and look is wonderful for learning.
What people use Python for
Python shows up in many places. Some common ones:
- Automation. Renaming hundreds of files, sending reports, tidying data. Small scripts save a lot of boring work.
- Websites and APIs. Many web back ends are written in Python.
- Data analysis. Cleaning, summarising and charting data.
- Science and research. Researchers use it to run calculations and experiments.
- Machine learning. A large share of machine learning work is done in Python.
- Teaching. Many schools and courses start with it.
Python is not the best tool for everything. It is usually slower than languages that are compiled ahead of time, and it is not the usual choice for building phone apps. For learning programming, and for a very wide range of everyday tasks, it is an excellent fit.
Python 3 and this tutorial
You will see two version numbers mentioned online. Python 2 is old and no longer supported. Python 3 is what everyone uses now, and it is what this tutorial teaches. Every example here was run on Python 3.9, and you can use 3.9 or anything newer. Where a newer version adds something handy, the lesson will mention it in a sentence but will not rely on it.
This written tutorial is a companion to the video course at /learn/python/, so you can read a topic here and watch it explained there if you like to learn both ways.
Common mistakes
- Mixing up Python 2 and Python 3. Old articles may show code that fails on modern Python. Check which version a tutorial uses.
- Thinking indentation is decoration. In Python, the spaces at the start of a line are part of the grammar. You will see why in the control flow lessons.
- Expecting to understand everything at once. Programming is learned by writing small programs and running them. Reading alone is not enough.
- Choosing a language by fashion. Python is a good first language, but the ideas you learn, such as variables, loops and functions, carry over to others.
Exercise
Write down, on paper or in a note, three small chores that a program could do for a small shop, such as adding up a day's sales or listing items that are low in stock. For each one, say what information goes in and what you would want to see come out.
Show one possible answer
- Add up a day's sales. In: a list of sale amounts. Out: one total.
- List low-stock items. In: item names with stock counts. Out: only the items with fewer than five left.
- Count orders per customer. In: a list of orders with customer names. Out: each name with a count.
Thinking in terms of "what goes in, what comes out" is the habit that all programming builds on. You will use it in every lesson.
Next: installing Python on your own computer.
Key points
- Python is a general-purpose programming language known for code that reads close to plain English.
- A Python program is a plain text file, usually ending in .py, that the Python interpreter reads and runs.
- People use Python for websites, automation, data analysis, science, teaching and machine learning.
- This tutorial uses Python 3 and every example runs on Python 3.9 or newer.