What is Python?
Python is a generic, high level, loosely typed, interpreted programming language.
Firstly, Python is a generic language, which means that it is able to do any things you want. There are some languages that are designed for particular purpose or specialised at particular tasks, such as Matlab for matrix computation and R for statistical analysis. Contrastingly, Python is not limited to a particular filed, it is widely used in many areas, website backend, scientific research, artificial intelligence, etc..
Furthermore, it is a high level language. We don't need to care about low level things such as memory management, which is different from a low level language (e.g. C programming language).
Thirdly, Python is loosely typed, which makes it very flexible. (We'll explore more details about this in later tutorials). In this article, let's focus on the last characteristic: python is an interpreted language.
Interpreted vs Compiled
Traditionally, programming languages are compiled. For a compiled language (e.g. C/C++, Go), we first write the code, and then compile it with a compiler, a special software that converts human readable code into machine understandable commands (CPU instructions), resulting a binary executable file (typically with an .exe extension on Windows OS).
There are several other Python interpreters as well, such as Jython in Java, IronPython in .NET, and PyPy in Python! (i.e., using Python itself to run Python, this sounds really incredible, doesn't it?) But since we are beginners, let's forget the others first, use the default, official and popular CPython. Since it's the default one, we usually just call it Python (without C) interpreter.
To better understand the differences between a compiled language and an interpreted language, let's consider the programs calculating the sum of 1+2+...+99+100, in C and Python.
In C, we have to write a text file (sum.c) first, and then compile it into an executable (sum.exe) with GCC (GNU Compiler Collection), a popular C compiler. The executable (sum.exe) is a binary application file, which can run on any computers directly, without the original source file (sum.c).
While in Python, we just need to write the code (sum.py), and use an interpreter (python3) to run the file. And the interpreter python3 is just an application written in C. In other words, the python program relies on an interpreter (or a Python environment) to run on a computer.
Python 2 or Python 3?
The next thing is we need to know is about Python version. Python language evolves, just like English language changes over time. Currently, there are two supported versions, Python 2 and Python 3. For newcomers, I strongly recommend to start with Python 3.
The reasons are twofold. Python 3 is the future, it brings many improvements over Python 2 which will fade away eventually. Moreover, Python 3 is easier to learn, it eliminates many quirks that can unnecessarily trip up beginning programmers trying to learn Python 2.
Hence, begin with Python 3, and learn Python 2 afterwards only if you need to read code written in Python 2 (but the chance that you'll encounter Python 2 code is slim).
Set up a Python environment
Before start writing Python code, we need to prepare the Python environment, i.e., to install the Python 3 interpreter. For those who are not comfortable with command-line interfaces, the recommended one-stop solution is to use Anaconda 4.2 for Python 3.
Anaconda bundled the official CPython interpreter, and many useful third-party Python libs. It also includes an IDE called Spyder to help writing Python code.
Another way is to use PythonAnyWhere, an online Python environment, in which you can write and execute code in a browser, without installing Python on your own computer. You can check out this page by the author of 'Think Python 2e' to learn how to use it.
For Windows users, I recommend to use Anaconda. For Mac users, there is a third solution, to use the terminal as shown in the above screenshot:
1. Open the Terminal from Launchpad --> Others, and keep it to your dock.
2. Install homebrew (a package manager for macOS), by copying the follow command to the terminal:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
3. After that, install Python 3 using homebrew:
brew install python3
4. type python3 some-python-program.py to execute your code, as shown in the above screenshot: python3 sum.py
5. Install a text editor or IDE to help writing Python code. You can use Sumlime Text, a simple editor, or PyCharm, a powerful Python IDE.
Program, Software, Application (App): In computer science, these three words are often used interchangeably, but there is a technical difference. A 'Program' is some instructions telling a computer what to do, so it is more about low level things. A 'Software' can be made up of more than one program, and is often used in contrast to hardware. An 'Application' is a program that is designed for the end user, i.e., it requires user interaction. And 'App' typically refers to the Application runs on web (web app), and in phone (mobile app).
Code, Source, Executable: 'Code' is the human readable text in a program 'Source' file. The 'Source' file can be compiled into an 'Executable' file, in order to run on computers.