liveBook · Manning

Functional, stateless philosophy

As explained in Python Workout: 50 ten-minute exercises

Functional programming is a programming paradigm that emphasizes composing functions, minimizing mutable state, and transforming inputs into outputs.

Functional programming pursues short, reliable, maintainable code by keeping functions short, treating data as immutable, and avoiding tracked state. Functions transform inputs into outputs rather than modifying existing values, which can reduce test paths and debugging effort while feeling unnatural when programs must model changing real-world data. section 7.0

The approach rests on a few practical habits:

  • Keep functions short so they are easier to understand, test, and maintain. section 7.0
  • Avoid modifying data inside functions; create new values when a changed result is needed. section 7.0
  • Pass functions as arguments and use them to filter or transform iterable inputs. section 7.5
  • Prefer comprehensions for many Python transformations, while recognizing that map can handle multiple iterables conveniently. section 7.5

Transform without changing the input

A comprehension selects and transforms even numbers in one expression: [x*x for x in range(10) if x%2 == 0] The input range is not updated; the expression produces a new sequence of squared values. The same pipeline can be expressed with map and filter when named functions or multiple iterables make that form clearer. section 7.5

Table 7.1 What you need to know (view table figure)

Concept

What is it?

Example

To learn more

List comprehension

Produces a list based on the elements of an iterable

[x*x for x in range(5)]

http://mng.bz/lGpy

Dict comprehension

Produces a dict based on the elements of an iterable

{x : 2*x for x in range(5)}

http://mng.bz/Vggy

Set comprehension

Produces a set based on the elements of an iterable

{x*x for x in range(5)}

http://mng.bz/GVxO

input

Prompts the user to enter a string, and returns a string

input('Name: ')

http://mng.bz/wB27

str.isdigit

Returns True or False, if the string is nonempty and contains only 0-9

# returns True

'5'.isdigit()

http://mng.bz/oPVN

str.split

Breaks strings apart, returning a list

# Returns ['ab', 'cd', 'ef']

'ab cd ef'.split()

http://mng.bz/aR4z

str.join

Combines strings to create a new one

# Returns 'ab*cd*ef'

'*'.join(['ab', 'cd', 'ef'])

http://mng.bz/gyYl

string.ascii _lowercase

All English lowercase letters

string.ascii_lowercase

http://mng.bz/zjxQ

enumerate

Returns an iterator of two-element tuples, with an index

enumerate('abcd')

http://mng.bz/qM1K

Use the reference table to compare list, dictionary, and set comprehensions with input and string-transformation tools. section 7.0

How Python Workout: 50 ten-minute exercises covers it

How deep it goes
Explains immutability, state avoidance, higher-order functions, map, filter, and comprehensions, but stops short of formal theory.
Examples
Demonstrates squared even numbers, word-length mapping, long-word filtering, and parallel letter-number multiplication.
What it assumes
Assumes familiarity with Python functions and iterables while introducing immutable data, function arguments, expressions, and comprehensions.
What you can do after
Apply functional techniques through Python comprehensions, map, and filter to transform iterable data without direct mutation.
How it connects
Connects immutability with short functions, higher-order functions with map and filter, and comprehensions with iterable transformation.

Also explained in

Compare how 19 books explain functional, stateless philosophy →

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.