✦ Python Intermediate & OOP Code quality

L2 · Concept 01 · Code Quality & Testing

Type Hints — Write Code
That Explains Itself

Learn how type hints help you, other programmers, and your editor understand Python code more clearly.

Learning goalUse type hints for parameters, return values, variables, collections, optional values, and simple class methods.
01 Learn02 Work locally03 Check your understanding

Learning as a guest

Learning as a guest? Save your progress!

Create a free account to learn with Sloby AI, check your understanding, and track your progress.

The entire platform is free throughout 2026. Foundations stays free forever.

Type hints make expectations visible.

When you read a function without hints, it is not always clear which values it expects or returns.

def izracunaj_cenu(cena, popust):
    return cena - cena * popust / 100

def izracunaj_cenu(cena: float, popust: float) -> float:
    return cena - cena * popust / 100

The algorithm did not change. float describes the expected parameters, and -> float describes the expected return value.

Common basic hints are int for whole numbers, float for decimal numbers, str for text, and bool for True or False.

↳

Important: Python does not enforce type hints during normal execution. An annotation is useful information, not automatic runtime validation.

Parameters, returns, variables, and collections
def napravi_pozdrav(ime: str) -> str:
    return f"Zdravo, {ime}!"

def prikazi_poruku(poruka: str) -> None:
    print(poruka)

ime: str = "Ana"
godine: int = 20
ocene: list[int] = [8, 9, 10]
cene: dict[str, float] = {"Hleb": 85.0}

A parameter hint describes input. The hint after -> describes the result. None means that a function performs an action but does not return a useful value. Collection hints describe the values inside a list or dictionary.

Optional and alternative values
def pronadji_korisnika(korisnik_id: int) -> str | None:
    ...

vrednost: int | float = 12.5

str | None means the result can be text or no result. int | float accepts either kind of number. The | union syntax requires Python 3.10 or newer.

Hints in a class
class Proizvod:
    def __init__(self, naziv: str, cena: float) -> None:
        self.naziv = naziv
        self.cena = cena

    def prikazi(self) -> str:
        return f"{self.naziv}: {self.cena} RSD"

This connects type hints with OOP. In this simple example, self does not need its own explicit hint.

What type hints do not do

  1. They do not reject every wrong valueA function can still receive a value with the wrong type. A real error can happen later when Python performs an incompatible operation.
  2. They do not convert valuesA str hint does not automatically turn a number into text, and an int hint does not turn text into a number.
  3. They do not replace testing or reasoningCorrect hints cannot prevent logical errors. VS Code, Pylance, or static-analysis tools can warn about possible mismatches depending on their configuration, but that is not runtime validation.

Before asking Sloby for help

  • Expected type is not a guaranteeAsk what Python actually does with the supplied value at runtime.
  • Return type is unclearLook for every return path, then decide whether the function returns a value or None.

Make Your Code More Professional

Do this in VS Code

In the next step, you will use a small local product-inventory project. For now, prepare to add hints without changing program behavior.

Download starter project
Product inventory starter project
type_hints_proizvodi/
├── main.py
├── proizvodi.py
└── README.md

The starter program works without type hints. Add them yourself, then confirm that its terminal output stays the same.

Prepare for local practice

  1. Open the project in VS CodeYou will run the starting program first, then add hints to parameters, return values, variables, and collections.
  2. Keep behavior the sameAfter adding hints, run the program again. Type hints should clarify the code without changing its intended result.
↳

Remember: python main.py A program can run even when a static tool reports a possible type mismatch.

Local practice reminder

  • Use the selected interpreterVS Code warnings depend on the Python interpreter and available analysis tools.

What happens when this code runs?

Separate the type hint from the operation Python actually executes.

def pozdrav(ime: str) -> str:
    return "Zdravo, " + ime

print(pozdrav(123))
Choose the output

Build confidence independently

Add useful type hints

Work in your own local folder; do not send a complete solution to Sloby.

Your goalCreate a function that receives a list of prices and returns their total. Add suitable type hints and explain why you chose them.
your_project/
└── main.py

Work in your local VS Code folder

Progressive hints
  • First decide what each item in the price list represents.
  • Then decide what the function returns after adding the values.

Ask Sloby for one guiding question about expected and actual types; it cannot run your local files.

Sloby, the Python AI Tutor mascot

Your learning mentor

Sign in to ask Sloby

Create a free account or sign in to get AI guidance while you learn.

Sign in to check your understanding

Create a free account or sign in to receive AI feedback and save your learning progress.

Python AI Tutor · Learn by understanding, not just by running code.