some more on the python part
This commit is contained in:
parent
543078659b
commit
c1d578a71a
@ -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,
|
enthusiasts who are willing to share their code and knowledge thus,
|
||||||
answers to most problems can be found with a simple internet search.
|
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}
|
\section{Variables and Datatypes}
|
||||||
|
|
||||||
Befor we can start to solve programming problems we have to know a few
|
Befor we can start to solve programming problems we have to know a few
|
||||||
@ -23,11 +43,11 @@ memory. This pointer has a human readable name, or label, and a
|
|||||||
\enterm{data type} (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
|
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
|
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,
|
stored data type is used to interpret the \enterm{bit pattern}. That
|
||||||
the same bit pattern may be interpreted in different ways depending on
|
is, the same bit pattern may be interpreted in different ways
|
||||||
the datatype. In the example shown in figure \ref{variablefig} the
|
depending on the data type. In the example shown in figure
|
||||||
same bitpattern is stored but the interpretation as an 8-bit
|
\ref{variablefig} the same bitpattern is stored but the interpretation
|
||||||
\enterm{integer} leads to the numeric value of 38 while an
|
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.
|
interpretation as a \enterm{character} lead to the ampersand symbol.
|
||||||
|
|
||||||
\begin{figure}
|
\begin{figure}
|
||||||
@ -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:
|
program. Listing \ref{varListing1} shows different ways to do this:
|
||||||
|
|
||||||
\begin{lstlisting}[label=varListing1, caption={Creating variables.}, language=python]
|
\begin{lstlisting}[label=varListing1, caption={Creating variables.}, language=python]
|
||||||
w = 3.1415
|
In[0]: w = 3.1415
|
||||||
x = 38
|
In[1]: x = 38
|
||||||
y = 'A'
|
In[2]: y = 'A'
|
||||||
z = None
|
In[3]: z = None
|
||||||
\end{lstlisting}
|
\end{lstlisting}
|
||||||
|
|
||||||
Line one creates
|
Line one creates a simple variable named 'w' that contains the value
|
||||||
Die Zeile 1 kann etwa so gelesen werden:''Erzeuge eine Variable mit
|
$3.14.15$. The command can be read as ``Create a variable named
|
||||||
dem Namen \varcode{x} und weise ihr den Wert 38 zu''. Das
|
\varcode{w} and assign the value $3.1415$ to it.'' The equals sign in
|
||||||
Gleichheitszeichen ist der sogenannte
|
this context is called the \codeterm{Assignment operator} that is used
|
||||||
\codeterm{Zuweisungsoperator}. Zeile 5 definiert eine Variable \varcode{y}, der
|
when a value is \emph{assigned} to a variable. The following lines
|
||||||
ein leerer Wert zugewiesen wird. Da \matlab{}, wenn nicht anders
|
create further variables containing differen values. Python belongs to
|
||||||
angegeben, immer den \codeterm{double} Datentypen benutzt, haben beide
|
the class of so called \codeterm[interpreted language]{interpreted
|
||||||
Variablen diesen Datentyp. In Zeile 9 wird der Variablen \varcode{z} der Buchstabe
|
languages}, i.e. a command (like those in listing\ref{varListing1})
|
||||||
``A'' zugewiesen. \varcode{z} ist nicht ein Flie{\ss}kommazahl von Typ \codeterm{double},
|
is prompted, the Python \coderterm{interpreter} executes it, creates
|
||||||
sondern ein \codeterm{character} (Zeichen).
|
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.
|
Der Datentyp einer Variable kann mit \code{class()} abgefragt werden.
|
||||||
Eine Liste aller definierten Variablen gibt \code{who}
|
Eine Liste aller definierten Variablen gibt \code{who}
|
||||||
zur\"uck. Detailliertere Informationen \"uber Variablen zeigt
|
zur\"uck. Detailliertere Informationen \"uber Variablen zeigt
|
||||||
|
Reference in New Issue
Block a user