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.

Updated
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:

python3 -m venv .venv
source .venv/bin/activate      # on Windows: .venv\Scripts\activate
python -m pip install requests

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:

for c in range(0, 101, 20):
    f = c * 9 / 5 + 32
    print(f"{c:>3} °C = {f:5.1f} °F")

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 (ruff or flake8) 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

  1. Week 1 — setupPython 3, an editor, a virtual environment, first commands in interactive mode.
  2. Month 1 — syntaxTypes, f-strings, conditions, loops; converters and tables in the console.
  3. Month 2 — collections and functionsLists, dictionaries, sets, slicing, list comprehensions; a word frequency count from a file.
  4. Month 3 — modules and testsThe standard library, exceptions, unittest or pytest.
  5. 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.

Start the plan

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

Was this helpful?

More articles

Programming and IT How to learn SQL from scratch SQL is the query language of relational databases. Developers, analysts, testers and managers who want to pull numbers themselves all need it. Basic queries take a few weeks to learn; working confidently with complex reports takes two or three months of practice. Below is the order of topics and ways to train on a real database. Programming and IT How to learn Linux from scratch Linux runs most servers, containers and countless devices, so developers, testers, analysts and system administrators all need the command line. The easiest way to learn it is not by reading lists of commands but by working in the terminal every day and solving small practical tasks. Below is a sequence of topics for two to three months. Programming and IT How to learn JavaScript from scratch JavaScript runs in every browser and, through Node.js, on the server too. It is easy to start — you can run code right in the browser console — but it has plenty of surprising corners, from type coercion to asynchronous code. The plan below takes three to four months and assumes you already know basic HTML and CSS or are learning them alongside. Programming and IT How to learn Java from scratch Java is a strictly typed language behind banking systems, the servers of large services and Android apps. The strictness slows you down at first, but the compiler catches many mistakes before the program ever runs. This plan takes about six months at an hour a day and leads from your first program to a small backend application. Programming and IT How to learn machine learning from scratch Machine learning means building models that find patterns in data and make predictions from them. Starting with neural networks is tempting, but it is safer to go from the bottom up — from Python and math to classic algorithms, honest evaluation and only then deep learning. Without a technical background, expect six months to a year of regular study. Programming and IT C++ from scratch C++ is a compiled language used wherever speed and direct access to memory matter — game engines, browsers, databases, firmware. It is harder to get into than Python, but you can build your first working program on the very first evening.

More solutions