liveBook · Manning

Declarative programming

As explained in Tiny Python Projects: Learn coding and testing with puzzles and games

Declarative programming is a programming paradigm that describes the desired result or rules without specifying every execution step.

Declarative programming describes what to find or transform and delegates the execution work to a tool. Regular expressions illustrate the contrast: a pattern states what text to match, while the regex engine determines whether it is found. section 14.5 section 8.3.2

Recognize the approach through these contrasts:

  • Describe vowels with the pattern '[aeiou]' instead of iterating through characters and writing the matching logic manually. section 8.3.2
  • Let re.sub() apply a declared replacement to matching text, including separate patterns for lowercase and uppercase vowels. section 8.3.2
  • Treat list comprehensions, filter(), and map() as more concise ways to express selection or transformation over iterables. section 14.5

Replacing vowels with a pattern

pattern = '[aeiou]' vowel = 'o' re.sub(pattern, vowel, 'Apples and bananas!') # 'Applos ond bononos!' The program declares the characters to replace; re.sub() performs the search and replacement. section 8.3.2 section 14.0

>>> text = 'Apples and Bananas!'             #1
>>> vowel = 'o'                              #2
>>> new_text = []                            #3
>>> for char in text:                        #4
...     if char in 'aeiou':                  #5
...         new_text.append(vowel)           #5
...     elif char in 'AEIOU':                #6
...         new_text.append(vowel.upper())   #6
...     else:
...         new_text.append(char)            #7
...
>>> text = ''.join(new_text)                 #8
>>> text
'Opplos ond Bononos!'

Compare a character-by-character imperative vowel replacement with the declarative regex formulation. section 8.3.2

How Tiny Python Projects: Learn coding and testing with puzzles and games covers it

How deep it goes
Defines declarative pattern description and contrasts it with manual iteration, without descending into regex-engine internals.
Examples
Uses vowel replacement, list comprehensions, map(), filter(), and a rhyming-word generator to demonstrate declarative descriptions.
What it assumes
Assumes Python string-processing basics and introduces regular expressions as a more declarative alternative to character iteration.
What you can do after
Apply it via Python regular expressions to describe text patterns and replace matching characters without manual search loops.
How it connects
Connects regular expressions with imperative programming, list comprehensions, filter(), map(), and higher-order functions.

Also explained in

Compare how 14 books explain declarative programming →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Tiny Python Projects: Learn coding and testing with puzzles and games. The section links point to the parts of the book it draws on.