liveBook · Manning

Dynamic typing

As explained in Python Workout, Second Edition

Dynamic typing is a type system in which values have runtime types and names can refer to values of different types without fixed variable declarations.

Dynamic typing keeps data strongly typed while leaving variables without fixed types. A name can refer to values of different types, enabling one generic function instead of several type-specific versions. section 4.5 section 4.2

Build one function for several sequence types

Use the shared sequence operations—indexes and slices—to write one function for strings, lists, tuples, and compatible user-defined types: ```python def firstlast(sequence): return sequence[:1] + sequence[-1:] firstlast('abc') # 'ac' firstlast([1, 2, 3, 4]) # [1, 4] firstlast((1, 2, 3, 4))# (1, 4) ``` Indexing retrieves individual elements, which may have any type. Slicing returns the same sequence type, making slice concatenation the suitable strategy here. section 4.2 section 4.3

Apply the same strategy when:

  • One operation should handle integers and floats rather than separate numeric functions. section 4.5
  • A function should work across strings, lists, tuples, files, or testing simulators that support the required operations. section 4.5
  • Practice with even-odd sums, alternating addition and subtraction, thresholded sums, integer conversion, or dictionary merging. section 4.5 section 4.9

How Python Workout, Second Edition covers it

How deep it goes
Explains variable and data typing through indexing, slicing, and generic functions, but does not descend into runtime implementation internals.
Examples
Demonstrates firstlast across strings, lists, tuples, and exercises involving numeric, textual, file-like, and dictionary inputs.
What it assumes
Assumes basic functions and sequence operations while teaching variable typing, indexes, slices, and generic input handling.
What you can do after
Build generic Python functions that operate across compatible types and preserve sequence types when using slices.
How it connects
Connects dynamic typing with generic functions, sequences, indexing, slicing, and strong typing.

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, Second Edition. The section links point to the parts of the book it draws on.