Friday, February 23, 2018

Fun with list comprehensions

Hello!

Occasionally, I will be adding to this post, but I've been playing with list comprehensions. So, the code snippets I have here are 'tests' for you to take. I will post the code, and I will ask for an explanation on what the code does.

I will post the answers in another post.

The goal of this is to stimulate your mind so that you can dream up techniques and code that will be a big help. If you find the code useful in your own programs, that's great. If you could credit me in your documentation, even better.

If you need any help with your own code, I'd be happy to help you.

Happy coding!


Example 1: I was goofing around and developed this list comprehension. Can you explain what this does? This code is uses  Python 3. If you are using Python 2.7 use print without parentheses. If you are entering this in your interactive python session, you do not need print.
===================

alpha = 'abcdefghijklmnopqrstuvwxyz'

print([chr(ord(x)) for x in alpha if ord(x) % 2 == 0])
===============