Python

Table of Contents

Setup Shell

Installing Python

To see the full list of Operating systems and supported devices of Python along with official installation steps. Browse to URL https://docs.python.org/3.6/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

python


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.


Windows Setup

Python 3.6 supports Windows Vista and newer. Windows XP support then please install Python 3.4.

Download the latest executable from URL https://www.python.org/downloads/
After starting the installer, one of two options may be selected:



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”:



Proceed with the remainder of the installation steps by clicking next/OK/finish. 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.


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:

$ python3.6
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 Versions

To get started with a simple command and to test the shell output is working, we will import the sys module and query the attribute version to determine if we have the latest Python3 installed and configured. Type the below and follow along. TO determine if the correct version is installed you will see the first digit or version string is above 3.


>>> import sys
>>> sys.version
'3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)]'



Online Shell

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.




Chocolatey

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 . For instance, pip install numpy. (Note: On windows you must add pip to your PATH environment variables. To avoid this, use python -m 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

Visit next page to learn about - Get Help