ISSN 2567-6458, 6.April 2019
Author: Gerd Doeben-Henisch
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.
In this post I comment only on the changes between the actual program and the version before.
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:
# X-Column, Y-Column
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