some more on the python part

This commit is contained in:
Jan Grewe 2015-12-02 00:17:19 +01:00
parent 543078659b
commit c1d578a71a

View File

@ -10,6 +10,26 @@ strong community support. Most packages are developed by open-source
enthusiasts who are willing to share their code and knowledge thus,
answers to most problems can be found with a simple internet search.
\begin{important}[What is Python?]
It is important to understand what Python is and what it is
not. After installing python on a machine, one may expect to find an
elaborated graphical user interface or so called \enterm{Integrated
Development Environment, IDE} as e.g known in \matlab{}. You can
have it, if you want but this is not the Python core. Python is a
programming language, it defines the \enterm{syntax}, the grammar
and the keywords of the language, as well as the
\enterm{interpreter} that interprets your commands and executes
them. With the installation you get the python interpreter that,
called to work will open a console interface that can be used to
execute commands. Throughout the whole introduction we will use a
python console, or the command line. All examples should work with
the simplest python console. It is, however, nicer to use e.g. the
\emph{ipython} console for it offers some more convenience
methods. For bigger programs IDEs (e.g. spyder, pycharm, etc. ) come
in handy but they are building on top of Python, they are not
Python.
\end{important}
\section{Variables and Datatypes}
Befor we can start to solve programming problems we have to know a few
@ -20,15 +40,15 @@ basic concepts and the python syntax will be introduced.
A \enterm{variable} is basically a pointer to a place in a computer's
memory. This pointer has a human readable name, or label, and a
\enterm{datatype} (figure \ref{variablefig}). In the memory the
\enterm{data type} (figure \ref{variablefig}). In the memory the
variable's content is encoded as a pattern of \enterm[bit]{bits} (a
sequence of zeros and ones). Upon access of the variable's content the
stored datatype is used to interpret the \enterm{bit pattern}. That is,
the same bit pattern may be interpreted in different ways depending on
the datatype. In the example shown in figure \ref{variablefig} the
same bitpattern is stored but the interpretation as an 8-bit
\enterm{integer} leads to the numeric value of 38 while an
interpretation as a \enterm{character} lead to the ampersand symbol.
stored data type is used to interpret the \enterm{bit pattern}. That
is, the same bit pattern may be interpreted in different ways
depending on the data type. In the example shown in figure
\ref{variablefig} the same bitpattern is stored but the interpretation
as an 8-bit \enterm{integer} leads to the numeric value of 38 while an
interpretation as a \enterm{character} lead to the ampersand symbol.
\begin{figure}
\centering
@ -54,23 +74,46 @@ Variables can be created at any time at any place in a python
program. Listing \ref{varListing1} shows different ways to do this:
\begin{lstlisting}[label=varListing1, caption={Creating variables.}, language=python]
w = 3.1415
x = 38
y = 'A'
z = None
In[0]: w = 3.1415
In[1]: x = 38
In[2]: y = 'A'
In[3]: z = None
\end{lstlisting}
Line one creates
Die Zeile 1 kann etwa so gelesen werden:''Erzeuge eine Variable mit
dem Namen \varcode{x} und weise ihr den Wert 38 zu''. Das
Gleichheitszeichen ist der sogenannte
\codeterm{Zuweisungsoperator}. Zeile 5 definiert eine Variable \varcode{y}, der
ein leerer Wert zugewiesen wird. Da \matlab{}, wenn nicht anders
angegeben, immer den \codeterm{double} Datentypen benutzt, haben beide
Variablen diesen Datentyp. In Zeile 9 wird der Variablen \varcode{z} der Buchstabe
``A'' zugewiesen. \varcode{z} ist nicht ein Flie{\ss}kommazahl von Typ \codeterm{double},
sondern ein \codeterm{character} (Zeichen).
Line one creates a simple variable named 'w' that contains the value
$3.14.15$. The command can be read as ``Create a variable named
\varcode{w} and assign the value $3.1415$ to it.'' The equals sign in
this context is called the \codeterm{Assignment operator} that is used
when a value is \emph{assigned} to a variable. The following lines
create further variables containing differen values. Python belongs to
the class of so called \codeterm[interpreted language]{interpreted
languages}, i.e. a command (like those in listing\ref{varListing1})
is prompted, the Python \coderterm{interpreter} executes it, creates
the variable, checks for the type of data to be stored and assigns the
value. If we want to check for the variables that have been created or
find out their data type we can ask for this information by the
commands in listing \ref{varListing2}
\begin{lstlisting}[label=varListing2, caption={Checking for variables and data types.}]
In [4]: who
w x y z
In[5]: whos
Variable Type Data/Info
--------------------------------
w float 3.1415
x int 38
y str A
z NoneType None
In[6]: type(w)
Out[6]: float
In[7]: type(x)
Out[7]: int
\end{lstlisting}
To simply list the names of all \enterm{declared} variables, the command \code{who} can be used, in the \emph{ipython}
Der Datentyp einer Variable kann mit \code{class()} abgefragt werden.
Eine Liste aller definierten Variablen gibt \code{who}
zur\"uck. Detailliertere Informationen \"uber Variablen zeigt