Static and dynamic typing: what is the difference?
Last updated: 2026-11-26
Static and dynamic typing differ in when types are checked. A static check analyzes the program text without running it, while a dynamic check happens as the program runs. How strict a language is about mixing types, often called strong or weak typing, is a separate question.
Start learning No account needed
1. Ask when the check happens
A type error means an operation was given a kind of data it cannot work with, such as a division that is handed a string of letters. A static checker looks for such problems in the source code, so it can report one on a line that your tests have not run. The price is caution. For a Turing-complete language, a checker that is sound (it lets no faulty program through) and reaches a verdict on every input has to reject some programs that would have run without a type error.
A dynamic check happens during the run. The usual technique is to tag each value with its type at run time and look at the tag before an operation relies on it. The program can then fail partway through. Many languages mix the two: Java, for example, checks types before running and still checks some conversions at run time.
2. Dynamic does not mean weak
Strong and weak are looser words, and people do not agree on a single meaning for them. People often use them for how readily a language converts one type to another without being asked. Python is dynamically typed, yet adding a number to a string raises an error instead of converting either one. Multiplying a string by a whole number is a defined operation that repeats the string, so you need to know what each operation accepts.
Writing type names is also a separate matter. Haskell and OCaml can be statically typed with few declarations, because the compiler infers the types. Python lets you write annotations, and its documentation states: "The Python runtime does not enforce function and variable type annotations." Separate tools, such as a static type checker, can read them.
3. A worked example: one receipt, two checks
Here is the program without annotations. The rush branch passes the text "4" where a number was meant.
def total(price, count):
return price * count + 5
def receipt(rush):
if rush:
return total("4", 3)
return total(4, 3)
The second version changes one line, the first one, to add annotations.
def total(price: int, count: int) -> int:
The third version has no annotations and drops the + 5. We ran all three. We also gave each to a toy script of our own, about twenty lines long, that reads the code without calling it and compares literal values written at each call with the annotations. It is not a real type checker. We did not run mypy, Pyright or any other real checker, so the last column shows nothing about what those tools would report.
| Version | receipt(False) | receipt(True) | Toy script findings |
|---|---|---|---|
| No annotations | 17 | TypeError at + 5 | 0 |
| Annotated | 17 | TypeError at + 5 | 1 |
| No annotations, no + 5 | 12 | the text "444" | 0 |
In the first version the error is TypeError: can only concatenate str (not "int") to str. The bad value enters at the call to total, but the multiplication accepts it and produces "444". The failure comes one step later, at + 5, and only on a run that takes the rush branch.
Adding annotations did not change what happened at run time. It gave our toy script something to compare, and the script reported one finding without running either branch: line 6, total expects int for price and gets str. In the third version nothing rejects the string, so the program returns text where you expected a number, with no error at all.
4. How to predict where a type error appears
First ask whether anything checks the code before it runs. If so, find the places where a value's declared or inferred type disagrees with what the receiver expects. For a dynamic check, follow the values along the path that runs until an operation cannot accept what it gets. A branch that does not run raises no run-time type error.
Passing a static check does not show that a program is correct. A well-typed program can still compute the wrong thing, and in most languages a compile-time type checker does not look for division by zero.
5. Test yourself
Close this page and predict what the first version does in each case, then run it: total(4, "3"); total("ab", 2); a program that defines the two functions and calls receipt(False) and nothing else. For each, say whether an error appears and on which operation. Then say what changes if the + 5 is removed.
Related guides: how to learn programming without copying code and how to test yourself.
The worked example is checked by a short script, scripts/static-vs-dynamic-typing-worked-example.py.
What the sources found, and their limits
Wikipedia, Type system. It supports the meaning of a type error, static and dynamic checking, the limit on sound checkers, the mix of both with the Java example, inference in Haskell and OCaml, the lack of an agreed definition of strong and weak, and the limits of what type checks catch. Source: Type system, read on 2026-11-25.
Wikipedia, Type safety. It supports the use of strong and weak for aspects of type safety, including implicit conversion. Source: Type safety, read on 2026-11-25.
Python documentation. The typing module page is the source of the quoted sentence about annotations, the glossary defines a static type checker as a separate tool that analyzes code, and the tutorial introduction describes Python as having dynamic typing. Sources: typing, the glossary and the tutorial introduction, read on 2026-11-25. Limits: we read the relevant passages and not each page in full. The program and the toy script are ours.
How Professor Turing helps
Professor Turing is a tutor for adults learning any subject, in college or on their own. It asks you questions instead of giving the final answer, does not grade a bare guess, and remembers what you got wrong. No account is needed to start. It's an AI tutor, free to try.
Bring a line of code whose type error you cannot predict. Its first reply asks what you already know before it teaches anything. If you answer with a bare guess, it asks how you got there instead of saying right or wrong, and when you stay stuck it changes its approach and makes the next step smaller, so you work it out yourself. You must be 18 or older, and guest conversations are deleted 7 days after the last activity.
FAQ
Is Python statically or dynamically typed? Python checks types as the program runs. You can add annotations and use a separate static type checker, but Python does not enforce them when the program runs.
Does static typing mean I have to write every type? No. Some statically typed languages, such as Haskell and OCaml, infer many types, so you can leave those declarations out.
Start with what you're working on
Tell the tutor which topic you are studying and let it ask you the questions.
Start learning No account needed
See also: all study guides