ISSN 2567-6458, July 10, 2019
Author: Gerd Doeben-Henisch
This is the next step in the python3 programming project. The overall context is still the python Co-Learning project.
After a first clearing of the environment for python programming we will now focus a little bit more on the structure of the python programming language. Generally python is not different to any other programming language: there are different kinds of objects and different kinds of operations with these objects, and a certain order how to proceed.
To be able to write code it is not necessary to do philosophy. But as you will see shortly (and hopefully) it can add some more helpful knowledge by widening your view of what you are doing.
What you want to do while programming is to write some lines of code in the python language to enable some computing process.
Usually you will do this because you have some model in your head which you want to translate (encode, implement) in a python script which can be processed by the python interpreter who talks with the computing machinery.
The computing machinery is some real machine which matches the general (mathematical = philosophical) concept of an automaton called Turing machine.
From a Turing machine we know that one has to distinguish between input values which can be read in and being processed according to a finite set of computable rules and output values of these computations. The output values can also be understood as stored values, which can be read again. Input and output values can have an explicit address of some location where they are stored and a content (= value) at this location. Thus the processing rules can select an address and either read the associated value or write a value into the associated location.
Within this mathematical framework of a Turing machine the values represent objects and the computable rules represent possible operations with these objects.
It is possible to combine elementary values located in individual addresses to more complex values and to combine individual computable rules to more complex operations.
In the first moment this description of a Turing machine can appear as to be too simple to be useful for anything interesting, but as the history of Logic, of Mathematics as well as Computer Science has revealed to us, this simple concept can do anything what we can think of to be computable (a complete different story is that of quantum computers. It has still to be clarified whether the definition of ‘quantum computer’ is compatible with that of a Turing machine).
Taking the journey from a Turing machine to the python programming paradigm as defined in the language then we find a rich set of different types of values as well as a rich set of different types of operations (methods), often object specific.
Let us have a first look to the value (object) types numbers, strings, and sets.
Remark: the following information about numbers you get directly from the python manuals, which you can find associated with the entry for python 3.7.3 if you press the Windows-Button, look to the list of Apps (= programs), and identify the entry for python 3.7.3. If you open the python entry by clicking you see the sub-entry python 3.7.3 Manuals. If you click on this sub-entry the python documentation will open. In this documentation you can find nearly everything you will need. For Beginners you even find a nice tutorial.
VALUES (OBJECTS) AS NUMBERS
In our everyday world numbers occur usually as certain symbolic expressions like ’99’, ‘-62’, ‘6.23’, ‘2.3*10^4’ etc. From Mathematics we have learned that there are different kinds of numbers defined which obey different kinds of rules.
Thus we have in everyday life integers, rational numbers, real numbers, irrational numbers, complex numbers to mention the most common types.
While numbers as such have no special meaning they can be used to quantify certain properties like temperature, weight, length, clock-time, velocity, etc.
In python there are three distinct numeric types built in: integers, floating point numbers, and complex numbers. Integers have internally an unlimited precision. Example:
Information about the precision and internal representation of floating point numbers for the machine on which your program is running is available in
>>> import sys
sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)
Complex numbers have a real and imaginary part, which are each a floating point number. To extract these parts from a complex number z, use
There are additional numeric types like fractions and decimals that hold floating-point numbers with user-definable precision.
The different operations with these numeric types are the following ones:
A population p=1200 citizens increases by incoming people migPlus=500 to 12500.
With a birth rate br=0.015 and a death rate of dr=0.018 the population will change in a year like
If one would assume that the birth br and death dr rates are linearly distributed over the year one could compute some average per month like:
The floor operator ‘//’ turns a float number into the next integer which is smaller:
The remainder operator ‘%’ delivers the ‘rest’ of a division instead of the fraction as in a usual division:
The ‘abs()’ operator abstracts from possible negative signs:
The ‘int()’ operator turns a float number into an integer:
And the float operator ‘float()’ turns an integer into a float:
With the power operator ‘pow(x,y)’ one can raise x to the power of y:
Alternatively one can use the expression x**y: