# 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.

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

```bash
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:

```python
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](https://skillok.org/topics/python-lists), [dictionaries](https://skillok.org/topics/python-dictionaries),
[slicing](https://skillok.org/topics/python-slicing), [strings](https://skillok.org/topics/python-strings) and
[functions](https://skillok.org/topics/python-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](https://skillok.org/topics/python-exceptions)
and [reading files in Python](https://skillok.org/topics/python-read-file)). 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](https://skillok.org/topics/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 — setup** — Python 3, an editor, a virtual environment, first commands in interactive mode.
2. **Month 1 — syntax** — Types, f-strings, conditions, loops; converters and tables in the console.
3. **Month 2 — collections and functions** — Lists, dictionaries, sets, slicing, list comprehensions; a word frequency count from a file.
4. **Month 3 — modules and tests** — The standard library, exceptions, unittest or pytest.
5. **Month 4 — two projects** — Projects with a README, requirements.txt and passing tests in a Git repository.

## Check yourself

1. What does this code print: print([1, 2, 3][-1])
   - 1
   - 3
   - IndexError
2. What is sum(range(5)) in Python?
3. What does this code print: print("ab" * 3)
   - ab3
   - ababab
   - TypeError

## Sources

- [Python 3 documentation](https://docs.python.org/3/) — The official tutorial, language reference and standard library reference
- [PEP 8](https://peps.python.org/pep-0008/) — The official style guide for Python code
- [Exercism — Python track](https://exercism.org/tracks/python) — Free Python exercises with automated tests
- [Automate the Boring Stuff with Python](https://automatetheboringstuff.com/) — A free online book on practical scripts for beginners

---

https://skillok.org/articles/en/learn-python-from-scratch
