Programming and IT
How to learn Python from scratch
Python is a good first programming language: code reads almost like text, and the standard library covers most everyday tasks. This plan takes you from installing the interpreter to your own scripts covered by tests in about four months, at roughly an hour a day.
In this article
Setup: interpreter, editor, environment#
Download the current Python 3 release from python.org. On Windows, tick the
option to add Python to PATH during installation; on macOS and Linux the
interpreter is usually called with python3. For an editor, VS Code with the
Python extension or PyCharm Community both work well.
Get used to virtual environments from your very first project — they keep one project's libraries separate from another's:
Interactive mode (python3 with no arguments) is handy for experiments: type an
expression and see the result immediately.
Month 1: syntax and control flow#
Indentation in Python is part of the syntax: the block after if, for or def
is marked with four spaces. Work through the int, float, str and bool
types, string formatting with f-strings, conditions, and for and while loops.
A first meaningful program is a multiplication table or a temperature converter:
= * 9 / 5 + 32
Month 2: collections and functions#
Python's main data structures are the list, the tuple, the dictionary and the
set. Learn to choose between them: a dictionary when you need lookup by key, a
set when uniqueness matters. Master slicing, list comprehensions, sorted with
the key parameter, and unpacking. Write functions with clear names, default
values and a docstring. The exercise for the month: a word frequency count from a
text file that prints the ten most common words.
Each of these topics has its own article with examples: lists, dictionaries, slicing, strings and functions.
Month 3: modules, files, exceptions, tests#
Split your code into modules and learn to import your own and standard ones:
pathlib for paths, csv and json for data, datetime for dates,
collections for counters. Handle exceptions precisely — catch a specific error
type, not everything at once (see Python exceptions
and reading files in Python). Start writing tests:
the standard library has unittest, and pytest is a popular alternative. A good
habit is to write the test for a function first and the function after it.
Month 4: portfolio projects#
Pick two projects and take them to the point where "someone else can run it from the README":
- a scraper: download a page with
requests, extract data, save it to CSV; - a chat bot (Telegram or Discord) with a couple of commands and data stored in SQLite;
- a script that tidies up your downloads folder by file type;
- a small analysis of an open dataset with a chart in
matplotlib.
Keep the projects in a Git repository, add a requirements.txt dependency file
and tests. A clean pytest run is the minimum condition for calling a project
finished.
How to tell you have the basics down#
- You solve "easy" and some "medium" problems on auto-checked practice sites without hints — for a start, try the Python practice problems.
- You can read someone else's 200 lines of code and explain what they do.
- You find answers in the official documentation faster than with a search engine.
- You check your code style with a linter (
rufforflake8) and understand its warnings — many of them are based on PEP 8.
Typical traps: a mutable default argument (def f(items=[])), comparing with
None using == instead of is, and confusing a copy of a list with a second
reference to the same list.
Step-by-step plan
- Week 1 — setupPython 3, an editor, a virtual environment, first commands in interactive mode.
- Month 1 — syntaxTypes, f-strings, conditions, loops; converters and tables in the console.
- Month 2 — collections and functionsLists, dictionaries, sets, slicing, list comprehensions; a word frequency count from a file.
- Month 3 — modules and testsThe standard library, exceptions, unittest or pytest.
- Month 4 — two projectsProjects with a README, requirements.txt and passing tests in a Git repository.
Start learning this in your own space
The plan goes into your repository: tick off stages, keep notes — the change history shows how far you have come.
Check yourself
1.What does this code print: print([1, 2, 3][-1])
2.What is sum(range(5)) in Python?
3.What does this code print: print("ab" * 3)
Sources
-
Python 3 documentationThe official tutorial, language reference and standard library referencefree
-
PEP 8The official style guide for Python codefree
-
Exercism — Python trackFree Python exercises with automated testsfree
-
Automate the Boring Stuff with PythonA free online book on practical scripts for beginnersfree
Was this helpful?