Python on local machine

python
ide

#1

I’ve just started to learn how to code Python a couple days ago and now I am curious as to how I can get coding on my computer as opposed to this website, just for extra practice. Let me explain: with the lessons on this website, the middle screen is where one types the code and the screen on the far right is where the results are. My question is how I can get access to those two screens without needing to use the website. I have downloaded Python 3.6 but the files are very confusing. Also, if I learning Python 2, should I download Python 2 as opposed to Python 3, or is Python 3.6 a version of Python 2?


Welcome to our forums! Please Introduce Yourself
#2

Yes and no. There are a lot of differences, but most Python 2 code and syntax still works in Python 3. It doesn’t make a lot of sense to install Python 2 on your machine unless you want it offline, otherwise use a sandbox like REPL.IT, etc. There you can select Python 2 or Python 3 IDE.

If you open up the Shell, you can then access the editor through the File menu > New File.

I always save the file right away so I can set up the directory where it will be stored. That way, it doesn’t accidentally get stored in the Python system folder.


#3

On codecademy, the screen on the middle should represent the text editor and the screen on the right, the output.
To write some code in Python you’ll need to open a text editor and type your code in there. Microsoft Windows comes with notepad preinstalled. From the screen shot I can see that you’re using Mac OS. I don’t know what software it comes preinstalled with, but I can see in your file manager that you’ve IDLE installed. You can use it to write some code. When done, save your code. You should put a .py at the end of the filename. UNIX and UNIX-like operating systems don’t really care about the file name, but you should do that anyways.
Now, open a command prompt(search for terminal). And run(I’m assuming that you saved your code under /Users/john/Documents/my_code.py):

python /Users/john/Documents/my_code.py

Now the terminal should display an error message or what you intended your program to output.

There are some differences between python 2 and 3 some examples are:
When calling print you need to put the parameters within brackets(or however those ()s are called) in Python 3. Like a function(because that’s also what print is):
Python 2:

>>> print "Hello!"
Hello!
>>> print("Hello!")
Hello!

Python3:

>>> print "Hello!"
  File "<stdin>", line 1
    print "Hello!"
                 ^
SyntaxError: Missing parentheses in call to 'print'
>>> print("Hello!")
Hello!

Divisions between integers in Python 2 will always return an integer, while on python3, they will return a float:
Python2:

>>> 6/2
3
>>> 5/2
2

Python 3:

>>> 6/3
2.0
>>> 5/2
2.5

On Python 3 raw_input() was replaced with input() and the Python 2’s version of input() doesn’t exist anymore(if there’s one I would see no reason to use it).
Python 3:

>>> my_input = raw_input("Gimme something: ")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'raw_input' is not defined
>>> my_input = input("Gimme something: ")
Gimme something: something
>>> my_input
'something'
>>> my_input = input("Gimme something: ")
Gimme something: "something"
>>> my_input
'"something"'

Python 2:

>>> my_input = raw_input("Gimme something: ")
Gimme something: something
>>> my_input
'something'
>>> my_input = input("Gimme something: ")
Gimme something: something
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'something' is not defined
>>> my_input = input("Gimme something: ")
Gimme something: "something"
>>> my_input
'something'

There are other differences. These I mentioned should just give you an idea on how the different versions differ.

I said that UNIX an UNIX-like operating systems do not care about file names. With that I mean that you could call your file my_code instead of my_code.py and it would still run even when the program to read the file isn’t specified as long as you take a extra measure: adding a shebang.
Put this at the top of the file:

#!/usr/bin/python

This specifies the path of the program to read it(in this case the python interpreter). It is also useful to specify with which version it should be run. For example if you wanted that when someone double-clicked the file on linux, freebsd, mac os, whatever it should be run with Python 3 instead of 2 you could do the following:

#!/usr/bin/python3

So, summarizing, if someone executed this:

./my_code.py

on a command prompt. The program to run it would be chosen according to the shebang.

Windows looks at the file ending when a program isn’t specified, I think. Windows knows that the Python interpreter should be used when running .py files unless otherwise stated by the user.

So, when writing a file that is meant to be run directly(not just to be imported by another program) you should add a shebang and the .py ending to the file to guarantee a good user experience for most cases.