Python informer

Improve your Python coding skills

Higher order functions

Higher order functions are functions that act on other functions. There are two built-in Python higher order functions - map and filter. map map applies a function to each element in an iterable, and returns the results as a new iterable. Here is an example: import math k = [1, 25, 81] e = map(math.sqrt, k) print(list(e)) #[1.0, 5.0, 9.0] In this case the map function applies the sqrt function to each of the elements in the list [1, 25, 81]. Read more →

Built-in functions on iterables

Python includes a number of built-in functions that work on iterables. These functions generally work with any iterable, either a lazy iterable (as returned by a generator or a range function), or a data structure such as a list or tuple. There are a few exceptions, noted in the descriptions below. Most of these functions are described below. The exceptions are: map and filter are described in the section on higher order functions. Read more →

Iterators vs iterables

If you have studied Python even for a short while, you will probaly have come across the terms iterator, iterable and sequence. They are often used almost interchangeably, but they are all slightly different things. Iterators An iterator is very simple. It is any object that has a __next__ method. Each time you call the __next__ method, it will return a value. For example, the module itertools contains a function count that returns an iterator. Read more →


A generator is a convenient way to create your own iterators. Generator functions A generator function looks similar to a normal function, except that instead of a return statement it has a yield statement. This simple difference tells Python that it is a generator function. Here is an example: def count_down(): yield(5) yield(4) yield(3) yield(2) yield(1) You can use count_down anywhere you might use an iterator, for example in a for loop (it does a similar job to a range): Read more →

Map/reduce example

As an example of functional programming, in this article we will use some Python built in functions to analyse a text file. In the first example we will count the number of words in the file. In the second example we will extract the unique words. The basic method is map/reduce. In this model the map function is used to pre-process the input data. The reduce function is then used to combine the data to give the final result. Read more →


Programmers often need to deal with lists of values, and most languages provide data structures for this purpose (in Python the type list is usually used). However, in functional programming we don’t usually deal with lists directly. Instead we use an iterator that fetches values from a list or other sequence. This has two advantages: An iterator can read from a list but it can’t modify the list. So if you pass an iterator into a function, you can be sure that the function will not change the list. Read more →

What is functional programming?

There are several major programming paradigms. These include: Procedural programming - this is type of programming you might use when you are first learning Python. A program might consist of a sequence of lines assignment statements, if statements, and loops. Larger programs might define functions to make the code more intelligible and maintainable. This style of coding is fine for scripts and simple programs, but more complex programs usually benefit from an alternative style. Read more →