## Python language basics 67: iterating over a list with list comprehension

Introduction

In the previous post we looked at a practical example using the yield keyword in Python. The function distributed an integer across a number of groups as equally as possible.

In this post we’ll look at an interesting language construct called comprehension. A comprehension in Python is a form of iteration which provides a concise and expressive way of iterating over a collection. In particular we’ll consider list comprehensions, but keep in mind that comprehensions can be applied to any iterable object, such as sets, tuples or generator functions. The delimiting element, such as curly braces for sets and square brackets for lists will determine the type of object returned by the comprehension.

## Python language basics 66: a practical example of a yield generator in Python

Introduction

In the previous post we continued our investigation of yield generators in Python. In particular we looked at how a yield function could remember the state of its internal variables. We saw how code execution jumps in and out of a yield function as it’s being enumerated.

At this point you may think that yield functions are interesting but what can they be used for? You’ll find a lot of examples on the internet. In this post we’ll look at the following scenario:

Say you’d like to divide an integer, e.g. 20, into equal parts of 3 and distribute any remainder equally across the groups. The result of such an operation would be the following 3 integers:

7,7,6

20 can be divided into 3 equal parts of 6 and we have a remainder of 20 – 6 * 3 = 2. 2 is then added as 1 and 1 to the first two groups of 6. The result is a more or less equal distribution of the start integer.

The following function will perform just that:

## Python language basics 65: keeping state in yield generators

Introduction

In the previous post we looked at a basic example of using the yield keyword in Python. We saw that yield generator functions are essentially collections that return a certain number of elements when they are enumerated. The elements to be returned – yielded – are not loaded into memory before they are required in a loop. We also said that generator functions can “remember” the state of their variables.

We’ll see an example of what this means in code.

## Python language basics 64: a basic example of yield generators

Introduction

In the previous post we looked at the continue keyword in Python. We saw how it helped us stop the execution of a loop in case the current value of the iteration wasn’t to be handled in the same way as others.

In this post we’ll look at something completely different: the somewhat odd language construct of yielding a value.

The English word ‘yield‘ means approximately ‘produce’ or ‘give way’. A good investment can yield a high return. A fertile field can yield a lot of corn.

Yielding in Python

The yield keyword in Python is similar in usage. It is used in conjunction with loops that are lazily evaluated, where lazy means ‘on demand’ so that no memory is occupied unless the function is evaluated by the caller. Also, the function that yields a value – called a generator function – “remembers” the state from the previous execution. This all may sound mysterious. The usage of the yield keyword is definitely confusing at first I think.

## Python language basics 63: jump over an iteration with continue

Introduction

In the previous post we looked the finally keyword. We discussed how it could be used to write a code block which is executed whether or not the code within the try-block threw an exception.

In this post we’ll look at another Python keyword which helps you jump over an iteration in case you don’t want to handle the current value in the loop in any way.

There are cases where you simply don’t want to handle certain values while iterating over a collection. The for-each block will be executed for each element in the collection but for some of them this logic should not be executed.

You can easily solve this with if-else block(s), but there’s another solution.

Continue

The continue keyword will stop the execution of the for-each block. Execution will jump back to the beginning of the loop and take the next value in the collection. This keyword exists in other popular languages such as Java or C# and it works in exactly the same way in Python.

## Python language basics 62: clean-up after exceptions with finally

Introduction

In the previous post we discussed how to re-raise an exception using the raise keyword. You can use “raise” in order to re-throw the most recent exception with all its details so that it’s not silently suppressed or replaced with another exception which lacks the necessary details in order to locate bugs.

In this post we’ll see how to run a code block irrespective of whether an exception was raised or not.

## Python language basics 61: raising exceptions

Introduction

In the previous post we saw how to extract some useful information from an exception in Python. We discussed how to store the exception in a variable using an “as” clause and how to read its type and arguments.

In this post we’ll see how to use the “raise” keyword to re-raise or re-throw an exception.

## Python language basics 60: interrogating the exception

Introduction

In the previous post we saw a couple of ways to handle multiple exception types in the same except block. You can provide a list of exception types separated by a comma or you can have a generic handler where you catch the Exception type.

Exception handling is not as straightforward and easy as it first may seem. Therefore we’ll continue our discussion of the topic by ways of querying the exception object.

## Python language basics 59: catching multiple exception types

Introduction

In the previous post we looked at how to handle exceptions in python. The most important keywords you’ll need to know of are “try” and “except”. We saw how code execution stops at the point where the exception is raised. It continues on the first line of a matching except-block, i.e. which handles just that type of exception that was raised.

This short post shows a little trick if you want to handle different types of exception in the same way.

## Python language basics 58: exception handling in code

Introduction

In the previous post we started discussing exceptions and errors. We looked at a couple of important terms, such as throwing, catching and handling exceptions. Good exception handling will make your application more stable. Your users may still be irritated by exception messages but it’s nothing compared to how they feel when an application simply crashes, closes by itself or even freezes the device it’s running on so that it needs rebooting.

In this post we’ll look at how to handle exceptions in code.

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.