tags:

views:

95

answers:

2

I installed the 3.1.2 IDLE python console, then I entered this code:

>>> a = ['a', 'b', 'c', 'd']
>>> len(a)
4

Directly from the python official docs http://docs.python.org/py3k/tutorial/introduction.html#lists

But it does not work in the interpreter as it should, it does not return 4.

What am I doing wrong? Are the official docs wrong?

I've looked in a few other sites but many are talking about Python 2.x.

A: 

THE DOCS are not wrong. Please check your installation.

jknair
This should be a comment, not an answer.
AndiDog
newbie !!! sorry :)
jknair
+5  A: 

There is no bug; you have misunderstood what should happen.

Python can be called interactively (by running python.exe at the prompt). This mode automatically prints the result of a line when it is finished, for ease of reading/debugging. However, it's not very useful for writing any serious amount of code.

The way do to that is to put the code in a script (a plain text file with a .py ending on Windows) and call the script with python.exe foo.py. In this case, something else happens: Python executes the script and does not automatically print the result of every line. That's what the print function/statement is for. Write print(len(a)) instead.

Note that this has nothing to do with the version of Python you are using.

katrielalex