Example python3: popShow0 – simple file-reader

eJournal: uffmm.org,
ISSN 2567-6458, 6.April 2019
Email: info@uffmm.org
Author: Gerd Doeben-Henisch
Email: gerd@doeben-henisch.de

CONTEXT

This is a possible 5th step in the overall topic ‘Co-Learning python3′. After downloading WinPython and activating the integrated editor ‘spyder’ (see here),  one can edit another simple program dealing with population dynamics in a most simple way (see the source code below under the title ‘EXAMPLE: popShow0.py’). This program is a continuation of the program pop0e.py, which has been described here.

COMMENTS

In this post I comment only on the changes between the actual program and the version before.

IMPORT

In this program the following two libraries are used:

import numpy as np # Lib for math
from tkinter.filedialog import askopenfilename

numpy is known from previous programs while tkinter is here used to enable a file dialog to find a certain file while browing the different folders.

The program opens such a windows for browsing:

print(‘A window is asking you for a filename\n’)
infilename = askopenfilename()

The variable ‘infilename’ is a variable for strings, which in this case saves the content of the file.  This content looks like this:

# br=0.03,dr=0.019
# X-Column, Y-Column
1.000000000000000000e+00   7.466964000000000000e+06
2.000000000000000000e+00   7.549100604000000283e+06
3.000000000000000000e+00   7.632140710644000210e+06
4.000000000000000000e+00   7.716094258461084217e+06
5.000000000000000000e+00   7.800971295304155909e+06
6.000000000000000000e+00   7.886781979552501813e+06
7.000000000000000000e+00   7.973536581327578984e+06
8.000000000000000000e+00   8.061245483722181991e+06
9.000000000000000000e+00   8.149919184043126181e+06
1.000000000000000000e+01    8.239568295067600906e+06
1.100000000000000000e+01    8.330203546313344501e+06
1.200000000000000000e+01    8.421835785322790965e+06
1.300000000000000000e+01    8.514475978961341083e+06
1.400000000000000000e+01    8.608135214729916304e+06
1.500000000000000000e+01    8.702824702091945335e+06
1.600000000000000000e+01    8.798555773814957589e+06

This context will then be formatted by the following lines:

data = np.loadtxt(infilename)
x = data[:, 0]
y = data[:, 1]

The leading header will automatically be discarded and the main content will be stored in two columns. These formatted two-columns data will then be printed with:

for i in range(len(x)):
print(‘Year %d = Citizens. %9.0f \n’ % (x[i],y[i]))

For each of the values to print x[i] and y[i] there are formatting options telling that the x[i] represents a ‘year’ understood as an integer, and  that y[i] represents the population number Citizens understood as a floting point number with zero signs behind the floating point.

The output looks then like this (Date are from the UN for 2016; the simulation computes this into a possible future):

Year 1 = Citizens. 7466964

Year 2 = Citizens. 7549101

Year 3 = Citizens. 7632141

Year 4 = Citizens. 7716094

Year 5 = Citizens. 7800971

Year 6 = Citizens. 7886782

Year 7 = Citizens. 7973537

Year 8 = Citizens. 8061245

Year 9 = Citizens. 8149919

Year 10 = Citizens. 8239568

Year 11 = Citizens. 8330204

Year 12 = Citizens. 8421836

Year 13 = Citizens. 8514476

Year 14 = Citizens. 8608135

Year 15 = Citizens. 8702825

Year 16 = Citizens. 8798556

What is missing here is the information about the ‘real years’ as 1 = 2016 etc.

SOURCE CODE for popShow0.py

popShow0.py as popShow0.pdf

Example python3: pop0e – simple population program

eJournal: uffmm.org,
ISSN 2567-6458, 4-6.April 2019
Email: info@uffmm.org
Author: Gerd Doeben-Henisch
Email: gerd@doeben-henisch.de

CONTEXT

This is a possible 4th step in the overall topic ‘Co-Learning python3′. After downloading WinPython and activating the integrated editor ‘spyder’ (see here),  one can edit another simple program dealing with population dynamics in a most simple way (see the source code below under the title ‘EXAMPLE: pop0e.py’). This program is a continuation of the program pop0d.py, which has been described here.

COMMENTS

In this post I comment only on the changes between the actual program and the version before.

IMPORTS

In the new version one more liibrary is used for the handling of time stamps:

import time # Lib for time

STORING DATA IN A FILE

The only extension in the new version of the small program are some lines enabling the storage of the data from the simulation in a file.

data = np.column_stack((x,pop))

This line is formating the plot-values as x and x axes written as two columns bedides each other in a file.

What comes next is a construction of a file name which includes the actual time as well as the values of the br and the dr variable:

ts = time.gmtime()
t=time.strftime(“%c”, ts) # format time data into ISO format
t=t.replace(‘ ‘,’-‘)
t=t.replace(‘:’,’-‘)
header=’br=’+str(br)+’,’+’dr=’+str(dr)+’\n’+’X-Column, Y-Column’
fname=’hxyPTL’+t+’br=’+str(br)+’-‘+’dr=’+str(dr)

After this construction a file is generated with the plotting data as well as an expressive name.

np.savetxt(fname+’.txt’, data,header=header)

SOURCE CODE

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
“””
Created on Thu 4-6 April Konga (Sweden) 2019

@author: gerd doeben-henisch
Email: gerd@doeben-henisch.de
“””

##################################
# pop0e()
###################################
#
# IDEA
#
# Simple program to compute the increase/ decrease of a population with
# the parameters population number (p), birth-rate (br) and death-rate (dr).
# In this version an extension with the following features:
# – a loop to repeat the computation for n-many cycles
# – a storage of the data in an array
# – an additional automatic storage of the plotting data in a file
# with the actual time in the file-name
# – a plot of the stored data for n-many cycles
# – the overall change of the population in %

#########################################################
# IMPORTS
# As part of the distribution of Winpython there are already many
# libraries pre-installed, which can be activated by the import command.
# Other libraries outside of the distribution have to be downloaded
# with the pip command
# Lib for plotting

import matplotlib.pyplot as plt # Lib for plotting
import numpy as np # Lib for math
import time # Lib for time

###########################################################
# DEFINITION

def pop0(p,br,dr):

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

return p

###################################
# INPUT OF DATA

p = int(input(‘Population number ? ‘))

br = float(input(‘Birthrate in % ? ‘))
br = br/100

dr = float(input(‘Deathrate in % ? ‘))
dr = dr/100

n = int(input(‘How many cycles ? ‘))

baseYear = int(input(‘What is your Base Year ? ‘))

#############################################
# GLOBAL VARIABLES

pop = [] # storage for the pop-numbers for plotting
pop.append(p)

#################################################
# COMPUTE

for i in range(n):
p=pop0(p,br,dr)
pop.append(p)

##################################################
# SHOW RESULTS

for i in range(n+1):
print(‘Year %5d = Citizens. %8d \n’ %(baseYear+i, pop[i]) )

x = np.linspace(1,len(pop),len(pop))

plt.plot(x, pop, ‘bo’)
plt.show()

#####################################################
# STORE VALUES ON DISK
#
# For this see the online article
# https://www.pythonforthelab.com/blog/introduction-to-storing-data-in-files/
#
# Saves the plot data automatically in a file with a header and two columns

data = np.column_stack((x,pop))
ts = time.gmtime()
t=time.strftime(“%c”, ts) # format time data into ISO format
t=t.replace(‘ ‘,’-‘)
t=t.replace(‘:’,’-‘)
header=’br=’+str(br)+’,’+’dr=’+str(dr)+’\n’+’X-Column, Y-Column’
fname=’hxyPTL’+t+’br=’+str(br)+’-‘+’dr=’+str(dr)
np.savetxt(fname+’.txt’, data,header=header)

###########################################
# Compute Change of POP

n1=pop[0]
n2=pop[len(pop)-1]
Increase=(n2-n1)/(n1/100)

print(“From Year %5d, until Year %5d, a change of %2.2f percent \n” % (baseYear, baseYear+n,Increase) )

plt.close()

####################################################
# REAL DATA
#
# UN Demographic Yearbook 2017
# https://unstats.un.org/unsd/demographic-social/products/dyb/dybsets/2017.pdf
#
# Basic Tables UN
# https://unstats.un.org/unsd/demographic-social/products/vitstats/seratab1.pdf
#
# UN public tables
# http://data.un.org/Explorer.aspx?d=POP
#
# UN Rate of population change
# http://data.un.org/Data.aspx?d=PopDiv&f=variableID%3a47
# https://www.un.org/en/development/desa/population/index.asp
”’
Population number ? 6958169

Birthrate in % ? 1.9

Deathrate in % ? 0.77

How many cycles ? 15

What is your Base Year ? 2010
Year 2010 = Citizens. 6958169

Year 2011 = Citizens. 7036796

Year 2012 = Citizens. 7116312

Year 2013 = Citizens. 7196726

Year 2014 = Citizens. 7278049

Year 2015 = Citizens. 7360291

Year 2016 = Citizens. 7443462

Year 2017 = Citizens. 7527573

Year 2018 = Citizens. 7612635

Year 2019 = Citizens. 7698658

Year 2020 = Citizens. 7785653

Year 2021 = Citizens. 7873630

Year 2022 = Citizens. 7962602

Year 2023 = Citizens. 8052580

Year 2024 = Citizens. 8143574

Year 2025 = Citizens. 8235596

From Year 2010, until Year 2025, a change of 18.36 percent

Real UN data for 2010 – 2015
2010 2011 2012 2013 2014 2015
6 958 169 7 043 009 7 128 177 7 213 426 7 298 453 7.383.009

########################################################
# STORING DATA
#
# https://www.pythonforthelab.com/blog/introduction-to-storing-data-in-files/
#
# Example of saved file:
file name:
hxyPTLSun-Apr–7-08-55-46-2019br=0.019-dr=0.0077.txt

# br=0.019,dr=0.0077
# X-Column, Y-Column
1.000000000000000000e+00 6.958169000000000000e+06
2.000000000000000000e+00 7.036796309700000100e+06
3.000000000000000000e+00 7.116312107999609783e+06
4.000000000000000000e+00 7.196726434820005670e+06
5.000000000000000000e+00 7.278049443533471785e+06
6.000000000000000000e+00 7.360291402245399542e+06
7.000000000000000000e+00 7.443462695090772584e+06
8.000000000000000000e+00 7.527573823545298539e+06
9.000000000000000000e+00 7.612635407751359977e+06
1.000000000000000000e+01 7.698658187858950347e+06
1.100000000000000000e+01 7.785653025381756946e+06
1.200000000000000000e+01 7.873630904568570666e+06
1.300000000000000000e+01 7.962602933790194802e+06
1.400000000000000000e+01 8.052580346942024305e+06
1.500000000000000000e+01 8.143574504862469621e+06
1.600000000000000000e+01 8.235596896767416038e+06
”’

 

 

Example python3: pop0d – simple population program

eJournal: uffmm.org,
ISSN 2567-6458, 2-4.April 2019
Email: info@uffmm.org
Author: Gerd Doeben-Henisch
Email: gerd@doeben-henisch.de

CONTEXT

This is a possible 3rd step in the overall topic ‘Co-Learning python3′. After downloading WinPython and activating the integrated editor ‘spyder’ (see here),  one can edit another simple program dealing with population dynamics in a most simple way (see the source code below under the title ‘EXAMPLE: pop0d.py’). This program is a continuation of the program pop0.py, which has been described here.

COMMENTS

In this post I comment only on the changes between the actual program and the version before.

IMPORTS

In the new version two libraries are used:

import matplotlib.pyplot as plt # Lib for plotting
import numpy as np # Lib for math

This extends the set of possible functions by functions for plotting and some more math.

MORE INPUT DATA

In this version one can enter a base year, thus allowing a direct relation to real year numbers in the history or the future. In the example run at the end of the program I am using the official population numbers of the UN for the world population in the years 2016 and 2017, which will hypothetically be forecasted for 15 years.

MORE GLOBAL VARIABLES

As explained in the previous version there are local variables restricted in their meaning to a certain function and global variables outside a function. In this version a datastructure called pop is introduced to store information in a sequential order. In this case the first population number given in the variable p is stored on the first position by the append operation which is part of the data structure pop.

pop = [] # storage for the pop-numbers for plotting
pop.append(p)

Later a data structure x is needed  a s a sequence of consecutive numbers starting with 1, ending with the number of entries in the pop data strucure and with as many positions as pop has entries. The number of elements in pop can be computed by applying the len() operator to pop.

x = np.linspace(1,len(pop),len(pop))

EXTENDED COMPUTATION

The computation has extended a little bit by the new line appending the actual value of p to the pop storage:

for i in range(n):
       p=pop0(p,br,dr)
      pop.append(p)

Thus the pop data structure stores every new population value in p in the sequential order of its occurence. Moreover it has been here realized a loop with the for function. The variable i is running through a sequence of values provided by the range() function. This function builts an array of numbers from 0 to n-1 and repeats therefor the call of pop0() n times.

But the most important point here is that the value of the global variable p is handed over to the function pop0(), and the local value of the variable p inside the pop0() function is again handed over by the return command to the outside of the function. Outside there is waiting the global p variable which is receiving the new value by the =-operation. In the next call of pop0() pop0() receives as new input the global variable p with the new value.

SHOWING THE RESULTS

There are now three different data show actions: (i) the list of all years with their numbers, (ii) a graphical plot of data points, and (iii) a print out of the increase of the population compared the final result with the base year.

LIST OF ALL YEARS

for i in range(n+1):
print(‘Year %5d = Citizens. %8d \n’ %(baseYear+i, pop[i]) )

Again is the for function in action ranging through the number of cycles given by the variable n. The number of the population in the different years is catched from the pop data structure by indexing the individual elements of pop by the bracket command []. pop[i] represents the i-th element of pop.

PLOTING THE POPULATION VALUES

x = np.linspace(1,len(pop),len(pop))
plt.plot(x, pop, ‘bo’)
plt.show()

The plotfunction plot of the library plt needs an arry of numbers for the x-axis given here by the x data structure, an arry of numbers for the y-axis given here by the pop-data structures, and optionally some parameter for the format of the plot symbolds. Here with ‘bo’ a black small circle. After all values are prepared will the command plt.show() make the plotted data visible.

Plot with the UN data for the possible growth of the world population
Plot with the UN data for the possible growth of the world population

OVERALL PERCENTAGE OF CHANGE

n1=pop[0]
n2=pop[len(pop)-1]
Increase=(n2-n1)/(n1/100)

print(“From Year %5d, until Year %5d, a change of %2.2f percent \n” % (baseYear, baseYear+n,Increase) )

Getting the base year from pop[0] and the last year from pop[len(pop)-1] one can compute the difference as the increase translated into a percentage.

REAL DATA

Although the population program is still very simple it is usefull to compute real numbers of the real world. One example are the official data of the United Nations (UN) which are collecting world wide data since their foundation 1948.

But, as a first surprise, although the UN provides lots of data from all the countries world wide, they do not systematicall a birthrate (br) or a deathrate (dr). Thus I have compiled the br and dr by inferring it from absolute population numbers from 2016 and 2017 combined with the fertility rate for 1000 people in the year 2013 averaged over all countries. This gives an estimate of 3% for the birthrate br. Using this from 2016 to 2017 this gives an ‘overshoot’ to the real numbers of 2017. I inferred from this the deathrate dr which is clearly a very week inference. Nevertheless it works for 2016 to 2017 and gives a first simple example for the upcoming years.

SOURCE CODE OF pop0d.py

pop0d.py as pop0d.pdf

Example python3: pop0 – simple population program

eJournal: uffmm.org,
ISSN 2567-6458, 2-3.April 2019
Email: info@uffmm.org
Author: Gerd Doeben-Henisch
Email: gerd@doeben-henisch.de

CONTEXT

This is a possible 2nd step in the overall topic ‘Co-Learning python3′. After downloading WinPython and activating the integrated editor ‘spyder’ (see here),  one can edit a first simple program dealing with population dynamics in a most simple way (see the source code below under the title ‘EXAMPLE: pop0.py’.

BASIC PROGRAMMING ELEMENTS

SOURCE FILE

The source code is stored under the name ‘pop0.py’. It is a ‘stand alone’ program not making use of any kind of a library except the built in functions of python3. The external libraries can be included by the ‘import command’.

FUNCTION DEFINITION

If one wants to use the built-in functions in some new way one can do this by telling the computer the keyword ‘def‘ which states that the following text defines a new function.

A function has always a name, some input arguments between rounded brackets followed by a colon marking the beginning of the function-body.  After the colon follows a list of built-in commands or some already defined functions. With defined functions one can make life much easier. Instead of repeating all the commands of the function-body again and again one can limit the writing to the call of the function name with its input arguments.

In the example file we have the following function definition:

def pop0(p,br,dr):
       p=p+(p*br)-(p*dr)
       return p

The name is pop0, the input arguments are (p,br,dr), the used built-in functions are +, -, *, return as well as the =-sign, and the new composition is p=p+(p*br)-(p*dr). This new composition combines known function names with new variable names to compute a certain mathematical mapping. The result of this simple mapping is stored in the variable ‘p’ and it is delivered to the outside of the function pop0 by the return-statement.

NECESSARY INPUT VALUES

To run the program one has to call the defined new function. But because the called function will need some values for the input variables one has first to enable the user to interact with the program by some input commands.

The input commands are informing the user which kind of information is asked for and the answers of the users will be stored in the variables p, br, and dr. The input values can also be ‘casted‘ into different value types like int — for integer — and float — for floating point –.

Here is the protocol of a possible input:

Number of citizens in the start year? 1000

Birthrate %? 0.82

Deathrate in %? 0.92

CALLING A DEFINED FUNCTION

After these preparations one can call the defined new function with the statement pnew = pop0(p,br,dr). Because the input variables have values received from the user the new function can start it’s mapping and can compute the follow up value for the population.

SHOW RESULTS

To show the user this new value explicitly on the screen  one has to use the print function, the counterpart to the input function:  print(‘New population number:\n’,int(pnew)). The print function prints the new value for the population number on the screen.  If you look closer to the print function you can detect some inherent structure: print() is the main structure with the function name ‘print’ and the brackets () as the placeholder for possible input arguments. In the used example the input arguments have two ‘parts’: (‘…’,v). The ‘…’-part allows some text which will be shown to the user, in our case New population number: followed by a line break caused by the symbols \n. The v-part allows the names of variables which have some values which can be printed. In the used example we have the expression int(pnew). pnew is the name of a variable with a value delivered by the pop0() function enabled by the return p function of the pop0() function. (Attention: the ‘return’ function works without ()-brackets to receive input arguments! The variable ‘p’ is an input argument) The value delivered by ‘return p’ is a floating point value. But because we have only ‘whole citizens’ we cast the non-integer parts of the float value away by making the variable ‘pnew’ an argument of another function int(). The int() function translates a floating point value into an integer value. This is the reason that we do not see ‘999.0’ but ‘999’:

New population number:
999

REMARK: Global and Local Variables

This simple example tells already something about the difference between global and local values. If one enters the variable names ‘p’ and ‘pnew’ in the python console of the spyder editor then one can see the following:

p
Out[5]: 1000

pnew
Out[6]: 999.0

After the function call to pop0() the original value of ‘p’ is unchanged, and the new value ‘pnew’ is different. That means the new value of ‘p internal in the function’ and the ‘old value of p external to the function’ are separated. The name of a variable has therefore to be distinguished with regard to the actual context: The same name  in different contexts (inside a function definition or outside) does represents different memory spaces.   The variable names inside a function definition are called local variables and the variable names external to a function definition are called global variables.

A continuation of this post you can find here.

SOURCE CODE: EXAMPLE: pop0.py

pop0.py as pop0.pdf

Co-Learning with python 3

 

eJournal: uffmm.org, ISSN 2567-6458, 1-6.April 2019
Email: info@uffmm.org
Author: Gerd Doeben-Henisch
Email: gerd@doeben-henisch.de

CONTEXT

The context of this small initiative ist the Actor-Actor Interaction (AAI) paradigm described in this blog.  For this AAI paradigm one will need an assisting software to manage real problems with real people. In principle this software can be realized by every kind of programming language. Which one will be used for the planned overall software service will be decided by those groups, which will do the final job. But for the development of the ideas, for an open learning process, we will use here the programming language python 3. To get a first understanding of the main programming languages and the special advantages and disadvantages of python 3 compared to python 2 and to other important languages see the short overview here 2019-03-18 in apenwarr or chapters 1-2 from the excellent book by Lutz mentioned below.

VISION

There are many gifted young people around who could produce wonderfull programms, if they would find a ‘start’, how to begin, where to go from here… sometimes these are friends doing other things together — like playing computer games 🙂 — why not start doing programming real programs by themselves? Why not start together some exciting project by their own?

If they want to do it, they need some first steps to enter the scene. This is the idea behind this vision: getting young people to do first steps, helping each other, document the process, improve, making nice things…

CONTRIBUTING POSTS

Here is a list of posts which can support the Co-Learning Process:

More posts will follow in a random order depending of the questions which will arise or the ideas someone wants to test.

REFERENCES

There is a huge amount of python books, articles and online resources. I will mention here only some of the books and resources I have used. The following is therefore neither complete nor closed. I will add occasionally some titles.

  • Mark Lutz, Learn Python, 2013,5th ed.,Sebastopol (CA), O’Reilly
  • To get the sources: https://www.python.org/
  • Documentation: https://docs.python.org/3/
  • Python Software Foundation (PSF): https://www.python.org/psf-landing/
  • Python Community: https://www.python.org/community/

 

Python Program Example: Simple Population Simulation

eJournal: uffmm.org, ISSN 2567-6458, 30.Dec 2018
Email: info@uffmm.org
Author: Gerd Doeben-Henisch
Email: gerd@doeben-henisch.de

CONTEXT

In a preceding post I have described a simple way to install the python software as part of a integrated development environment. In this post I show a simple program to simulate the increase/ decrease of a population with nearly no parameters. It can be used as a starting point for further discussions and developments.

HOW TO MAKE IT

Of one has installed (in case of windows) the winpython software as described above and one has selected the ‘spyder.exe’ module from the folder of the winpython software) either directly (by double clicking) or one clicks the icon on the task bar (which one has placed there before), then one has the spyder working environment on the screen.

spyder software screen appearance
spyder software screen appearance

In the left subscreen one can now edit the program (by copy th source code below and paste it into the window) and then one can test the software by clicking on the green run button (alternatively: pressing F5).

Then the python console will be activated in the sub-window in the lower right corner. One has to enter the required values. After the input the console window will show the numbers as well as the graph.

THE PROGRAM SOURCE CODE

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
“””
Created on Wed Jan 2 19:34:43 2019

@author: gerd doeben-henisch
Email: gerd@doeben-henisch.de
“””

##################################
# pop1()
###################################
#
# IDEA
#
# Simple program to compute the increase/ decrease of a population with
# the parameters population number (p), birth-rate (br), death-rate (dr),
# mirgration Plus (migrPlus), and migration Minus (migrMinus)
#

#######################################
# Used modules

import matplotlib.pyplot as plt
import numpy as np

#########################################
# Defining a function pop1()

def pop1(p,br,dr,migrPlus,migrMinus):

p=p+(p*br)-(p*dr)+migrPlus-migrMinus

return p

###################################
# Asking for input values
#
# input() creates a strng which has to be converted into an int()

baseYear = int(input(‘Basisjahr als Zahl ? ‘))

p = int(input(‘Bevölkerung als Zahl ‘))

br = float(input(‘Geburtenrate in % ‘))

dr = float(input(‘Sterberate in % ‘))

migrPlus = int(input(‘Zuwanderung Zahl ‘))
migrMinus = int(input(‘Abwanderung Zahl ‘))

n = int(input(‘Wieviele Jahre voraus ? ‘))

############################################
# processing the data
#
# creating a range called ‘run’ for the years to compute

run = np.arange(1, n+1, 1)

####################################
# pop is a ‘list’ to collect the pop-values for every year

pop = []

########################################
# The first element of pop is the base year
pop.append(p)

######################################
# Compute the changing values for the population p and store these in pop
# Use for this computation the function pop1() defined before

for i in run:
p=pop1(p,br,dr,migrPlus,migrMinus)
pop.append(p)

##############################################
# Print the content of pop for the user to show
# the different years with their pop-values

for i in range(n+1):
print(‘Jahr %5d = Einw. %8d \n’ %(baseYear+i, pop[i]) )

##############################################
# Make the numbers visible as a graph

plt.figure(1)
plt.axis([0, len(run)+1, 1, max(pop)])

run2 = np.arange(0, n+1, 1)
plt.plot(run2, pop, ‘bo’)

plt.show()
plt.close()

EXAMPLE RUNS

EXAMPLE 1

Shows a population with a lower birth rate than death rate but a positive migration outcome. (Bevölkerung = population, Zahl = number, Gebrtenrate = biirth rate, Sterberate = death rate, Zuwanderung = migration plus, Abwanderung = migration minus, Wieviele Jahre voraus = how many years forcasting)

Bevölkerung als Zahl 1000

Geburtenrate in % 0.15

Sterberate in % 0.17

Zuwanderung Zahl 200

Abwanderung Zahl 100

Wieviele Jahre voraus ? 20

Jahr 2019 = Einw. 1000

Jahr 2020 = Einw. 1080

Jahr 2021 = Einw. 1158

Jahr 2022 = Einw. 1235

Jahr 2023 = Einw. 1310

Jahr 2024 = Einw. 1384

Jahr 2025 = Einw. 1456

Jahr 2026 = Einw. 1527

Jahr 2027 = Einw. 1596

Jahr 2028 = Einw. 1665

Jahr 2029 = Einw. 1731

Jahr 2030 = Einw. 1797

Jahr 2031 = Einw. 1861

Jahr 2032 = Einw. 1923

Jahr 2033 = Einw. 1985

Jahr 2034 = Einw. 2045

Jahr 2035 = Einw. 2104

Jahr 2036 = Einw. 2162

Jahr 2037 = Einw. 2219

Jahr 2038 = Einw. 2275

Jahr 2039 = Einw. 2329

example 1 - increasing population
example 1 – increasing population

EXAMPLE 2

Shows a population with a lower birth rate than death rate and a negative migration outcome. (Bevölkerung = population, Zahl = number, Gebrtenrate = biirth rate, Sterberate = death rate, Zuwanderung = migration plus, Abwanderung = migration minus, Wieviele Jahre voraus = how many years forcasting)

Basisjahr als Zahl ? 2019

Bevölkerung als Zahl 1000

Geburtenrate in % 0.15

Sterberate in % 0.17

Zuwanderung Zahl 100

Abwanderung Zahl 120

Wieviele Jahre voraus ? 30
Jahr 2019 = Einw. 1000

Jahr 2020 = Einw. 960

Jahr 2021 = Einw. 920

Jahr 2022 = Einw. 882

Jahr 2023 = Einw. 844

Jahr 2024 = Einw. 807

Jahr 2025 = Einw. 771

Jahr 2026 = Einw. 736

Jahr 2027 = Einw. 701

Jahr 2028 = Einw. 667

Jahr 2029 = Einw. 634

Jahr 2030 = Einw. 601

Jahr 2031 = Einw. 569

Jahr 2032 = Einw. 538

Jahr 2033 = Einw. 507

Jahr 2034 = Einw. 477

Jahr 2035 = Einw. 447

Jahr 2036 = Einw. 418

Jahr 2037 = Einw. 390

Jahr 2038 = Einw. 362

Jahr 2039 = Einw. 335

Jahr 2040 = Einw. 308

Jahr 2041 = Einw. 282

Jahr 2042 = Einw. 256

Jahr 2043 = Einw. 231

Jahr 2044 = Einw. 206

Jahr 2045 = Einw. 182

Jahr 2046 = Einw. 159

Jahr 2047 = Einw. 135

Jahr 2048 = Einw. 113

Jahr 2049 = Einw. 90

example 2 - decreasing population
example 2 – decreasing population

LEARNING ENVIRONMENT

For an overview of all posts in this block about programming with python 3 see HERE.

SIMPLE PROGRAMMING ENVIRONMENT WITH PYTHON-SPYDER

eJournal: uffmm.org, ISSN 2567-6458, 30.Dec 2018; extension 10.April 2019
Email: info@uffmm.org
Author: Gerd Doeben-Henisch
Email: gerd@doeben-henisch.de

CONTEXT

This post is part of the online book project for the AAI-paradigm. As mentioned in the text of the book the AAI paradigm will need for its practical usage appropriate software. Some preliminary (experimentally) programming is already underway. The programming language used for this programming is python. Here some bits of information how one can install a simple python environment to share these activities.

WINPYTHON OR ANACONDA WITH SPYDER
(Windows as well as Linux (ubuntu))

To work with the python programming language — here python 3 —  one needs some tools interacting with each other. For this different integrated development packages have been prepared.  In this uffmm-software project I am using the spyder development environment either as part of the winpython distribution or as part of the anaconda distribution.

WINPYTHON DISTRIBUTION

The winpython package can be found here. See also the picture below.

Website of winpython distribution
Website of winpython distribution

If one has downloaded the winpython distribution in some local folder then you will see the following files and folders (see picture below):

winpython distribution folder after installation
winpython distribution folder after installation

SPYDER INTEGRATED ENVIRONMENT

To use the integrated spyder environment one can also look to the spyder website directly (see picture below).

spyder working environment for python - website
spyder working environment for python – website

The spyder team recomments to download the spyder software as part of the bigger anaconda distribution with lots of additional options (see picture below).

spyder working environment embedded in the anaconda distribution
spyder working environment embedded in the anaconda distribution

Downloading the anaconda distribution needs much more time then the winpython distribution. But one gets a lot of stuff and the software is fairly good integrated into the windows 10 operating system. I personally recomment for the beginners not to beginn with the complex anaconda environment but to stay with the spyder integrated environment only. This can be done by activating with the windows-button the list of apps, looking to the anaconda icon, and there one can find the spyder icon (an idealized spyder web). One can click with the right mouse button on this icon and then select to ‘add to the task bar’. After this operation you can observe the spyder icon as attached to the task bar like in the picture below.

part of the task bar in windows with icons for spyder and a python environment
Part of the task bar in windows with icons for spyder (right border)  and a python environment (left from spyder)

If one activates the spyder icon a window opens showing some standard configuration of the integrated spyder development environment (see picture below).

spyder working environment with editor, console, and additional object informations
spyder working environment with editor, console, and additional object informations

The most important sub-windows are the window left from the editor and the window right-below from the console.  One can use the console to make small experiments with python commands and the editor to write larger source code.  In the header bar are many helpful icons for editing, running of programs, testing, and more.

LINUX (UBUNTU)

If one is working with linux (what I am usually are doing; I use the distribution ubuntu 18.04.1 LTS) then python is part of the system in version 2 as well in version 3 and spyder can be used too.

WINPYTHON CONSOLE

Besides the integrated development editor (IDE) spyder you can find even more programming tools. One very helpful tool is the WinPython Console (see icon within the red circle). You can attach this icon on your task bar too and then you can start the Winpython Console by clicking on it.

Figure 1: Directory path after starting the WinPython Console
Figure 1: Directory path after starting the WinPython Console

To apply this console to some python programm you have to navigate to that folder where you have the python program to be executed. If You do not know already the whole path then you have two options: (i) move the path upwards by using the command ‘cd ..’ (change directory one level upwards) or (ii) move the path downwards by using the command ‘cd DIR-NAME‘. If you are in a new folder you can use the command ‘dir‘ to list all the files and folders in the actual directory. Doing this You can reach the following folder with some python program files:

Figure 2: Folder with some python programs
Figure 2: Folder with some python programs

There is one example file pop0e.py which we can start (the whole program will be described later in detail).

Figure 3: Shows an example run with the program pop0e.py, some print outs as well as a diagram.
Figure 3: Shows an example run with the program pop0e.py, some print outs as well as a diagram.

For this last example with the Winpython Console the program can be edited by nearly any kind of a text editor. After the file has been saved with a .py ending one can use the console to start the program. In the last example (cf. figure 3) the important input was: ‘python pop0e.py‘ this states that the python interpreter shall take the program text of the file ‘pop0e.py’ and execute the program.

LEARNING ENVIRONMENT

For an overview of all posts in this block about programming with python 3 see HERE.