Tutorial by Examples

In most other languages indentation is not compulsory, but in Python (and other languages: early versions of FORTRAN, Makefiles, Whitespace (esoteric language), etc.) that is not the case, what can be confusing if you come from another language, if you were copying code from an example to your own, ...
These exceptions are caused when the type of some object should be different TypeError: [definition/method] takes ? positional arguments but ? was given A function or method was called with more (or less) arguments than the ones it can accept. Example If more arguments are given: def foo(a): re...
Is raised when you tried to use a variable, method or function that is not initialized (at least not before). In other words, it is raised when a requested local or global name is not found. It's possible that you misspelt the name of the object or forgot to import something. Also maybe it's in anot...
AssertError The assert statement exists in almost every programming language. When you do: assert condition or: assert condition, message It's equivalent to this: if __debug__: if not condition: raise AssertionError(message) Assertions can include an optional message, and you can d...
The gross majority of the time a SyntaxError which points to an uninteresting line means there is an issue on the line before it (in this example, it's a missing parenthesis): def my_print(): x = (1 + 1 print(x) Returns File "<input>", line 3 print(x) ^...

Page 1 of 1