liveBook · Manning

Dynamic typing

As explained in Python Workout: 50 ten-minute exercises

Dynamic typing is a type system property in which an object's type is determined and checked at runtime rather than declared statically for each variable.

Dynamic typing lets variables refer to values of different types while functions operate on shared behavior rather than fixed declarations. Python therefore favors one generic function over several type-specific versions. section 3.1 section 3.5

One function for several sequence types

```python def firstlast(sequence): return sequence[0] + sequence[-1:] print(firstlast('abc')) # 'ac' print(firstlast([1, 2, 3, 4])) # [1, 4] print(firstlast((10, 20, 30)))# (10, 30) ``` The function relies on indexing and slicing, shared by strings, lists, and tuples. Its output keeps the input’s sequence type, avoiding separate implementations for each type. section 3.1 section 3.5

Practice the same flexible approach with functions that:

  • Sum values above a threshold for numbers, strings, or other comparable types. section 3.10
  • Convert integer-like arguments, ignore unconvertible values, and return their total. section 3.10
  • Combine dictionaries while collecting repeated keys into lists. section 3.10

How Python Workout: 50 ten-minute exercises covers it

How deep it goes
Explains variables without fixed types through worked sequence examples, stopping short of implementation internals.
Examples
Demonstrates first-last extraction, thresholded sums, numeric conversion, dictionary merging, and index-based sequence exercises.
What it assumes
Assumes functions and sequence operations, while teaching how shared APIs support type-flexible implementations.
What you can do after
Build it from scratch by writing functions that handle related operations across several compatible types.
How it connects
Connects dynamic typing with generic functions, sequences, indexing, slicing, comparison, and type conversion.

Also explained in

Compare how 6 books explain dynamic typing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Workout: 50 ten-minute exercises. The section links point to the parts of the book it draws on.