Forums

Beginner question

Hello everyone!

I'm new to this website and my question may seem silly. I actually have no idea how to run my python code. Whenever I create a new file and hit "run and save", it will open a console, but nothing will happen. Or sometimes it will give error messages on commands that are absolutely correct, like

import pandas as pd import numpy as np s = pd.Series([1,3,5,np.nan,6,8]) print s

It will say "print s" is invalid syntax, which it certainly is not. What am I missing?

Hi there,

Our consoles use Python 3 by default, and Python 3 requires brackets around your print statements:

print(s)

That's my best guess as to what's going on. If you'd like, I can switch your default to Python 2...

That would be great! I'm currently learning python from sites like scipy.org and their documentation and tutorials seem to use Python 2.7.

Thanks!

Actually, I just remembered there's a way you can do this yourself, and then you can choose a different default version for each of your files.

The secret is to put a special line called a "hashbang" at the top of your file. It should be the first line, and look like this:

1
#!/usr/bin/env python2.7

or use #!/usr/bin/env python3.4 to get python 3.4, and similarly for 2.6 or 3.3...

Thanks a lot! That helps with the version issue.

I still have a question though. I just copied some code from a tutorial into a file and tried to run it. Still, the console (2.7 this time) remained black. The code worked fine on a desktop IDE. How come the plot didn't get displayed?

Sander

This was the code:

import matplotlib.pyplot as plt; plt.rcdefaults() import numpy as np import matplotlib.pyplot as plt

Example data

people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim') y_pos = np.arange(len(people)) performance = 3 + 10 * np.random.rand(len(people)) error = np.random.rand(len(people))

plt.barh(y_pos, performance, xerr=error, align='center', alpha=0.4) plt.yticks(y_pos, people) plt.xlabel('Performance') plt.title('How fast do you want to go today?')

plt.show()

Unfortunately the PythonAnywhere environment doesn't have direct access to your computer graphics, so we can't do things like interactive charts with plt.show().

There is a workaround though, which is that you can tell matplotlib (and other tools) to save charts to disk, and then you can view them via the Files tab. More info here: https://www.pythonanywhere.com/wiki/MatplotLibGraphs

Thank you so much! Everything's working now. Last question: is it recommended to learn python using 2.7? Or should I switch to 3.5 a.s.a.p.?

There's not that much difference between the two versions, so I wouldn't worry about it too much. Use the language that each tutorial recommends while you're following it, to make your life as easy as possible, and then try to use 3.4/3.5 when it comes to writing your own code. If you ever find you need to use an old library that's not supported in Python3 yet, you can switch to Python 2.

The main difference you're likely to bump into are that print is a function in Python 3. But in Python 2, if you put

from __future__ import print_function

Then you can use the Python 3 print function syntax. So I would do that in all your Python 2 code, so it's super-easy to switch it to being Python 3 if you need to, or back again. You can also keep that __future__ import in your Python 3 code, it just does nothing.

from __future__ import print_function
print("Hello World")  # this works happily in both Python 2 and Python 3