Table of Contents
Python is a programming language developed by the Python.org team. Python also represents a piece of software called an interpreter. The interpreter is the program you’ll need to run Python code and scripts. Technically, the interpreter is a layer of software that works between your program and your computer hardware to get your code running.
Depending on the Python implementation you use, the interpreter can be:
A program written in C, like CPython, which is the core implementation of the language
A program written in Java, like Jython
A program written in Python itself, like PyPy
A program implemented in .NET, like IronPython
Or the default Python interpreter IDLE written in Python itself
The interpreter is able to run Python code in two different ways; as a script/module or as a piece of code typed into an interactive session directly at the Command Line interface (CLI) also known as the txt prompt.
Once the code is run Python then translates the code to bytecode which doesn't directly work on the CPU, but it uses a virtual machine on the operating system to independently run the python program code there.
Bytecode is program code that has been compiled from source code (In this case from human readable Python code) into low-level code (For the computer to read) designed for a software interpreter program. It may be executed by a virtual machine or further compiled into machine code, which is recognized by the Computer processor (CPU).
Whatever form the interpreter takes, the code you write will always be run by this program. Therefore, the first condition to be able to run Python code is to have the interpreter correctly installed on your system.
Installing Python - Determining Version
We recommend to install the latest stable release version of Python to date which is version 3.8. By installing the latest stable releases your ensured some community documented support of third party libraries and implementations for main product retailers such as Microsoft or MySQL database support. This enables you to start writing programs in Python code knowing their will be future security and software updates for the current version of Python, thereby future-proofing your code.
For a full list of Python versions to install see the latest up to date Python Version release table located here
To see the full documented list of supported operating systems along with official installation steps browse to the official Python documentation repository located here https://docs.python.org/3.8/using/index.html
Unlike most Unix systems and services, Windows does not include a system supported installation of Python. To make Python available, the CPython team has compiled Windows installers (MSI packages) with every release for many years. These installers are primarily intended to add a per-user installation of Python, with the core interpreter and library being used by a single user. The installer is also able to install for all users of a single machine, and a separate ZIP file is available for application-local distributions.
Mac OS Setup
Mac OS versions X 10.8 and up comes with Python 2.7 pre-installed by Apple. If you wish, you are invited to install the most recent version of Python 3 from the Python website (https://www.python.org). A current “universal binary” build of Python, which runs natively on the Mac's new Intel and legacy PPC CPU's, is available there.
To start Python on a Mac simply start the Terminal app located in all MacOS versions and type command
this will start the Python interpreter on the default Bash Terminal.
By default the version of Python 2.7 preinstalled on MacOS doesn't have any simple package management modules preinstalled. Instead you will have to install a package management solution before you can start using other modules. pip is a tool for installing and managing Python packages. To install pip at the terminal command line type
sudo easy_install pip
This command will install pip on your local machine.
Python 3.6 supports Windows Vista and newer OS such as Windows 7 and 10. For Windows XP support then please install Python version 3.4 or less.
First start by downloading the latest executable from URL https://www.python.org/downloads/
After starting the installer, one of two options may be selected:
1) For the purpose of this tutorial it's recommended to select the add python version to path checkbox and click 'Customise installation'
One thing to watch out for: During the installation, you will notice a window marked "Setup". Make sure you tick the "Add Python 3.6 to PATH" checkbox and click on "Install Now", as shown here:
Note: if you are using an older version of Windows (7, Vista, or any older version) and the Python 3.6.x installer fails with an error, you can try either:
1) install all Windows Updates and try to install Python 3.6 again; or
2) install an older version of Python, e.g., 3.4.6.
If you select “Install Now”:
- You will not need to be an administrator (unless a system update for the C Runtime Library is required or you install the Python Launcher for Windows for all users)
- Python will be installed into your user directory
- The Python Launcher for Windows will be installed according to the option at the bottom of the first page
- The standard library, test suite, launcher and pip will be installed
- If selected, the install directory will be added to your PATH
- Shortcuts will only be visible for the current user
2) Leave all checkboxes as is and click Next
3) In the 'Customise Install location' text field select browse, then in your C: drive make a folder named 'Python' then select it with OK button.
Click the 'Install' button. You may require administration privileges.
The setup will now install and give you a confirmation of successful installation you are now able to run Python in your native command line interface. If you are using a Windows computer You can start CMD (Batch) or use PowerShell to start Python. On MacOS python comes installed already and you can immediately start it on the CLI by typing 'py'.
Using Python - Interactive sessions
A widely used way to run Python code is through an interactive session. To start a Python interactive session, just open a command-line or terminal and then type in
pydepending on your Python installation, and then press the <Enter> keyboard key.
Using Python in PowerShell
By executing (running) the python command in your terminal, you are presented with an interactive Python shell. This is also known as the Python Interpreter or a REPL (for 'Read Evaluate Print Loop').
Python only works with the default PowerShell.exe shell and not the ISE. With PowerShell.exe code is run which directly works with python interactively and get the output into PowerShell without redirecting it through files. When you try to run Python in ISE, it opens a legacy console app which ISE illogically hides (even though it can't bridge your actions to that app). This is not a hard rule python can be configured in many different CLI Shells including PowerShell ISE but it is beyond the scope of this course.
To start using python in PowerShell, open up the PowerShell.exe shell. It is not required to run as an administrator to use python within PowerShell. Then type "py". You will see the screen below.
You can also import commands from the host shell (in this instance PowerShell) by using the module "import os". Once the module is loaded, you may use the os modules methods such as system() to input a host shell command such as "dir" in CMD batch which translates to the alias for get-childitem in PowerShell by typing the module.method('command'). E.g. os.system('dir') will output the current directory.
Other common GUI commands you would use would be;
exit() - to exit out of the python window and back into the underlying PowerShell interface type the exit method "exit()".
os.system('cls') - To clear the screen you would use "os.system('cls')" command.
Default Python Shell - IDLE
Once Python is installed, you will find that the default Python developed shell named IDLE is also installed onto the computer. This shell is a standalone CLI for python programming language. When starting IDLE you will notice pythons distinct light green/brown colour theme and see the output below.
When commands are read from a Host Shell CLI, the interpreter is said to be in interactive mode. In this mode it prompts for the next command with the primary prompt, usually three greater-than signs (>>>); for continuation lines it prompts with the secondary prompt, by default three dots (...). The interpreter prints a welcome message stating its version number and a copyright notice before printing the first prompt:
Python 3.6 (default, Sep 16 2015, 09:25:04)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
Python 3.6 (default, Sep 16 2015, 09:25:04)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
Visual Studio Code
An alternate better programming environment
Visual Studio code not to be confused with Visual Studio is Microsoft's solution to coding in different environments within one software. Visual Studio Code is defined as a Source Code Editor rather than an IDE like Idle or PowerShell. An IDE combines a program editor and a language environment as a convenience to the programmer. Visual Studio code uses third party modules to expand its capability to develop/debug and Control source code of multiple programming languages without the need for any other IDE software installed on the host device. Although The programming languages of the respective languages will still need to be installed on the host device if not present with the OS e.g. Python Needs to be installed with all Windows Operating System versions.
We recommend to install Visual Studio code as an alternative software to the default native IDE's installed on your OS, e.g. PowerShell for Windows, or Terminal for MacOS. Visual Studio code is the choice programming tool for many developers Out there and learning how to use it puts you one step further into the journey of programming development.
You can learn more about Visual Studio from here - https://code.visualstudio.com/docs
To learn how to setup Python within Visual Studio code, follow this documentation here - https://code.visualstudio.com/docs/python/environments
And for those that do not have access to an computer to access Python, there are many online python shells to use. The offical Python website has a python tool for use at https://www.python.org/. Simply browse to the URL and click on start Interactive session at the homepage image and a new python shell session will start.
Python installations are also supported in many popular package management softwares. One such software chocolatey can be installed onto a computer which can then be run from the CLI of the hosts default CLI application to install the version of Python requested and also download all the dependencies required to install the software if they are missing.
To use chocolatey to install python 3 follow the steps below.
Step 1) Install chocolatey https://chocolatey.org/install
Step 2) Run this command in PowerShell.exe
choco install python
Shells and Beyond
Package Management - The PyPA recommended tool for installing Python packages is PIP. To install, on your command line execute pip install
So far, we have discussed different ways to run code using Python's native interactive shell. Shells use Python's interpretive power for experimenting with code real-time. Alternative shells include IDLE GUI, IPython - known for extending the interactive experience, pycharm and third party software developing programs such as Visual Studio.
Programs - For long-term storage you can save content to .py files and edit/execute them as scripts or programs with external tools e.g. shell, IDEs (such as PyCharm), Jupyter notebooks, etc. Intermediate users may use these tools; however, the methods discussed here are sufficient for getting started.
Python tutor allows you to step through Python code so you can visualize how the program will flow, and helps you to understand where your program went wrong.
PEP8 defines guidelines for formatting Python code. Formatting code well is important so you can quickly read what the code does.
The shell Language by default, Python source files are treated as encoded in UTF-8
Verify Python Installation - Versions
Once installed you will be able to use Python from any of the shell environments that come natively installed with your OS or with Python such as IDLE.
To verify python is installed you may open the native shell on your host operating system either ;
Windows – PowerShell
MacOS – Terminal
And type either of the following command at the command line.
Either command will output the version of Python that is installed on your computer and verify that a successful installation
When running commands you will notice that some commands work with 'python' or only with 'py'
pythonis the Python executable of the Python installation which you have selected as a default during the installation. This basically put the path to that version inside the PATH, so that the executable is directly available.
pyis the Python launcher which is a utility that comes with Python installations on Windows. It gets installed into C:\Windows\ so it’s available without requiring PATH modifications. The Python launcher detects what Python versions are installed on your machine and is able to automatically delegate to the right version. By default, it will use the latest Python version that is on your machine. So if you have installed 2.7, 3.5 and 3.6, running py will launch 3.6. You can also specify a different version by doing e.g. py -3.5 to lauch 3.5, or py -2 to launch the latest Python 2 version on your machine.