1. Generators

  • Function that returns an iterator — i.e. An item that we can iterate over one value at a time
  • A generator can preserves all variables and states when a yield is encountered. Thereafter, when a next call is encountered the function carries on the execution from where it left off.

2. List comprehensions

  • List comprehensions are formed by creating a new list by applying some logic to all items in the original list.
  • For example, to square all entries in a list:

3. Optional arguments

  • It is possible to write functions with an arbitrary number of arguments
  • Consequentially, a & b are called required arguments, whereas optional is a list of optional arguments.
  • The optional arguments can also be represented as a hash-map if **optional is used instead of *optional.

4. Regular expressions

  • Regular expressions can be leveraged to uncover complex patterns from strings.
  • Python comes with a built in re module that can be used for this purpose

5. Partial functions

  • Partial functions are commonly used in tasks where you need to pre-populate some arguments in a function such as in the example below:

6. Closures

  • A closure is just a function that contains a nested function that uses one or more of the enclosing function’s variables.

7. Decorators

  • A decorator simply modifies a function and return a new function
  • This essentially means to augment a core function with additional functionality.

Written by

Former Glorified Electrician(aka Electrical Engineer). Now a Software Developer working on complex Enterprise Software. Lets connect on twitter @NdamuleloNemakh

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store