Tutorial: List Comprehensions in Python
66 0 8790
List comprehensions are a really powerful tool that every Python programmer should know, they often reduce many lines of awkward, hard to read code down to a single line. Most Python code you will find will be full of them for good reason.
This is an updated version of my video where I explain list comprehensions (alongside set/dictionary comprehensions and generator expressions). This new version has better audio, has cut some of the intro stuff and has a few extra nuggets of information.
Please note that set and dictionary comprehensions are a feature of Python 2.7+, so if you are using an older version, you will not be able to use them (as an alternative, use a generator expression to the set() builtin or one producing (key, value) tuples in the dict() builtin as appropriate).
I am using PyCharm (http://www.jetbrains.com/pycharm/) as my editor, and I highly recommend it. It's not free, and while you definitely don't *need* an IDE to work in Python, for more complex tasks it's very nice to have a good one. The font I'm using is Source Code Pro, which is freely available at http://sourceforge.net/projects/sourcecodepro.adobe/.
By anonymous 2017-09-20
Your traceback is telling you that
int() takes integers, you are trying to give a decimal, so you need to use
a = float(a)
This should work as expected:
>>> int(input("Type a number: ")) Type a number: 0.3 Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: '0.3' >>> float(input("Type a number: ")) Type a number: 0.3 0.3
Computers store numbers in a variety of different ways. Python has two main ones. Integers, which store whole numbers (ℤ), and floating point numbers, which store real numbers (ℝ). You need to use the right one based on what you require.
(As a note, Python is pretty good at abstracting this away from you, most other language also have double precision floating point numbers, for instance, but you don't need to worry about that. Since 3.0, Python will also automatically convert integers to floats if you divide them, so it's actually very easy to work with.)
Previous guess at answer before we had the traceback:
Your problem is that whatever you are typing is can't be converted into a number. This could be caused by a lot of things, for example:
>>> int(input("Type a number: ")) Type a number: -1 -1 >>> int(input("Type a number: ")) Type a number: - 1 Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: '- 1'
Adding a space between the
1 will cause the string not to be parsed correctly into a number. This is, of course, just an example, and you will have to tell us what input you are giving for us to be able to say for sure what the issue is.
Advice on code style:
y = [int(a)**(-2),int(a)**(-1.75),int(a)**(-1.5),int(a)**(-1.25), int(a)**(-1),int(a)**(-0.75),int(a)**(-0.5),int(a)**(-0.25), int(a)**(0),int(a)**(0.25),int(a)**(0.5),int(a)**(0.75), int(a)**1,int(a)**(1.25),int(a)**(1.5),int(a)**(1.75), int(a)**(2)]
This is an example of a really bad coding habit. Where you are copying something again and again something is wrong. Firstly, you use
int(a) a ton of times, wherever you do this, you should instead assign the value to a variable, and use that instead, avoiding typing (and forcing the computer to calculate) the value again and again:
a = int(a)
In this example I assign the value back to
a, overwriting the old value with the new one we want to use.
y = [a**i for i in x]
This code produces the same result as the monster above, without the masses of writing out the same thing again and again. It's a simple list comprehension. This also means that if you edit
x, you don't need to do anything to
y, it will naturally update to suit.