Part I
Snake in the GrassPython and Its Environment
Springer International Publishing AG 2017
Iain Gray Snake Charming - The Musical Python
1. Installing Python
Iain Gray 1
(1)
AFIMAAssociate Fellow of the Institute of Mathematics and its Applications, Southend-on-Sea, UK
Latet anguis in herba Virgil, Eclogues III.
Python is a simple and elegant language that is easy to learn and install on any platform. The language is cross-platform and truly multi-paradigm embodying functional, imperative and object-oriented features. A simple development environment IDLE is also installed with Python allowing interactive programming and syntax aware code editing. Sophisticated package managers allow easy access to pre-made scientific and graphics libraries amongst many others.
On visiting the Python homepage https://www.python.org you will see.
Clicking on the Downloads tab followed by All releases in the left hand sidebar you will then see
so click on Download Python 3.5.2 to have a graphical installer loaded on your machine. Open this installer by double clicking on it and that is alllight blue touch paper and retire! The homepage identifies your target architecture and will install the relevant version of Python, and as the language is machine independent Python source code will run on any supported architecture. Note that Python 3.5.2 was the latest version at time of press but the installation procedure above will always obtain the latest version.
Finally clicking on the Documentation tab followed by Docs in the left hand sidebar will bring you to the documents page
from where you can access tutorials and the main language references online.
Springer International Publishing AG 2017
Iain Gray Snake Charming - The Musical Python
2. The Python ShellIDLE
Iain Gray 1
(1)
AFIMAAssociate Fellow of the Institute of Mathematics and its Applications, Southend-on-Sea, UK
IDLE, the Integrated Development and Learning Environment, is a shell that comes bundled with the Python distribution on download.
Locate the Python 3.5 folder, open it, find IDLE and open it seeing a large empty window headed by
the final
prompt is where you can enter code interactively for the Python interpreter.
If on any platform you see the following warning message
it is safe to ignore this and type < return > or < enter > carrying on with the rest of the chapter. You will only be using Tcl/Tk within the spyder environment of Chap. and scroll down to
from where you can download the latest 8.5 version to install for your platform.
2.1 Basic Python Syntax
Python itself has an official tutorial accessible as described in Chap.. The aim here is slightly less lofty and is intended as a get you started subset of Python. More advanced concepts such as functions or datatypes such as lists will be introduced in the projects as and when required.
2.1.1 Comments
Comments to be ignored by the interpreter are introduced by the
(hash) character (< option > or < alt
on many keyboards). Multi-line comments can be delimited (before and after) by (three single quotes).
2.1.2 Indentation and Block Structure
Python is block structured but uses code indentation, rather begin...end, to delimit related blocks of code.
2.1.3 Input and Output
The most basic functions are input() and print().
2.1.4 Declaration of Simple Types and Type Casting
Only the numeric type of int and float will be used here: an int holds an integer value and has no decimal point: a float hold a floating point value and has a decimal point. The two types have different internal representations. Before being used an alphanumeric variable must be declared to be of a particular type by
assigning it to a constant as in a = 0.0
making multiple assignments as in a, b, c = 0.0, 1, 2
assigning it to the value of an expression as in a = b/c, where b and c have previously been declared.
Variables of one type may be cast as the other by using the int() and float() functions.
2.1.5 Arithmetic Operators and Precedence
The basic arithmetic operators are
supplemented by ** for exponentiation, // for floor (integer) division and
for remainder. Parsing of arithmetic expressions is carried out left to right obeying the following order of precedence
(highest) ** and unary minus but see note below
(lowest)
Note that ** is higher than left unary minus, ** is lower than a right unary minus in terms of precedence.
This order of precedence may be changed by using parentheses ().
2.1.6 Conditional Expressions, Relational and Logical Operators
Python has a cast of all the usual suspects for its relational operators, which are
equals. Note that a single
is only used for declaration or assignment