Category Archives: reading a file

STARTING WITH PYTHON 3 – The very beginning

ISSN 2567-6458, July 1, 2019
Author: Gerd Doeben-Henisch


The idea is to give some advice to start with python 3 programming. In other posts I have used an integrated package using WinPython including the integrated  development package  spyder. While such an integrated package offers lots of nice tools it hides the basic structures of your system and of the language. Therefore I decided to start again at a more basic level using only the python language and the windows command shell (I myself are working with an ubuntu system for the normal python programming and for the work with a python web-framework called ‘django’). Because I myself am not too much experienced with the windows 10 environment it challenges me to investigate the win10 environment to some degree.


To get a fresh start from scratch I have removed all python stuff from my machine.  Then I have downloadad the newest pythion version for windows from This is python version 3.7.3 in the 32-Bit version. There exists also a 64-Bit version, but because not all the different modules are already prepared for the 64-Bit version the 32-Bit version is more flexible. To learn python this is more than enough. To switch to a 64-Bit version would be simple.


On my machine the installation path is:


To test whether python works on your windows machine you can activate the Windows Power Shell. You will find this shell by clicking on the Windows Icon in the left corner of your laptop which opens the list of all Windows-Apps (programs).  At the end of this list there are some windows-Apps including the Windows-Power-Shell. I took the first one. By clicking on the right mouse-button I selected to attach the power-shell on the task bar. This allows me to click once on the icon of the power shell and the power shell will open.

Typing the command ‘python’ the first time into the command line will cause an error message, because Windows does not know where to find the command ‘python’. You can do two things: (i) enter the whole path for python or (ii) inform the PATH-variable about the python path.

Enter the whole path worked out:

PS C:\Users\gerd_2> C:\Users\gerd_2\AppData\Local\Programs\Python\Python37-32\python

Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32

Type “help”, “copyright”, “credits” or “license” for more information.


Because this procedure is a bit cumbersome I tried the other proposal, to edit the PATH-variable. One proposal (from the community) goes like this:

  1. Open the Start Search, type in “env”, and choose “Edit the system environment variables”:
  2. Click the “Environment Variables…” button.
  3. Under the “System Variables” section (the lower half), find the row with “Path” in the first column, and click edit.
  4. The “Edit environment variable” UI will appear.

I could edit the PATH-variable this way, but it showed no effect. Therefore I tried another procedure, where one can modify the PATH-variable directly from the power-shell. The needed command has the following format:

$Env:PATH += “;Wanted Path”

This addresses the PATH variable, let the actual content as it is and adds after a semicolon the needed path. With the command


you can check the actual values of the variable PATH and after adding your new path

PS C:\Users\gerd_2> $Env:PATH +=”;C:\Users\gerd_2\AppData\Local\Programs\Python\Python37-32″

you can check again. It worked, I got my new path added to  the variable. But, even more important, now it worked. Entering in the power shell only ‘python’ it worked:

PS C:\Users\gerd_2> python
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.

I also tried the ‘help-option’ like this:

>>> help()

Welcome to Python 3.7’s help utility!

If this is your first time using Python, you should definitely check out

the tutorial on the Internet at

Enter the name of any module, keyword, or topic to get help on writing

Python programs and using Python modules. To quit this help utility and

return to the interpreter, just type “quit”.

To get a list of available modules, keywords, symbols, or topics, type

“modules”, “keywords”, “symbols”, or “topics”. Each module also comes

with a one-line summary of what it does; to list the modules whose name

or summary contain a given string such as “spam”, type “modules spam”.

I tried e.g.  the ‘modules option:

help> modules

Please wait a moment while I gather a list of all available modules…

__future__ _tracemalloc glob secrets

_abc _warnings gzip select

_ast _weakref hashlib selectors

_asyncio _weakrefset heapq setuptools

From this I selected the  ‘socket’-option:

help> socket

Help on module socket:




This module provides socket operations and some related functions.

On Unix, it supports IP (Internet Protocol) and Unix domain sockets.

On other systems, it only supports IP. Functions specific for a

socket are available as methods of the socket object.


socket() — create a new socket object

socketpair() — create a pair of new socket objects [*]

fromfd() — create a socket object from an open file descriptor [*]

fromshare() — create a socket object from data received from socket.share() [*]

Thus this function works and looks promising.


The main idea of using python is to be able to write some program code which then the machine can run. The basic processing steps are

  1. Start python in the power shell by typing  ‘python’
  2. Enter some python code you want to test.
  3. If you want to edit many python commands together then take a text editor and write some text which looks like python code.
  4. The text you enter directly into a python command line or a edited text in a file which you can load both will be handed out to the python interpreter which translate these pyton commands into a byte code which in turn will be run by the python virtual machine (PVM).

It is helpful befor you start programming to generate at least one folder where you will store your python programs. Following the proposal from Mark Lutz (see below references) I generate in my main directory a folder called ‘code’:

PS C:\> cd $HOME

PS C:\Users\gerd_2> mkdir code

Verzeichnis: C:\Users\gerd_2

Mode LastWriteTime Length Name

—- ————- —— —-

d—– 01.07.2019 18:40 code

To edit a first simple program with name ‘’ I have called the notepad editor in the power shell like this:

PS C:\Users\gerd_2\code> notepad

In the editor I have typed the following simple text:


import sys





Then I have stored this file in the ‘code’ folder. Wih the command ‘dir’ one can have a look into the ‘code’ folder:

PS C:\Users\gerd_2\code> dir

Verzeichnis: C:\Users\gerd_2\code

Mode LastWriteTime Length Name

—- ————- —— —-

-a—- 01.07.2019 18:41 92

To check what happens when I load this script with the python interpreter one can enter the following command:

PS C:\Users\gerd_2\code> python





One can also redirect the output of the python interpreter from the console into a file:

PS C:\Users\gerd_2\code> python >savesrc.txt

Then one can again call an editor like notepad to read the content of this file:

PS C:\Users\gerd_2\code> notepad savesrc.txt

CONTENT savesrc.txt:





Like many other programming language python organizes larger programs by putting together  smaller programs like building blocks. These blocks are called modules and this strategy  can only work if one follows some rules. Mark Lutz formulates it as follows:

“… a module is mostly just a package of variable names, known as a namespace, and the names within that package are called attributes. An attribute is simply a variable name that is attached to a specific object (like a module).” (Lutz, Mark. Learning Python (S.70). O’Reilly Media. Kindle-Version)

One possibility to fetch a module is to use the import command, either directly in a python console or indirectly as command in a python program text. Thus with going direct to the console:

PS C:\Users\gerd_2\code> python
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import os
>>> os.getcwd()

Here the module ‘os’ has been imported and within tis module exists a function ‘getcwd()’ which fetches the actual path you are in. In my case my home directory and there the folder ‘code’. Doing an import with the new file ‘’ it works like this:

PS C:\Users\gerd_2\code> python

Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32

Type “help”, “copyright”, “credits” or “license” for more information.

>>> import script1




Thus the import also causes a run of the script.

If one wants to know which kinds of names are used in the module one can use the command dir():

>>> dir(script1)

[‘__builtins__’, ‘__cached__’, ‘__doc__’, ‘__file__’, ‘__loader__’, ‘__name__’, ‘__package__’, ‘__spec__’, ‘sys’, ‘x’]


Go back to the main page.


  • Mark Lutz, Learn Python, 2013,5th ed.,Sebastopol (CA), O’Reilly
  • To get the sources:
  • Documentation:
  • Python Software Foundation (PSF):
  • Python Community:

Example python3: popShow0 – simple file-reader

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:’). This program is a continuation of the program, 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:

# 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 as popShow0.pdf