STARTING WITH PYTHON3 – The very beginning – part 3

Journal: uffmm.org,
ISSN 2567-6458, July 10, 2019
Email: info@uffmm.org
Author: Gerd Doeben-Henisch
Email:
gerd@doeben-henisch.de

CONTEXT

This is the next step in the python3 programming project. The overall context is still the python Co-Learning project.

SUBJECT

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.

PHILOSOPHICAL CONSIDERATIONS

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:

>>> print(23**23)

20880467999847912034355032910567

>>> print(234**23)

3104307401943398225947002752118451297846365869366575104

>>> print(2345**33)

1639509557347018492896272198387903513810572353466749818375721601077990329493344735781232477165758609771728515625

Information about the precision and internal representation of floating point numbers for the machine on which your program is running is available in sys.float_info:

>>> import sys

>>> print(sys.float_info)

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 z.real and z.imag.

>>> z=complex(2.33,-2)

>>> z

(2.33-2j)

>>> z.real

2.33

>>> z.imag

-2.0

>>> z=complex(2.33,-2)

>>> z

(2.33-2j)

>>> z.real

2.33

>>> z.imag

-2.0

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.

>>> p=12000

>>> migrPlus=500

>>> pnew=p+migrPlus

>>> pnew

12500

With a birth rate br=0.015 and a death rate of dr=0.018 the population will change in a year like

>>> p=12500

>>> br=0.015

>>> dr=0.018

>>> pnew=p+(p*br)-(p*dr)

>>> pnew

12462.5

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:

>>> (p*br)/12

15.625

>>> (p*dr)/12

18.749999999999996

The floor operator ‘//’ turns a float number into the next integer which is smaller:

>>> (p*br)/12

15.625

>>> (p*br)//12

15.0

>>> (p*dr)/12

18.749999999999996

>>> (p*dr)//12

18.0

>>>

The remainder operator ‘%’ delivers the ‘rest’ of a division instead of the fraction as in a usual division:

>>> 12/8

1.5

>>> 12%8

4

The ‘abs()’ operator abstracts from possible negative signs:

>>> abs(-7)

7

>>> abs(-7.2)

7.2

The ‘int()’ operator turns a float number into an integer:

>>> int(7.2)

7

>>> int(-7.2)

-7

And the float operator ‘float()’ turns an integer into a float:

>>> float(7)

7.0

>>> float(-7)

-7.0

With the power operator ‘pow(x,y)’ one can raise x to the power of y:

>>> pow(2,3)

8

>>> pow(2,5)

32

>>> pow(3,3)

27

Alternatively one can use the expression x**y:

>>> 2**3

8

>>> 2**5

32

>>> 3**3
27