Python is a very popular interpretive language for computers. Interpreted means that programs written in Python are generally not compiled, but rather are converted to machine language a line at a time on the fly, each time they are run. This has the advantage of avoiding the sometimes long and confusing process of compiling an executable file, but may be slower in execution of computationally-intensive programs. For people learning programming and for program development work, the speed of interpretation over compilation is a big win.
So, the first step is to make sure you have the Python interpreter on your computer. In Terminal:
iMac: python -V iMac: Python 2.7.10
which indicates that Python 2 is on my computer. This version is required for a number of Apple’s internal programs, so we can’t disturb this, but we really want to have the latest and greatest version which is Python 3. Let’s check now for Python 3:
iMac: python3 -V iMac: bash: python3: command not found
OK, it’s not there, so it’s time to install Python 3. In your browser, go to python.org and click on the Downloads button in its header. They are pretty smart and will detect that you have a Mac and will give you the link to download the latest Python version customized for the Mac operating system. As of today’s writing, that is python-3.6.5-macos10.6.pkg. Click this and save the pkg file. It will probably end up in your downloads folder.
Install Python 3 by opening Finder, double-clicking the pkg file in the Downloads folder and following the instructions. When you are done, confirm that it installed by going to Terminal again:
iMac: python3 -V iMac: Python 3.6.5
It’s there and available to start writing Python code. In Terminal:
iMac: python3 Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 05:52:31) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> 1 + 1 2 >>> 3 * 4 12 >>> print('Hello World!') Hello World! >>> quit() iMac:
After I started python3, it responded with version and licensing information, followed by its prompt, “>>>”
You can type python commands at the prompt and Python3 will execute them if it can. So it gave me the answers to 1 + 1 and 3 * 4, and executed the print function. In this mode, it is kind of like a big, fancy calculator which you can use to test different python statements to help you understand how they work.
To actually write a program, however, you will need to write a number of python lines of code and save them in a file. You will then tell Python to attempt to execute that program by typing something like this in Terminal:
iMac: python3 myprogram.py iMac: [python will either run the program or will give you error messages telling you why it couldn't]
Before getting to the program, though, you will need to set up a program editor, which will be the next topic.