Python 3

Introduction

Python is a general propose programming language and is more associated with system admins and data science, however it offer dynamic and object oriented features and is cross-platform, you can run in multiple environments such as Linux, Mac and Windows. It's genenrally used in the same place other interpretered languages like Perl and Ruby, but over the last few years it seems to have grown more popular.

Python is a interpreted programming language which means its a type of programming language for which most of its implementations execute instructions directly and freely, without previously compiling a program into machine-language instructions. Python is a dynamically-typed language in contrast Java is a statically-typed language. In a weakly typed language, variables can be implicitly coerced to unrelated types, whereas in a strongly typed language they cannot, and an explicit conversion is required

Python has a number of implementation, which I briefly describe below:

The examples and code i will be demostrating, I will be using CPython (often just called Python)

There are two main versions, version 2 and version 3 and you may find both versions in the wild, there are many differences between the two and will leave you to the web for more answers, I will be covering the newer version 3 (I will be using 3.8.2).

Lastly Python has many extension modules that are freely available, if you go to pypi there are many thousands of modules, projects, etc that you can import into your own projects, also they are now many onlines courses for Python.

Installation and Setup

I am not going into details on how to setup Python as this can change but summarize each O/S below:

I just want to touch on IDE's, some perfer to use a plain text editor like sublime or visual code other like a bit more powerful IDE like PyCharm or Spyder, depending on your needs or what you company uses my dictate what you use, you don't have to use a professional IDE to code in Python.

Below is Pycharm by JetBrains

If you type python on a commandline you will be placed into an interactive session and it will prompt you to enter python statements or expressions, this can be useful to explore and check things out quickly, you can see the windows python interactive shell below

Python Interpreter and Commandline options

The Python interpreter is run as python (python.exe on windows), it includes both the interpreter and compiler, there are a number of environment variables that are associated with Python

PYTHONHOME The Python installation directory. A lib subdirectory, containing the standard Python library, must exist under this directory.
PYTHONPATH A list of directories, separated by colons on Unix-like systems, and by semicolons on Windows. Python can import modules from these directories.
PYTHONSTARTUP The name of a Python source file to run each time an interactive interpreter session starts. No such file runs if this variable is not set or if it is set to the path of a file that is not found. The PYTHONSTARTUP file does not run when you run a Python script; it runs only when you start an interactive session.

Python also has many commandline options that can be used, in the table are the most common but see the Python documentation for more details, Python 3.8.2 documentation.

Option Meaning
-B Don’t save compiled bytecode files to disk
-c Specifies Python statements as part of the command line
-E Ignores all environment variables
-h Prints a full list of options and summary help, then terminates
-i Runs an interactive session after the file or command runs
-m Specifies a Python module or package to run as the main script
-O Optimizes generated bytecode (PYTHONOPTIMIZE)—note that this is an uppercase letter O, not the digit 0
-OO Like -O, but also removes documentation strings from the bytecode
-Q arg Controls the behavior of division operator / on integers (v2 only)
-S Omits the implicit import site on startup
-t Issues warnings about inconsistent tab usage (-tt issues errors)
-u Uses unbuffered binary files for standard output and standard error
-v Verbosely traces module import and cleanup actions
-V Prints the Python version number, then terminates
-W Prints the Python version number, then terminates
-x Excludes (skips) the first line of the main script’s source

Once Python has been installed its very easy to run a Python program, depending on what operating system or if your IDE has the capability to execute the program, in Linux environments you use the shebang as the first line of your script (see examples below), this tells the shell where to find the intepreter (Python, Perl, PHP, etc). Windows you can simply double click on the script and providing the file extension has been setup to execute python.exe command, the script will be executed.

shebang line
#!/usr/bin/env python
#!/usr/bin/python -0             # you can pass commandline options to script