<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://eclr.humanities.manchester.ac.uk/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Jb</id>
		<title>ECLR - User contributions [en]</title>
		<link rel="self" type="application/atom+xml" href="http://eclr.humanities.manchester.ac.uk/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Jb"/>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php/Special:Contributions/Jb"/>
		<updated>2026-05-02T03:51:33Z</updated>
		<subtitle>User contributions</subtitle>
		<generator>MediaWiki 1.30.1</generator>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3217</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3217"/>
				<updated>2013-10-16T13:37:01Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* Running Python programs */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The following assumes use of Python version 3, as opposed to Python 2. No more major releases are planned for Python 2, and so version 3 is expected to be the future of Python. The two versions of Python, although similar, are not compatible in a forwards or backwards direction&amp;lt;ref&amp;gt;Although Python 2 and 3 are not totally compatible, Python 2.7 is close to Python 3. If you have to use Python 2, it is recommended using version 2.7, writing code as close to Python 3 as possible, and using tools like &amp;#039;&amp;#039;2to3&amp;#039;&amp;#039; to port to Python 3. Alternatively there is a Python compatibility packages called &amp;#039;&amp;#039;six&amp;#039;&amp;#039;.&amp;lt;/ref&amp;gt;, and some legacy code exists only as Python 2. Some differences between the two versions are discussed in the footnotes.&lt;br /&gt;
&lt;br /&gt;
= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list/range to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python 3. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. The advantage of a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; object over a Python &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;list&amp;lt;/source&amp;gt; is that every single integer value is not stored in memory with a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;. &amp;lt;ref&amp;gt;In Python 3 the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object. However the Python 2 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a list, i.e. stores every integer value required in memory which is very inefficient if simply looping through a long sequence of integers in a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for&amp;lt;/source&amp;gt; loop. Python 2 has &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;xrange&amp;lt;/source&amp;gt; that behaves like the Python 3 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;.&amp;lt;/ref&amp;gt;. For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 0 and 1 respectively for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 0, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at Python equivalents of the MATLAB &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics can be found on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows (a description of how to run this code is given towards the end of this page).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
This is a good point to mention that the behaviour of integer division changed in Python 3, compared to version 2. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm. Remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, these are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;. Namespaces are useful because they allow package writers to choose functions and variable names without worrying about whether that name has been used elsewhere. For example, NumPy includes a function called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt;, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespaces within the function call, e.g. to use &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; at the Python command prompt (to generate 5 random numbers)&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, rather than assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;does not copy&amp;#039;&amp;#039;&amp;#039; values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; to a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array of values. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; does copy all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
NumPy array (and Python list) slices work in subtly different ways to MATLAB&amp;#039;s too. For example, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A[m:n]&amp;lt;/source&amp;gt; returns all values from the element with the index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;m&amp;lt;/source&amp;gt; to the element with index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;n-1&amp;lt;/source&amp;gt;, and because the first element has index 0, we receive the (m+1)&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; to n&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; values, e.g. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[0:10]&lt;br /&gt;
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[4:6]&lt;br /&gt;
[5, 6]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Compare this to MATLAB&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
r =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(1:10)&lt;br /&gt;
ans =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(4:6)&lt;br /&gt;
ans =&lt;br /&gt;
     4     5     6&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on whole arrays are much faster than the equivalent code using loops, as they allow optimal use of the processor (such code optimisation is often called vectorisation). In addition code using vector and matrix operations is often shorter and easier to read that the equivalent using loops.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, this operation is much quicker than growing arrays on-the-fly. In this example we preallocate two arrays at the Python prompt with 10,000 elements each, the first array contains integers and the second contains double precision floating point numbers.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced example ===&lt;br /&gt;
We now look at the Python equivalent of the code on the MATLAB page in the section entitled [[Program_Flow_and_Logicals#Relevant_example|Relevant example]], which assumed we have &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; returns in a vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to:&lt;br /&gt;
&lt;br /&gt;
# Count the number of positive, negative and zero returns&lt;br /&gt;
# Create an array holding only the positive values&lt;br /&gt;
# Create another array holding only the negative values&lt;br /&gt;
# Compute the means of the positive and negative returns&lt;br /&gt;
&lt;br /&gt;
A naive Python algorithm that uses a loop rather than vectorisation is as follows.&lt;br /&gt;
# Find the length of the NumPy array holding  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=numpy.size(r)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus=0; Tzero=0; Tminus=0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate two sum variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;psum=0.0; nsum=0.0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Preallocate NumPy arrays &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=numpy.zeros(T)&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=numpy.zeros(T)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe)&lt;br /&gt;
# Set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;i=0&amp;lt;/source&amp;gt; &lt;br /&gt;
# Check whether &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; is greater, smaller or equal to 0&lt;br /&gt;
#* If &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;gt;0&amp;lt;/source&amp;gt;, set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus[Tplus]=r[i]&amp;lt;/source&amp;gt;, add &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;psum&amp;lt;/source&amp;gt;, and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;0&amp;lt;/source&amp;gt; set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus[Tminus]=r[i]&amp;lt;/source&amp;gt;, add &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;nsum&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Repeat 6 for &amp;lt;math&amp;gt;i=1,\ldots,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove spare zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=rplus[0:Tplus].copy()&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=rminus[0:Tminus].copy()&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus (the number of positive, negative and zero returns are stored in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;)&lt;br /&gt;
&lt;br /&gt;
The Python code is as follows, however note that this code isn&amp;#039;t completely free of vector operations, since removal of zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; is vectorised.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
T=numpy.size(r)&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0&lt;br /&gt;
psum=0.0;nsum=0.0&lt;br /&gt;
rplus=numpy.zeros(T);rminus=numpy.zeros(T)&lt;br /&gt;
for i in range(T):&lt;br /&gt;
   if r[i]&amp;gt;0:&lt;br /&gt;
      rplus[Tplus]=r[i]   #Store positive return in array rplus&lt;br /&gt;
      Tplus+=1            #Increase Tplus by one if return is positive&lt;br /&gt;
      psum+=r[i]          #Add return to sum of positive values&lt;br /&gt;
   elif r[i]&amp;lt;0:&lt;br /&gt;
      rminus[Tminus]=r[i] #Store negative return in array rminus&lt;br /&gt;
      Tminus+=1           #Increase Tminus by one if return is negative&lt;br /&gt;
      nsum+=r[i]          #Add return to sum of negative values&lt;br /&gt;
   else:&lt;br /&gt;
      Tzero+=1            #Increase Tzero by one if return is zero&lt;br /&gt;
rplus=rplus[0:Tplus].copy()      #Remove zeros from rplus&lt;br /&gt;
rminus=rminus[1:Tminus].copy()   #Remove zeros from rminus&lt;br /&gt;
meanplus=psum/Tplus              # Compute mean of positive returns &lt;br /&gt;
meanminus=nsum/Tminus            # Compute mean of negative returns &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
We can create an alternative algorithm that only uses vector operations, using the following algorithm.&lt;br /&gt;
# Create an array &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; containing the positive values from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;&lt;br /&gt;
# Create an array &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; containing the negative values from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the length of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and assign to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the length of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; and assign to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Calculate &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the mean of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; using vectorised functions&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
rminus=r[r&amp;lt;0].copy()         # Create an array containing negative returns&lt;br /&gt;
Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This version is much shorter and cleaner, and therefore easier to create and maintain.&lt;br /&gt;
&lt;br /&gt;
== Running Python programs ==&lt;br /&gt;
For people who are familiar with MATLAB it may be surprising to discover there is no simple way of running a Python program from within Python. If you want to run Python code using the standard Python interpreter, your choices are either&lt;br /&gt;
# Launch it from outside Python, e.g. save to a file &amp;lt;code&amp;gt;myscript.py&amp;lt;/code&amp;gt; and at the command line enter &amp;lt;code&amp;gt;python myscript.py&amp;lt;/code&amp;gt;&lt;br /&gt;
# Convert the program to a function and use the [http://docs.python.org/3/tutorial/modules.html Python module functionality], e.g. save the function in a file &amp;lt;code&amp;gt;myfunctions.py&amp;lt;/code&amp;gt; and use Python&amp;#039;s &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;import&amp;lt;/source&amp;gt; to make the function available.&lt;br /&gt;
&lt;br /&gt;
The first method can be demonstrated by creating a text file &amp;lt;code&amp;gt;ReturnAnalysis.py&amp;lt;/code&amp;gt; containing the following program (modified from the vectorised [[Python/Program_Flow_and_Logicals#More_advanced_example|More advanced example]] above).&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
n=500000000&lt;br /&gt;
r=numpy.random.rand(n)*10-5&lt;br /&gt;
&lt;br /&gt;
import time&lt;br /&gt;
time1 = time.clock()&lt;br /&gt;
rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
rminus=r[r&amp;lt;0].copy()        # Create an array containing negative returns&lt;br /&gt;
Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
time2 = time.clock()&lt;br /&gt;
print(time2-time1)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In this example the array of values &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt; is generated using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rand&amp;lt;/source&amp;gt; function, in a real scenario these values might be loaded from a file. To run this from the &amp;#039;&amp;#039;&amp;#039;operating system command line&amp;#039;&amp;#039;&amp;#039; we can enter &amp;lt;code&amp;gt;python ReturnAnalysis.py&amp;lt;/code&amp;gt;. Note that this program outputs how long it takes to run, and on my desktop takes around 12.3s to complete (using the Anaconda Python distribution with the Accelerate package).&lt;br /&gt;
&lt;br /&gt;
Using the second method we can create a function, the following example undertakes the computation and returns the values required. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;def returnanalysis(r):&lt;br /&gt;
   import numpy&lt;br /&gt;
   rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
   rminus=r[r&amp;lt;0].copy()        # Create an array containing negative returns&lt;br /&gt;
   Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
   Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
   Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
   meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
   meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
   return meanplus, meanminus, Tplus, Tminus, Tzero&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
If this is saved to a file called &amp;lt;code&amp;gt;myfunctions.py&amp;lt;/code&amp;gt;, we can import and use the function from the Python prompt as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; n=500000000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=numpy.random.rand(n)*10-5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import myfunctions&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; mplus, mminus, Tp, Tm, Tz = myfunctions.returnanalysis(r)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; mplus&lt;br /&gt;
2.4999997176593398&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; mminus&lt;br /&gt;
-2.4999816498237375&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The Python prompt has various other limitations which mean it isn&amp;#039;t ideal for interactive work. For example, it doesn&amp;#039;t include commands like &amp;lt;source enclose=none&amp;gt;pwd&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;cd&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;pwd&amp;lt;/source&amp;gt;, etc. An improved command line is included in [http://ipython.org/ IPython (Interactive Python)] which behaves far more like MATLAB. For example if we save the first Python code snippet from the [[Python/Program_Flow_and_Logicals#More_advanced_example|&amp;#039;&amp;#039;&amp;#039;More advanced Example&amp;#039;&amp;#039;&amp;#039; section (see above)]] to a file called &amp;lt;code&amp;gt;ReturnAnalysis1.py&amp;lt;/code&amp;gt;, we can execute this program from within IPython using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;run&amp;lt;/source&amp;gt;. For MATLAB-like behaviour, where a script can see the variables in the interactive namespace, we need to use &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;run&amp;lt;/source&amp;gt; with the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;-i&amp;lt;/source&amp;gt; flag. The &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;-t&amp;lt;/source&amp;gt; flag is useful too, as it times how long the script takes to run.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
In [1]: n=500000000&lt;br /&gt;
In [2]: from numpy.random import rand&lt;br /&gt;
In [3]: r=rand(n)*10-5&lt;br /&gt;
In [4]: run -i -t ReturnAnalysis1&lt;br /&gt;
&lt;br /&gt;
IPython CPU timings (estimated):&lt;br /&gt;
  User   :     978.66 s.&lt;br /&gt;
  System :       0.00 s.&lt;br /&gt;
Wall time:     978.71 s.&lt;br /&gt;
&lt;br /&gt;
In [5]: meanplus&lt;br /&gt;
Out[5]: 2.5001402997170192&lt;br /&gt;
&lt;br /&gt;
In [6]: meanminus&lt;br /&gt;
Out[6]: -2.5000714107736286&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The above example used the unvectorised version, and to demonstrate the importance of vectorisation in getting good performance we compare with the vectorised version (saved in &amp;lt;code&amp;gt;ReturnAnalysis2.py&amp;lt;/code&amp;gt;).&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
In [7]: run -i -t ReturnAnalysis2&lt;br /&gt;
&lt;br /&gt;
IPython CPU timings (estimated):&lt;br /&gt;
  User   :      12.18 s.&lt;br /&gt;
  System :       0.00 s.&lt;br /&gt;
Wall time:      12.18 s.&lt;br /&gt;
&lt;br /&gt;
In [8]: meanplus&lt;br /&gt;
Out[8]: 2.5001402997170192&lt;br /&gt;
&lt;br /&gt;
In [9]: meanminus&lt;br /&gt;
Out[9]: -2.5000714107736286&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Finally to compare with the vectorised MATLAB version (saved to a file called &amp;lt;code&amp;gt;ReturnAnalysis2.m&amp;lt;/code&amp;gt;), the run time is as follows.&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; n=500000000;&lt;br /&gt;
&amp;gt;&amp;gt; r=rand(n,1)*10-5;&lt;br /&gt;
&amp;gt;&amp;gt; tic,ReturnAnalysis2,toc&lt;br /&gt;
Elapsed time is 11.193218 seconds.&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3216</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3216"/>
				<updated>2013-10-16T13:24:01Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* More advanced example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The following assumes use of Python version 3, as opposed to Python 2. No more major releases are planned for Python 2, and so version 3 is expected to be the future of Python. The two versions of Python, although similar, are not compatible in a forwards or backwards direction&amp;lt;ref&amp;gt;Although Python 2 and 3 are not totally compatible, Python 2.7 is close to Python 3. If you have to use Python 2, it is recommended using version 2.7, writing code as close to Python 3 as possible, and using tools like &amp;#039;&amp;#039;2to3&amp;#039;&amp;#039; to port to Python 3. Alternatively there is a Python compatibility packages called &amp;#039;&amp;#039;six&amp;#039;&amp;#039;.&amp;lt;/ref&amp;gt;, and some legacy code exists only as Python 2. Some differences between the two versions are discussed in the footnotes.&lt;br /&gt;
&lt;br /&gt;
= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list/range to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python 3. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. The advantage of a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; object over a Python &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;list&amp;lt;/source&amp;gt; is that every single integer value is not stored in memory with a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;. &amp;lt;ref&amp;gt;In Python 3 the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object. However the Python 2 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a list, i.e. stores every integer value required in memory which is very inefficient if simply looping through a long sequence of integers in a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for&amp;lt;/source&amp;gt; loop. Python 2 has &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;xrange&amp;lt;/source&amp;gt; that behaves like the Python 3 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;.&amp;lt;/ref&amp;gt;. For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 0 and 1 respectively for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 0, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at Python equivalents of the MATLAB &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics can be found on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows (a description of how to run this code is given towards the end of this page).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
This is a good point to mention that the behaviour of integer division changed in Python 3, compared to version 2. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm. Remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, these are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;. Namespaces are useful because they allow package writers to choose functions and variable names without worrying about whether that name has been used elsewhere. For example, NumPy includes a function called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt;, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespaces within the function call, e.g. to use &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; at the Python command prompt (to generate 5 random numbers)&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, rather than assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;does not copy&amp;#039;&amp;#039;&amp;#039; values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; to a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array of values. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; does copy all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
NumPy array (and Python list) slices work in subtly different ways to MATLAB&amp;#039;s too. For example, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A[m:n]&amp;lt;/source&amp;gt; returns all values from the element with the index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;m&amp;lt;/source&amp;gt; to the element with index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;n-1&amp;lt;/source&amp;gt;, and because the first element has index 0, we receive the (m+1)&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; to n&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; values, e.g. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[0:10]&lt;br /&gt;
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[4:6]&lt;br /&gt;
[5, 6]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Compare this to MATLAB&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
r =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(1:10)&lt;br /&gt;
ans =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(4:6)&lt;br /&gt;
ans =&lt;br /&gt;
     4     5     6&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on whole arrays are much faster than the equivalent code using loops, as they allow optimal use of the processor (such code optimisation is often called vectorisation). In addition code using vector and matrix operations is often shorter and easier to read that the equivalent using loops.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, this operation is much quicker than growing arrays on-the-fly. In this example we preallocate two arrays at the Python prompt with 10,000 elements each, the first array contains integers and the second contains double precision floating point numbers.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced example ===&lt;br /&gt;
We now look at the Python equivalent of the code on the MATLAB page in the section entitled [[Program_Flow_and_Logicals#Relevant_example|Relevant example]], which assumed we have &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; returns in a vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to:&lt;br /&gt;
&lt;br /&gt;
# Count the number of positive, negative and zero returns&lt;br /&gt;
# Create an array holding only the positive values&lt;br /&gt;
# Create another array holding only the negative values&lt;br /&gt;
# Compute the means of the positive and negative returns&lt;br /&gt;
&lt;br /&gt;
A naive Python algorithm that uses a loop rather than vectorisation is as follows.&lt;br /&gt;
# Find the length of the NumPy array holding  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=numpy.size(r)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus=0; Tzero=0; Tminus=0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate two sum variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;psum=0.0; nsum=0.0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Preallocate NumPy arrays &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=numpy.zeros(T)&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=numpy.zeros(T)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe)&lt;br /&gt;
# Set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;i=0&amp;lt;/source&amp;gt; &lt;br /&gt;
# Check whether &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; is greater, smaller or equal to 0&lt;br /&gt;
#* If &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;gt;0&amp;lt;/source&amp;gt;, set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus[Tplus]=r[i]&amp;lt;/source&amp;gt;, add &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;psum&amp;lt;/source&amp;gt;, and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;0&amp;lt;/source&amp;gt; set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus[Tminus]=r[i]&amp;lt;/source&amp;gt;, add &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;nsum&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Repeat 6 for &amp;lt;math&amp;gt;i=1,\ldots,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove spare zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=rplus[0:Tplus].copy()&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=rminus[0:Tminus].copy()&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus (the number of positive, negative and zero returns are stored in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;)&lt;br /&gt;
&lt;br /&gt;
The Python code is as follows, however note that this code isn&amp;#039;t completely free of vector operations, since removal of zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; is vectorised.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
T=numpy.size(r)&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0&lt;br /&gt;
psum=0.0;nsum=0.0&lt;br /&gt;
rplus=numpy.zeros(T);rminus=numpy.zeros(T)&lt;br /&gt;
for i in range(T):&lt;br /&gt;
   if r[i]&amp;gt;0:&lt;br /&gt;
      rplus[Tplus]=r[i]   #Store positive return in array rplus&lt;br /&gt;
      Tplus+=1            #Increase Tplus by one if return is positive&lt;br /&gt;
      psum+=r[i]          #Add return to sum of positive values&lt;br /&gt;
   elif r[i]&amp;lt;0:&lt;br /&gt;
      rminus[Tminus]=r[i] #Store negative return in array rminus&lt;br /&gt;
      Tminus+=1           #Increase Tminus by one if return is negative&lt;br /&gt;
      nsum+=r[i]          #Add return to sum of negative values&lt;br /&gt;
   else:&lt;br /&gt;
      Tzero+=1            #Increase Tzero by one if return is zero&lt;br /&gt;
rplus=rplus[0:Tplus].copy()      #Remove zeros from rplus&lt;br /&gt;
rminus=rminus[1:Tminus].copy()   #Remove zeros from rminus&lt;br /&gt;
meanplus=psum/Tplus              # Compute mean of positive returns &lt;br /&gt;
meanminus=nsum/Tminus            # Compute mean of negative returns &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
We can create an alternative algorithm that only uses vector operations, using the following algorithm.&lt;br /&gt;
# Create an array &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; containing the positive values from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;&lt;br /&gt;
# Create an array &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; containing the negative values from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the length of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and assign to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the length of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; and assign to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Calculate &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the mean of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; using vectorised functions&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
rminus=r[r&amp;lt;0].copy()         # Create an array containing negative returns&lt;br /&gt;
Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This version is much shorter and cleaner, and therefore easier to create and maintain.&lt;br /&gt;
&lt;br /&gt;
== Running Python programs ==&lt;br /&gt;
For people who are familiar with MATLAB it may be surprising to discover there is no simple way of running a Python program from within Python. If you want to run Python code using the standard Python interpreter, your choices are either&lt;br /&gt;
# Launch it from outside Python, e.g. save to a file &amp;lt;code&amp;gt;myscript.py&amp;lt;/code&amp;gt; and at the command line enter &amp;lt;code&amp;gt;python myscript.py&amp;lt;/code&amp;gt;&lt;br /&gt;
# Convert the program to a function and use the [http://docs.python.org/3/tutorial/modules.html Python module functionality], e.g. save the function to a file &amp;lt;code&amp;gt;myfunctions.py&amp;lt;/code&amp;gt; and use Python&amp;#039;s &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;import&amp;lt;/source&amp;gt; to make the functions available.&lt;br /&gt;
&lt;br /&gt;
The first method can be demonstrated by creating a text file &amp;lt;code&amp;gt;ReturnAnalysis.py&amp;lt;/code&amp;gt; containing the following program.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
n=500000000&lt;br /&gt;
r=numpy.random.rand(n)*10-5&lt;br /&gt;
&lt;br /&gt;
import time&lt;br /&gt;
time1 = time.clock()&lt;br /&gt;
rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
rminus=r[r&amp;lt;0].copy()        # Create an array containing negative returns&lt;br /&gt;
Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
time2 = time.clock()&lt;br /&gt;
print(time2-time1)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In this example the array of values &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt; is generated using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rand&amp;lt;/source&amp;gt; function, in a real scenario these values might be loaded from a file. To run this from the &amp;#039;&amp;#039;&amp;#039;operating system command line&amp;#039;&amp;#039;&amp;#039; we can enter &amp;lt;code&amp;gt;python ReturnAnalysis.py&amp;lt;/code&amp;gt;. Note that this program outputs how long it takes to run, and on my desktop takes around 12.3s to complete (using the Anaconda Python distribution with the Accelerate package).&lt;br /&gt;
&lt;br /&gt;
Using the second method we can create a function, for example the following that undertakes the computation and returns the values of the two means. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;def returnanalysis(r):&lt;br /&gt;
   import numpy&lt;br /&gt;
   n=len(r)&lt;br /&gt;
   rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
   rminus=r[r&amp;lt;0].copy()        # Create an array containing negative returns&lt;br /&gt;
   Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
   Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
   Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
   meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
   meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
   return meanplus, meanminus&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
If this is saved to a file called &amp;lt;code&amp;gt;myfunctions.py&amp;lt;/code&amp;gt;, after importing we can use the function from the Python prompt as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; n=500000000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=numpy.random.rand(n)*10-5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import myfunctions&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; mplus, mminus = myfunctions.returnanalysis(r)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; mplus&lt;br /&gt;
1.7391992566830077&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; mminus&lt;br /&gt;
-2.7456183947241075&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The Python prompt has various other limitations which mean it isn&amp;#039;t ideal for interactive work. For example, it doesn&amp;#039;t include commands like &amp;lt;source enclose=none&amp;gt;pwd&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;cd&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;pwd&amp;lt;/source&amp;gt;, etc. An improved command line is included in [http://ipython.org/ IPython (Interactive Python)] which behaves far more like MATLAB. For example if we save the first Python code snippet from the [[Python/Program_Flow_and_Logicals#More_advanced_example|&amp;#039;&amp;#039;&amp;#039;More advanced Example&amp;#039;&amp;#039;&amp;#039; section (see above)]] to a file called &amp;lt;code&amp;gt;ReturnAnalysis1.py&amp;lt;/code&amp;gt;, we can execute this program from within IPython using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;run&amp;lt;/source&amp;gt;. For MATLAB-like behaviour, where a script can see the variables in the interactive namespace, we need to use &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;run&amp;lt;/source&amp;gt; with the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;-i&amp;lt;/source&amp;gt; flag. The &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;-t&amp;lt;/source&amp;gt; flag is useful too, as it times how long the script takes to run.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
In [1]: n=500000000&lt;br /&gt;
In [2]: from numpy.random import rand&lt;br /&gt;
In [3]: r=rand(n)*10-5&lt;br /&gt;
In [4]: run -i -t ReturnAnalysis1&lt;br /&gt;
&lt;br /&gt;
IPython CPU timings (estimated):&lt;br /&gt;
  User   :     978.66 s.&lt;br /&gt;
  System :       0.00 s.&lt;br /&gt;
Wall time:     978.71 s.&lt;br /&gt;
&lt;br /&gt;
In [5]: meanplus&lt;br /&gt;
Out[5]: 2.5001402997170192&lt;br /&gt;
&lt;br /&gt;
In [6]: meanminus&lt;br /&gt;
Out[6]: -2.5000714107736286&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The above example used the unvectorised version, and to demonstrate the importance of vectorisation in getting good performance we compare with the vectorised version (saved in &amp;lt;code&amp;gt;ReturnAnalysis2.py&amp;lt;/code&amp;gt;).&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
In [7]: run -i -t ReturnAnalysis2&lt;br /&gt;
&lt;br /&gt;
IPython CPU timings (estimated):&lt;br /&gt;
  User   :      12.18 s.&lt;br /&gt;
  System :       0.00 s.&lt;br /&gt;
Wall time:      12.18 s.&lt;br /&gt;
&lt;br /&gt;
In [8]: meanplus&lt;br /&gt;
Out[8]: 2.5001402997170192&lt;br /&gt;
&lt;br /&gt;
In [9]: meanminus&lt;br /&gt;
Out[9]: -2.5000714107736286&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Finally to compare with MATLAB, when running the vectorised MATLAB version (saved to a file called &amp;lt;code&amp;gt;ReturnAnalysis2.m&amp;lt;/code&amp;gt;, the run time is as follows.&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; n=500000000;&lt;br /&gt;
&amp;gt;&amp;gt; r=rand(n,1)*10-5;&lt;br /&gt;
&amp;gt;&amp;gt; tic,ReturnAnalysis2,toc&lt;br /&gt;
Elapsed time is 11.193218 seconds.&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3215</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3215"/>
				<updated>2013-10-16T13:16:48Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* Using Python Packages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The following assumes use of Python version 3, as opposed to Python 2. No more major releases are planned for Python 2, and so version 3 is expected to be the future of Python. The two versions of Python, although similar, are not compatible in a forwards or backwards direction&amp;lt;ref&amp;gt;Although Python 2 and 3 are not totally compatible, Python 2.7 is close to Python 3. If you have to use Python 2, it is recommended using version 2.7, writing code as close to Python 3 as possible, and using tools like &amp;#039;&amp;#039;2to3&amp;#039;&amp;#039; to port to Python 3. Alternatively there is a Python compatibility packages called &amp;#039;&amp;#039;six&amp;#039;&amp;#039;.&amp;lt;/ref&amp;gt;, and some legacy code exists only as Python 2. Some differences between the two versions are discussed in the footnotes.&lt;br /&gt;
&lt;br /&gt;
= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list/range to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python 3. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. The advantage of a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; object over a Python &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;list&amp;lt;/source&amp;gt; is that every single integer value is not stored in memory with a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;. &amp;lt;ref&amp;gt;In Python 3 the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object. However the Python 2 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a list, i.e. stores every integer value required in memory which is very inefficient if simply looping through a long sequence of integers in a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for&amp;lt;/source&amp;gt; loop. Python 2 has &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;xrange&amp;lt;/source&amp;gt; that behaves like the Python 3 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;.&amp;lt;/ref&amp;gt;. For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 0 and 1 respectively for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 0, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at Python equivalents of the MATLAB &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics can be found on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows (a description of how to run this code is given towards the end of this page).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
This is a good point to mention that the behaviour of integer division changed in Python 3, compared to version 2. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm. Remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, these are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;. Namespaces are useful because they allow package writers to choose functions and variable names without worrying about whether that name has been used elsewhere. For example, NumPy includes a function called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt;, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespaces within the function call, e.g. to use &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; at the Python command prompt (to generate 5 random numbers)&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, rather than assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;does not copy&amp;#039;&amp;#039;&amp;#039; values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; to a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array of values. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; does copy all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
NumPy array (and Python list) slices work in subtly different ways to MATLAB&amp;#039;s too. For example, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A[m:n]&amp;lt;/source&amp;gt; returns all values from the element with the index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;m&amp;lt;/source&amp;gt; to the element with index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;n-1&amp;lt;/source&amp;gt;, and because the first element has index 0, we receive the (m+1)&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; to n&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; values, e.g. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[0:10]&lt;br /&gt;
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[4:6]&lt;br /&gt;
[5, 6]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Compare this to MATLAB&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
r =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(1:10)&lt;br /&gt;
ans =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(4:6)&lt;br /&gt;
ans =&lt;br /&gt;
     4     5     6&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on whole arrays are much faster than the equivalent code using loops, as they allow optimal use of the processor (such code optimisation is often called vectorisation). In addition code using vector and matrix operations is often shorter and easier to read that the equivalent using loops.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, this operation is much quicker than growing arrays on-the-fly. In this example we preallocate two arrays at the Python prompt with 10,000 elements each, the first array contains integers and the second contains double precision floating point numbers.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced example ===&lt;br /&gt;
We now look at the Python equivalent of the [[Program_Flow_and_Logicals#Relevant_example|Relevant example on the MATLAB page]], which assumes we have &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; returns in a vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to:&lt;br /&gt;
&lt;br /&gt;
# Count the number of positive, negative and zero returns&lt;br /&gt;
# Create an array holding only the positive values&lt;br /&gt;
# Create another array holding only the negative values&lt;br /&gt;
# Compute the means of the positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The naive algorithm using a loop in Python is as follows.&lt;br /&gt;
# Find the length of the NumPy array holding  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=numpy.size(r)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus=0; Tzero=0; Tminus=0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate two sum variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;psum=0.0; nsum=0.0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Preallocate NumPy arrays &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=numpy.zeros(T)&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=numpy.zeros(T)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe)&lt;br /&gt;
# Set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;i=0&amp;lt;/source&amp;gt; &lt;br /&gt;
# Check whether &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; is greater, smaller or equal to 0&lt;br /&gt;
#* If &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;gt;0&amp;lt;/source&amp;gt;, set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus[Tplus]=r[i]&amp;lt;/source&amp;gt;, add &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;psum&amp;lt;/source&amp;gt;, and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;0&amp;lt;/source&amp;gt; set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus[Tminus]=r[i]&amp;lt;/source&amp;gt;, add &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;nsum&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Repeat 6 for &amp;lt;math&amp;gt;i=1,\ldots,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove spare zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=rplus[0:Tplus].copy()&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=rminus[0:Tminus].copy()&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus (the number of positive, negative and zero returns are stored in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;)&lt;br /&gt;
&lt;br /&gt;
The Python code is as follows, however note that this code isn&amp;#039;t completely free of vector operations, since removal of zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; is vectorised.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
T=numpy.size(r)&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0&lt;br /&gt;
psum=0.0;nsum=0.0&lt;br /&gt;
rplus=numpy.zeros(T);rminus=numpy.zeros(T)&lt;br /&gt;
for i in range(T):&lt;br /&gt;
   if r[i]&amp;gt;0:&lt;br /&gt;
      rplus[Tplus]=r[i]   #Store positive return in array rplus&lt;br /&gt;
      Tplus+=1            #Increase Tplus by one if return is positive&lt;br /&gt;
      psum+=r[i]          #Add return to sum of positive values&lt;br /&gt;
   elif r[i]&amp;lt;0:&lt;br /&gt;
      rminus[Tminus]=r[i] #Store negative return in array rminus&lt;br /&gt;
      Tminus+=1           #Increase Tminus by one if return is negative&lt;br /&gt;
      nsum+=r[i]          #Add return to sum of negative values&lt;br /&gt;
   else:&lt;br /&gt;
      Tzero+=1            #Increase Tzero by one if return is zero&lt;br /&gt;
rplus=rplus[0:Tplus].copy()      #Remove zeros from rplus&lt;br /&gt;
rminus=rminus[1:Tminus].copy()   #Remove zeros from rminus&lt;br /&gt;
meanplus=psum/Tplus              # Compute mean of positive returns &lt;br /&gt;
meanminus=nsum/Tminus            # Compute mean of negative returns &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
We can create an alternative algorithm that only uses vector operations, using the following algorithm.&lt;br /&gt;
# Create an array &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; containing the positive values from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;&lt;br /&gt;
# Create an array &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; containing the negative values from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the length of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and assign to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the length of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; and assign to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Calculate &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the mean of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; using vectorised functions&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
rminus=r[r&amp;lt;0].copy()         # Create an array containing negative returns&lt;br /&gt;
Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This version is much shorter and cleaner, and therefore easier to create and maintain.&lt;br /&gt;
&lt;br /&gt;
== Running Python programs ==&lt;br /&gt;
For people who are familiar with MATLAB it may be surprising to discover there is no simple way of running a Python program from within Python. If you want to run Python code using the standard Python interpreter, your choices are either&lt;br /&gt;
# Launch it from outside Python, e.g. save to a file &amp;lt;code&amp;gt;myscript.py&amp;lt;/code&amp;gt; and at the command line enter &amp;lt;code&amp;gt;python myscript.py&amp;lt;/code&amp;gt;&lt;br /&gt;
# Convert the program to a function and use the [http://docs.python.org/3/tutorial/modules.html Python module functionality], e.g. save the function to a file &amp;lt;code&amp;gt;myfunctions.py&amp;lt;/code&amp;gt; and use Python&amp;#039;s &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;import&amp;lt;/source&amp;gt; to make the functions available.&lt;br /&gt;
&lt;br /&gt;
The first method can be demonstrated by creating a text file &amp;lt;code&amp;gt;ReturnAnalysis.py&amp;lt;/code&amp;gt; containing the following program.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
n=500000000&lt;br /&gt;
r=numpy.random.rand(n)*10-5&lt;br /&gt;
&lt;br /&gt;
import time&lt;br /&gt;
time1 = time.clock()&lt;br /&gt;
rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
rminus=r[r&amp;lt;0].copy()        # Create an array containing negative returns&lt;br /&gt;
Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
time2 = time.clock()&lt;br /&gt;
print(time2-time1)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In this example the array of values &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt; is generated using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rand&amp;lt;/source&amp;gt; function, in a real scenario these values might be loaded from a file. To run this from the &amp;#039;&amp;#039;&amp;#039;operating system command line&amp;#039;&amp;#039;&amp;#039; we can enter &amp;lt;code&amp;gt;python ReturnAnalysis.py&amp;lt;/code&amp;gt;. Note that this program outputs how long it takes to run, and on my desktop takes around 12.3s to complete (using the Anaconda Python distribution with the Accelerate package).&lt;br /&gt;
&lt;br /&gt;
Using the second method we can create a function, for example the following that undertakes the computation and returns the values of the two means. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;def returnanalysis(r):&lt;br /&gt;
   import numpy&lt;br /&gt;
   n=len(r)&lt;br /&gt;
   rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
   rminus=r[r&amp;lt;0].copy()        # Create an array containing negative returns&lt;br /&gt;
   Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
   Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
   Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
   meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
   meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
   return meanplus, meanminus&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
If this is saved to a file called &amp;lt;code&amp;gt;myfunctions.py&amp;lt;/code&amp;gt;, after importing we can use the function from the Python prompt as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; n=500000000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=numpy.random.rand(n)*10-5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import myfunctions&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; mplus, mminus = myfunctions.returnanalysis(r)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; mplus&lt;br /&gt;
1.7391992566830077&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; mminus&lt;br /&gt;
-2.7456183947241075&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The Python prompt has various other limitations which mean it isn&amp;#039;t ideal for interactive work. For example, it doesn&amp;#039;t include commands like &amp;lt;source enclose=none&amp;gt;pwd&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;cd&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;pwd&amp;lt;/source&amp;gt;, etc. An improved command line is included in [http://ipython.org/ IPython (Interactive Python)] which behaves far more like MATLAB. For example if we save the first Python code snippet from the [[Python/Program_Flow_and_Logicals#More_advanced_example|&amp;#039;&amp;#039;&amp;#039;More advanced Example&amp;#039;&amp;#039;&amp;#039; section (see above)]] to a file called &amp;lt;code&amp;gt;ReturnAnalysis1.py&amp;lt;/code&amp;gt;, we can execute this program from within IPython using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;run&amp;lt;/source&amp;gt;. For MATLAB-like behaviour, where a script can see the variables in the interactive namespace, we need to use &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;run&amp;lt;/source&amp;gt; with the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;-i&amp;lt;/source&amp;gt; flag. The &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;-t&amp;lt;/source&amp;gt; flag is useful too, as it times how long the script takes to run.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
In [1]: n=500000000&lt;br /&gt;
In [2]: from numpy.random import rand&lt;br /&gt;
In [3]: r=rand(n)*10-5&lt;br /&gt;
In [4]: run -i -t ReturnAnalysis1&lt;br /&gt;
&lt;br /&gt;
IPython CPU timings (estimated):&lt;br /&gt;
  User   :     978.66 s.&lt;br /&gt;
  System :       0.00 s.&lt;br /&gt;
Wall time:     978.71 s.&lt;br /&gt;
&lt;br /&gt;
In [5]: meanplus&lt;br /&gt;
Out[5]: 2.5001402997170192&lt;br /&gt;
&lt;br /&gt;
In [6]: meanminus&lt;br /&gt;
Out[6]: -2.5000714107736286&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The above example used the unvectorised version, and to demonstrate the importance of vectorisation in getting good performance we compare with the vectorised version (saved in &amp;lt;code&amp;gt;ReturnAnalysis2.py&amp;lt;/code&amp;gt;).&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
In [7]: run -i -t ReturnAnalysis2&lt;br /&gt;
&lt;br /&gt;
IPython CPU timings (estimated):&lt;br /&gt;
  User   :      12.18 s.&lt;br /&gt;
  System :       0.00 s.&lt;br /&gt;
Wall time:      12.18 s.&lt;br /&gt;
&lt;br /&gt;
In [8]: meanplus&lt;br /&gt;
Out[8]: 2.5001402997170192&lt;br /&gt;
&lt;br /&gt;
In [9]: meanminus&lt;br /&gt;
Out[9]: -2.5000714107736286&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Finally to compare with MATLAB, when running the vectorised MATLAB version (saved to a file called &amp;lt;code&amp;gt;ReturnAnalysis2.m&amp;lt;/code&amp;gt;, the run time is as follows.&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; n=500000000;&lt;br /&gt;
&amp;gt;&amp;gt; r=rand(n,1)*10-5;&lt;br /&gt;
&amp;gt;&amp;gt; tic,ReturnAnalysis2,toc&lt;br /&gt;
Elapsed time is 11.193218 seconds.&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3214</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3214"/>
				<updated>2013-10-16T13:14:16Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* Improvements on the above (avoiding loops) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The following assumes use of Python version 3, as opposed to Python 2. No more major releases are planned for Python 2, and so version 3 is expected to be the future of Python. The two versions of Python, although similar, are not compatible in a forwards or backwards direction&amp;lt;ref&amp;gt;Although Python 2 and 3 are not totally compatible, Python 2.7 is close to Python 3. If you have to use Python 2, it is recommended using version 2.7, writing code as close to Python 3 as possible, and using tools like &amp;#039;&amp;#039;2to3&amp;#039;&amp;#039; to port to Python 3. Alternatively there is a Python compatibility packages called &amp;#039;&amp;#039;six&amp;#039;&amp;#039;.&amp;lt;/ref&amp;gt;, and some legacy code exists only as Python 2. Some differences between the two versions are discussed in the footnotes.&lt;br /&gt;
&lt;br /&gt;
= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list/range to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python 3. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. The advantage of a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; object over a Python &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;list&amp;lt;/source&amp;gt; is that every single integer value is not stored in memory with a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;. &amp;lt;ref&amp;gt;In Python 3 the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object. However the Python 2 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a list, i.e. stores every integer value required in memory which is very inefficient if simply looping through a long sequence of integers in a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for&amp;lt;/source&amp;gt; loop. Python 2 has &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;xrange&amp;lt;/source&amp;gt; that behaves like the Python 3 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;.&amp;lt;/ref&amp;gt;. For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 0 and 1 respectively for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 0, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at Python equivalents of the MATLAB &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics can be found on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows (a description of how to run this code is given towards the end of this page).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
This is a good point to mention that the behaviour of integer division changed in Python 3, compared to version 2. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm. Remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;. Namespaces are useful because they allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a function called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt;, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespaces within the function call, e.g. to use &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; at the Python command prompt to generate 5 random numbers&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, rather than assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;does not copy&amp;#039;&amp;#039;&amp;#039; values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; to a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array of values. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; does copy all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
NumPy array (and Python list) slices work in subtly different ways to MATLAB&amp;#039;s too. For example, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A[m:n]&amp;lt;/source&amp;gt; returns all values from the element with the index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;m&amp;lt;/source&amp;gt; to the element with index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;n-1&amp;lt;/source&amp;gt;, and because the first element has index 0, we receive the (m+1)&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; to n&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; values, e.g. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[0:10]&lt;br /&gt;
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[4:6]&lt;br /&gt;
[5, 6]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Compare this to MATLAB&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
r =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(1:10)&lt;br /&gt;
ans =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(4:6)&lt;br /&gt;
ans =&lt;br /&gt;
     4     5     6&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on whole arrays are much faster than the equivalent code using loops, as they allow optimal use of the processor (such code optimisation is often called vectorisation). In addition code using vector and matrix operations is often shorter and easier to read that the equivalent using loops.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, this operation is much quicker than growing arrays on-the-fly. In this example we preallocate two arrays at the Python prompt with 10,000 elements each, the first array contains integers and the second contains double precision floating point numbers.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced example ===&lt;br /&gt;
We now look at the Python equivalent of the [[Program_Flow_and_Logicals#Relevant_example|Relevant example on the MATLAB page]], which assumes we have &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; returns in a vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to:&lt;br /&gt;
&lt;br /&gt;
# Count the number of positive, negative and zero returns&lt;br /&gt;
# Create an array holding only the positive values&lt;br /&gt;
# Create another array holding only the negative values&lt;br /&gt;
# Compute the means of the positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The naive algorithm using a loop in Python is as follows.&lt;br /&gt;
# Find the length of the NumPy array holding  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=numpy.size(r)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus=0; Tzero=0; Tminus=0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate two sum variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;psum=0.0; nsum=0.0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Preallocate NumPy arrays &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=numpy.zeros(T)&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=numpy.zeros(T)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe)&lt;br /&gt;
# Set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;i=0&amp;lt;/source&amp;gt; &lt;br /&gt;
# Check whether &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; is greater, smaller or equal to 0&lt;br /&gt;
#* If &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;gt;0&amp;lt;/source&amp;gt;, set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus[Tplus]=r[i]&amp;lt;/source&amp;gt;, add &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;psum&amp;lt;/source&amp;gt;, and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;0&amp;lt;/source&amp;gt; set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus[Tminus]=r[i]&amp;lt;/source&amp;gt;, add &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;nsum&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Repeat 6 for &amp;lt;math&amp;gt;i=1,\ldots,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove spare zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=rplus[0:Tplus].copy()&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=rminus[0:Tminus].copy()&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus (the number of positive, negative and zero returns are stored in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;)&lt;br /&gt;
&lt;br /&gt;
The Python code is as follows, however note that this code isn&amp;#039;t completely free of vector operations, since removal of zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; is vectorised.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
T=numpy.size(r)&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0&lt;br /&gt;
psum=0.0;nsum=0.0&lt;br /&gt;
rplus=numpy.zeros(T);rminus=numpy.zeros(T)&lt;br /&gt;
for i in range(T):&lt;br /&gt;
   if r[i]&amp;gt;0:&lt;br /&gt;
      rplus[Tplus]=r[i]   #Store positive return in array rplus&lt;br /&gt;
      Tplus+=1            #Increase Tplus by one if return is positive&lt;br /&gt;
      psum+=r[i]          #Add return to sum of positive values&lt;br /&gt;
   elif r[i]&amp;lt;0:&lt;br /&gt;
      rminus[Tminus]=r[i] #Store negative return in array rminus&lt;br /&gt;
      Tminus+=1           #Increase Tminus by one if return is negative&lt;br /&gt;
      nsum+=r[i]          #Add return to sum of negative values&lt;br /&gt;
   else:&lt;br /&gt;
      Tzero+=1            #Increase Tzero by one if return is zero&lt;br /&gt;
rplus=rplus[0:Tplus].copy()      #Remove zeros from rplus&lt;br /&gt;
rminus=rminus[1:Tminus].copy()   #Remove zeros from rminus&lt;br /&gt;
meanplus=psum/Tplus              # Compute mean of positive returns &lt;br /&gt;
meanminus=nsum/Tminus            # Compute mean of negative returns &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
We can create an alternative algorithm that only uses vector operations, using the following algorithm.&lt;br /&gt;
# Create an array &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; containing the positive values from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;&lt;br /&gt;
# Create an array &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; containing the negative values from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the length of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and assign to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the length of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; and assign to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Calculate &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the mean of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; using vectorised functions&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
rminus=r[r&amp;lt;0].copy()         # Create an array containing negative returns&lt;br /&gt;
Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This version is much shorter and cleaner, and therefore easier to create and maintain.&lt;br /&gt;
&lt;br /&gt;
== Running Python programs ==&lt;br /&gt;
For people who are familiar with MATLAB it may be surprising to discover there is no simple way of running a Python program from within Python. If you want to run Python code using the standard Python interpreter, your choices are either&lt;br /&gt;
# Launch it from outside Python, e.g. save to a file &amp;lt;code&amp;gt;myscript.py&amp;lt;/code&amp;gt; and at the command line enter &amp;lt;code&amp;gt;python myscript.py&amp;lt;/code&amp;gt;&lt;br /&gt;
# Convert the program to a function and use the [http://docs.python.org/3/tutorial/modules.html Python module functionality], e.g. save the function to a file &amp;lt;code&amp;gt;myfunctions.py&amp;lt;/code&amp;gt; and use Python&amp;#039;s &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;import&amp;lt;/source&amp;gt; to make the functions available.&lt;br /&gt;
&lt;br /&gt;
The first method can be demonstrated by creating a text file &amp;lt;code&amp;gt;ReturnAnalysis.py&amp;lt;/code&amp;gt; containing the following program.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
n=500000000&lt;br /&gt;
r=numpy.random.rand(n)*10-5&lt;br /&gt;
&lt;br /&gt;
import time&lt;br /&gt;
time1 = time.clock()&lt;br /&gt;
rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
rminus=r[r&amp;lt;0].copy()        # Create an array containing negative returns&lt;br /&gt;
Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
time2 = time.clock()&lt;br /&gt;
print(time2-time1)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In this example the array of values &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt; is generated using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rand&amp;lt;/source&amp;gt; function, in a real scenario these values might be loaded from a file. To run this from the &amp;#039;&amp;#039;&amp;#039;operating system command line&amp;#039;&amp;#039;&amp;#039; we can enter &amp;lt;code&amp;gt;python ReturnAnalysis.py&amp;lt;/code&amp;gt;. Note that this program outputs how long it takes to run, and on my desktop takes around 12.3s to complete (using the Anaconda Python distribution with the Accelerate package).&lt;br /&gt;
&lt;br /&gt;
Using the second method we can create a function, for example the following that undertakes the computation and returns the values of the two means. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;def returnanalysis(r):&lt;br /&gt;
   import numpy&lt;br /&gt;
   n=len(r)&lt;br /&gt;
   rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
   rminus=r[r&amp;lt;0].copy()        # Create an array containing negative returns&lt;br /&gt;
   Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
   Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
   Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
   meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
   meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
   return meanplus, meanminus&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
If this is saved to a file called &amp;lt;code&amp;gt;myfunctions.py&amp;lt;/code&amp;gt;, after importing we can use the function from the Python prompt as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; n=500000000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=numpy.random.rand(n)*10-5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import myfunctions&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; mplus, mminus = myfunctions.returnanalysis(r)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; mplus&lt;br /&gt;
1.7391992566830077&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; mminus&lt;br /&gt;
-2.7456183947241075&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The Python prompt has various other limitations which mean it isn&amp;#039;t ideal for interactive work. For example, it doesn&amp;#039;t include commands like &amp;lt;source enclose=none&amp;gt;pwd&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;cd&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;pwd&amp;lt;/source&amp;gt;, etc. An improved command line is included in [http://ipython.org/ IPython (Interactive Python)] which behaves far more like MATLAB. For example if we save the first Python code snippet from the [[Python/Program_Flow_and_Logicals#More_advanced_example|&amp;#039;&amp;#039;&amp;#039;More advanced Example&amp;#039;&amp;#039;&amp;#039; section (see above)]] to a file called &amp;lt;code&amp;gt;ReturnAnalysis1.py&amp;lt;/code&amp;gt;, we can execute this program from within IPython using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;run&amp;lt;/source&amp;gt;. For MATLAB-like behaviour, where a script can see the variables in the interactive namespace, we need to use &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;run&amp;lt;/source&amp;gt; with the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;-i&amp;lt;/source&amp;gt; flag. The &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;-t&amp;lt;/source&amp;gt; flag is useful too, as it times how long the script takes to run.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
In [1]: n=500000000&lt;br /&gt;
In [2]: from numpy.random import rand&lt;br /&gt;
In [3]: r=rand(n)*10-5&lt;br /&gt;
In [4]: run -i -t ReturnAnalysis1&lt;br /&gt;
&lt;br /&gt;
IPython CPU timings (estimated):&lt;br /&gt;
  User   :     978.66 s.&lt;br /&gt;
  System :       0.00 s.&lt;br /&gt;
Wall time:     978.71 s.&lt;br /&gt;
&lt;br /&gt;
In [5]: meanplus&lt;br /&gt;
Out[5]: 2.5001402997170192&lt;br /&gt;
&lt;br /&gt;
In [6]: meanminus&lt;br /&gt;
Out[6]: -2.5000714107736286&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The above example used the unvectorised version, and to demonstrate the importance of vectorisation in getting good performance we compare with the vectorised version (saved in &amp;lt;code&amp;gt;ReturnAnalysis2.py&amp;lt;/code&amp;gt;).&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
In [7]: run -i -t ReturnAnalysis2&lt;br /&gt;
&lt;br /&gt;
IPython CPU timings (estimated):&lt;br /&gt;
  User   :      12.18 s.&lt;br /&gt;
  System :       0.00 s.&lt;br /&gt;
Wall time:      12.18 s.&lt;br /&gt;
&lt;br /&gt;
In [8]: meanplus&lt;br /&gt;
Out[8]: 2.5001402997170192&lt;br /&gt;
&lt;br /&gt;
In [9]: meanminus&lt;br /&gt;
Out[9]: -2.5000714107736286&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Finally to compare with MATLAB, when running the vectorised MATLAB version (saved to a file called &amp;lt;code&amp;gt;ReturnAnalysis2.m&amp;lt;/code&amp;gt;, the run time is as follows.&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; n=500000000;&lt;br /&gt;
&amp;gt;&amp;gt; r=rand(n,1)*10-5;&lt;br /&gt;
&amp;gt;&amp;gt; tic,ReturnAnalysis2,toc&lt;br /&gt;
Elapsed time is 11.193218 seconds.&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3213</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3213"/>
				<updated>2013-10-16T13:12:45Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* for  */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The following assumes use of Python version 3, as opposed to Python 2. No more major releases are planned for Python 2, and so version 3 is expected to be the future of Python. The two versions of Python, although similar, are not compatible in a forwards or backwards direction&amp;lt;ref&amp;gt;Although Python 2 and 3 are not totally compatible, Python 2.7 is close to Python 3. If you have to use Python 2, it is recommended using version 2.7, writing code as close to Python 3 as possible, and using tools like &amp;#039;&amp;#039;2to3&amp;#039;&amp;#039; to port to Python 3. Alternatively there is a Python compatibility packages called &amp;#039;&amp;#039;six&amp;#039;&amp;#039;.&amp;lt;/ref&amp;gt;, and some legacy code exists only as Python 2. Some differences between the two versions are discussed in the footnotes.&lt;br /&gt;
&lt;br /&gt;
= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list/range to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python 3. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. The advantage of a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; object over a Python &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;list&amp;lt;/source&amp;gt; is that every single integer value is not stored in memory with a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;. &amp;lt;ref&amp;gt;In Python 3 the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object. However the Python 2 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a list, i.e. stores every integer value required in memory which is very inefficient if simply looping through a long sequence of integers in a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for&amp;lt;/source&amp;gt; loop. Python 2 has &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;xrange&amp;lt;/source&amp;gt; that behaves like the Python 3 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;.&amp;lt;/ref&amp;gt;. For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 0 and 1 respectively for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 0, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at Python equivalents of the MATLAB &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics can be found on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows (a description of how to run this code is given towards the end of this page).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
This is a good point to mention that the behaviour of integer division changed in Python 3, compared to version 2. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm. Remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;. Namespaces are useful because they allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a function called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt;, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespaces within the function call, e.g. to use &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; at the Python command prompt to generate 5 random numbers&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, rather than assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;does not copy&amp;#039;&amp;#039;&amp;#039; values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; to a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array of values. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; does copy all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
NumPy array (and Python list) slices work in subtly different ways to MATLAB&amp;#039;s too. For example, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A[m:n]&amp;lt;/source&amp;gt; returns all values from the element with the index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;m&amp;lt;/source&amp;gt; to the element with index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;n-1&amp;lt;/source&amp;gt;, and because the first element has index 0, we receive the (m+1)&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; to n&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; values, e.g. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[0:10]&lt;br /&gt;
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[4:6]&lt;br /&gt;
[5, 6]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Compare this to MATLAB&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
r =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(1:10)&lt;br /&gt;
ans =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(4:6)&lt;br /&gt;
ans =&lt;br /&gt;
     4     5     6&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on whole arrays are much faster than the equivalent code using loops, as they allow optimal use of the processor (such code optimisation is often called vectorisation). In addition code using vector and matrix operations is often shorter and easier to read that the equivalent using loops.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, this operation is much quicker than growing arrays on-the-fly. In this example we preallocate two arrays at the Python prompt with 10,000 elements each, the first array contains integers and the second contains double precision floating point numbers.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced example ===&lt;br /&gt;
We now look at the Python equivalent of the [[Program_Flow_and_Logicals#Relevant_example|Relevant example on the MATLAB page]], which assumes we have &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; returns in a vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to:&lt;br /&gt;
&lt;br /&gt;
# Count the number of positive, negative and zero returns&lt;br /&gt;
# Create an array holding only the positive values&lt;br /&gt;
# Create another array holding only the negative values&lt;br /&gt;
# Compute the means of the positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The naive algorithm using a loop in Python is as follows.&lt;br /&gt;
# Find the length of the NumPy array holding  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=numpy.size(r)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus=0; Tzero=0; Tminus=0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate two sum variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;psum=0.0; nsum=0.0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Preallocate NumPy arrays &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=numpy.zeros(T)&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=numpy.zeros(T)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe)&lt;br /&gt;
# Set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;i=0&amp;lt;/source&amp;gt; &lt;br /&gt;
# Check whether &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; is greater, smaller or equal to 0&lt;br /&gt;
#* If &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;gt;0&amp;lt;/source&amp;gt;, set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus[Tplus]=r[i]&amp;lt;/source&amp;gt;, add &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;psum&amp;lt;/source&amp;gt;, and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;0&amp;lt;/source&amp;gt; set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus[Tminus]=r[i]&amp;lt;/source&amp;gt;, add &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;nsum&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Repeat 6 for &amp;lt;math&amp;gt;i=1,\ldots,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove spare zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=rplus[0:Tplus].copy()&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=rminus[0:Tminus].copy()&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus (the number of positive, negative and zero returns are stored in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;)&lt;br /&gt;
&lt;br /&gt;
The Python code is as follows, however note that this code isn&amp;#039;t completely free of vector operations, since removal of zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; is vectorised.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
T=numpy.size(r)&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0&lt;br /&gt;
psum=0.0;nsum=0.0&lt;br /&gt;
rplus=numpy.zeros(T);rminus=numpy.zeros(T)&lt;br /&gt;
for i in range(T):&lt;br /&gt;
   if r[i]&amp;gt;0:&lt;br /&gt;
      rplus[Tplus]=r[i]   #Store positive return in array rplus&lt;br /&gt;
      Tplus+=1            #Increase Tplus by one if return is positive&lt;br /&gt;
      psum+=r[i]          #Add return to sum of positive values&lt;br /&gt;
   elif r[i]&amp;lt;0:&lt;br /&gt;
      rminus[Tminus]=r[i] #Store negative return in array rminus&lt;br /&gt;
      Tminus+=1           #Increase Tminus by one if return is negative&lt;br /&gt;
      nsum+=r[i]          #Add return to sum of negative values&lt;br /&gt;
   else:&lt;br /&gt;
      Tzero+=1            #Increase Tzero by one if return is zero&lt;br /&gt;
rplus=rplus[0:Tplus].copy()      #Remove zeros from rplus&lt;br /&gt;
rminus=rminus[1:Tminus].copy()   #Remove zeros from rminus&lt;br /&gt;
meanplus=psum/Tplus              # Compute mean of positive returns &lt;br /&gt;
meanminus=nsum/Tminus            # Compute mean of negative returns &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
We can create an alternative algorithm that only uses vector operations, using the following algorithm.&lt;br /&gt;
# Create an array &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; containing the positive values from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;&lt;br /&gt;
# Create an array &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; containing the negative values from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the length of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and assign to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the length of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; and assign to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Calculate &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the mean of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; using vectorised functions&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
rminus=r[r&amp;lt;0].copy()         # Create an array containing negative returns&lt;br /&gt;
Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This version is much shorter and cleaner, and therefore easier to create and maintain.&lt;br /&gt;
&lt;br /&gt;
== Running Python programs ==&lt;br /&gt;
For people who are familiar with MATLAB it may be surprising to discover there is no simple way of running a Python program from within Python. If you want to run Python code using the standard Python interpreter, your choices are either&lt;br /&gt;
# Launch it from outside Python, e.g. save to a file &amp;lt;code&amp;gt;myscript.py&amp;lt;/code&amp;gt; and at the command line enter &amp;lt;code&amp;gt;python myscript.py&amp;lt;/code&amp;gt;&lt;br /&gt;
# Convert the program to a function and use the [http://docs.python.org/3/tutorial/modules.html Python module functionality], e.g. save the function to a file &amp;lt;code&amp;gt;myfunctions.py&amp;lt;/code&amp;gt; and use Python&amp;#039;s &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;import&amp;lt;/source&amp;gt; to make the functions available.&lt;br /&gt;
&lt;br /&gt;
The first method can be demonstrated by creating a text file &amp;lt;code&amp;gt;ReturnAnalysis.py&amp;lt;/code&amp;gt; containing the following program.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
n=500000000&lt;br /&gt;
r=numpy.random.rand(n)*10-5&lt;br /&gt;
&lt;br /&gt;
import time&lt;br /&gt;
time1 = time.clock()&lt;br /&gt;
rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
rminus=r[r&amp;lt;0].copy()        # Create an array containing negative returns&lt;br /&gt;
Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
time2 = time.clock()&lt;br /&gt;
print(time2-time1)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In this example the array of values &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt; is generated using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rand&amp;lt;/source&amp;gt; function, in a real scenario these values might be loaded from a file. To run this from the &amp;#039;&amp;#039;&amp;#039;operating system command line&amp;#039;&amp;#039;&amp;#039; we can enter &amp;lt;code&amp;gt;python ReturnAnalysis.py&amp;lt;/code&amp;gt;. Note that this program outputs how long it takes to run, and on my desktop takes around 12.3s to complete (using the Anaconda Python distribution with the Accelerate package).&lt;br /&gt;
&lt;br /&gt;
Using the second method we can create a function, for example the following that undertakes the computation and returns the values of the two means. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;def returnanalysis(r):&lt;br /&gt;
   import numpy&lt;br /&gt;
   n=len(r)&lt;br /&gt;
   rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
   rminus=r[r&amp;lt;0].copy()        # Create an array containing negative returns&lt;br /&gt;
   Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
   Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
   Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
   meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
   meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
   return meanplus, meanminus&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
If this is saved to a file called &amp;lt;code&amp;gt;myfunctions.py&amp;lt;/code&amp;gt;, after importing we can use the function from the Python prompt as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; n=500000000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=numpy.random.rand(n)*10-5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import myfunctions&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; mplus, mminus = myfunctions.returnanalysis(r)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; mplus&lt;br /&gt;
1.7391992566830077&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; mminus&lt;br /&gt;
-2.7456183947241075&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The Python prompt has various other limitations which mean it isn&amp;#039;t ideal for interactive work. For example, it doesn&amp;#039;t include commands like &amp;lt;source enclose=none&amp;gt;pwd&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;cd&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;pwd&amp;lt;/source&amp;gt;, etc. An improved command line is included in [http://ipython.org/ IPython (Interactive Python)] which behaves far more like MATLAB. For example if we save the first Python code snippet from the [[Python/Program_Flow_and_Logicals#More_advanced_example|&amp;#039;&amp;#039;&amp;#039;More advanced Example&amp;#039;&amp;#039;&amp;#039; section (see above)]] to a file called &amp;lt;code&amp;gt;ReturnAnalysis1.py&amp;lt;/code&amp;gt;, we can execute this program from within IPython using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;run&amp;lt;/source&amp;gt;. For MATLAB-like behaviour, where a script can see the variables in the interactive namespace, we need to use &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;run&amp;lt;/source&amp;gt; with the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;-i&amp;lt;/source&amp;gt; flag. The &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;-t&amp;lt;/source&amp;gt; flag is useful too, as it times how long the script takes to run.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
In [1]: n=500000000&lt;br /&gt;
In [2]: from numpy.random import rand&lt;br /&gt;
In [3]: r=rand(n)*10-5&lt;br /&gt;
In [4]: run -i -t ReturnAnalysis1&lt;br /&gt;
&lt;br /&gt;
IPython CPU timings (estimated):&lt;br /&gt;
  User   :     978.66 s.&lt;br /&gt;
  System :       0.00 s.&lt;br /&gt;
Wall time:     978.71 s.&lt;br /&gt;
&lt;br /&gt;
In [5]: meanplus&lt;br /&gt;
Out[5]: 2.5001402997170192&lt;br /&gt;
&lt;br /&gt;
In [6]: meanminus&lt;br /&gt;
Out[6]: -2.5000714107736286&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The above example used the unvectorised version, and to demonstrate the importance of vectorisation in getting good performance we compare with the vectorised version (saved in &amp;lt;code&amp;gt;ReturnAnalysis2.py&amp;lt;/code&amp;gt;).&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
In [7]: run -i -t ReturnAnalysis2&lt;br /&gt;
&lt;br /&gt;
IPython CPU timings (estimated):&lt;br /&gt;
  User   :      12.18 s.&lt;br /&gt;
  System :       0.00 s.&lt;br /&gt;
Wall time:      12.18 s.&lt;br /&gt;
&lt;br /&gt;
In [8]: meanplus&lt;br /&gt;
Out[8]: 2.5001402997170192&lt;br /&gt;
&lt;br /&gt;
In [9]: meanminus&lt;br /&gt;
Out[9]: -2.5000714107736286&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Finally to compare with MATLAB, when running the vectorised MATLAB version (saved to a file called &amp;lt;code&amp;gt;ReturnAnalysis2.m&amp;lt;/code&amp;gt;, the run time is as follows.&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; n=500000000;&lt;br /&gt;
&amp;gt;&amp;gt; r=rand(n,1)*10-5;&lt;br /&gt;
&amp;gt;&amp;gt; tic,ReturnAnalysis2,toc&lt;br /&gt;
Elapsed time is 11.193218 seconds.&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3212</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3212"/>
				<updated>2013-10-16T13:08:49Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* Preliminaries */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The following assumes use of Python version 3, as opposed to Python 2. No more major releases are planned for Python 2, and so version 3 is expected to be the future of Python. The two versions of Python, although similar, are not compatible in a forwards or backwards direction&amp;lt;ref&amp;gt;Although Python 2 and 3 are not totally compatible, Python 2.7 is close to Python 3. If you have to use Python 2, it is recommended using version 2.7, writing code as close to Python 3 as possible, and using tools like &amp;#039;&amp;#039;2to3&amp;#039;&amp;#039; to port to Python 3. Alternatively there is a Python compatibility packages called &amp;#039;&amp;#039;six&amp;#039;&amp;#039;.&amp;lt;/ref&amp;gt;, and some legacy code exists only as Python 2. Some differences between the two versions are discussed in the footnotes.&lt;br /&gt;
&lt;br /&gt;
= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list/range to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python 3. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. The advantage of a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; object over a Python &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;list&amp;lt;/source&amp;gt; is that every single integer value is not stored in memory with a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;. &amp;lt;ref&amp;gt;In Python 3 the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object. However the Python 2 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a list, i.e. stores every integer value required in memory which is very inefficient if simply looping through a long sequence of integers in a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for&amp;lt;/source&amp;gt; loop. Python 2 has &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;xrange&amp;lt;/source&amp;gt; that behaves like the Python 3 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;.&amp;lt;/ref&amp;gt;. For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 0 and 1 respectively for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 0, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm. Remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;. Namespaces are useful because they allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a function called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt;, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespaces within the function call, e.g. to use &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; at the Python command prompt to generate 5 random numbers&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, rather than assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;does not copy&amp;#039;&amp;#039;&amp;#039; values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; to a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array of values. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; does copy all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
NumPy array (and Python list) slices work in subtly different ways to MATLAB&amp;#039;s too. For example, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A[m:n]&amp;lt;/source&amp;gt; returns all values from the element with the index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;m&amp;lt;/source&amp;gt; to the element with index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;n-1&amp;lt;/source&amp;gt;, and because the first element has index 0, we receive the (m+1)&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; to n&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; values, e.g. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[0:10]&lt;br /&gt;
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[4:6]&lt;br /&gt;
[5, 6]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Compare this to MATLAB&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
r =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(1:10)&lt;br /&gt;
ans =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(4:6)&lt;br /&gt;
ans =&lt;br /&gt;
     4     5     6&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on whole arrays are much faster than the equivalent code using loops, as they allow optimal use of the processor (such code optimisation is often called vectorisation). In addition code using vector and matrix operations is often shorter and easier to read that the equivalent using loops.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, this operation is much quicker than growing arrays on-the-fly. In this example we preallocate two arrays at the Python prompt with 10,000 elements each, the first array contains integers and the second contains double precision floating point numbers.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced example ===&lt;br /&gt;
We now look at the Python equivalent of the [[Program_Flow_and_Logicals#Relevant_example|Relevant example on the MATLAB page]], which assumes we have &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; returns in a vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to:&lt;br /&gt;
&lt;br /&gt;
# Count the number of positive, negative and zero returns&lt;br /&gt;
# Create an array holding only the positive values&lt;br /&gt;
# Create another array holding only the negative values&lt;br /&gt;
# Compute the means of the positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The naive algorithm using a loop in Python is as follows.&lt;br /&gt;
# Find the length of the NumPy array holding  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=numpy.size(r)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus=0; Tzero=0; Tminus=0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate two sum variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;psum=0.0; nsum=0.0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Preallocate NumPy arrays &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=numpy.zeros(T)&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=numpy.zeros(T)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe)&lt;br /&gt;
# Set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;i=0&amp;lt;/source&amp;gt; &lt;br /&gt;
# Check whether &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; is greater, smaller or equal to 0&lt;br /&gt;
#* If &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;gt;0&amp;lt;/source&amp;gt;, set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus[Tplus]=r[i]&amp;lt;/source&amp;gt;, add &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;psum&amp;lt;/source&amp;gt;, and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;0&amp;lt;/source&amp;gt; set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus[Tminus]=r[i]&amp;lt;/source&amp;gt;, add &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;nsum&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Repeat 6 for &amp;lt;math&amp;gt;i=1,\ldots,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove spare zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=rplus[0:Tplus].copy()&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=rminus[0:Tminus].copy()&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus (the number of positive, negative and zero returns are stored in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;)&lt;br /&gt;
&lt;br /&gt;
The Python code is as follows, however note that this code isn&amp;#039;t completely free of vector operations, since removal of zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; is vectorised.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
T=numpy.size(r)&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0&lt;br /&gt;
psum=0.0;nsum=0.0&lt;br /&gt;
rplus=numpy.zeros(T);rminus=numpy.zeros(T)&lt;br /&gt;
for i in range(T):&lt;br /&gt;
   if r[i]&amp;gt;0:&lt;br /&gt;
      rplus[Tplus]=r[i]   #Store positive return in array rplus&lt;br /&gt;
      Tplus+=1            #Increase Tplus by one if return is positive&lt;br /&gt;
      psum+=r[i]          #Add return to sum of positive values&lt;br /&gt;
   elif r[i]&amp;lt;0:&lt;br /&gt;
      rminus[Tminus]=r[i] #Store negative return in array rminus&lt;br /&gt;
      Tminus+=1           #Increase Tminus by one if return is negative&lt;br /&gt;
      nsum+=r[i]          #Add return to sum of negative values&lt;br /&gt;
   else:&lt;br /&gt;
      Tzero+=1            #Increase Tzero by one if return is zero&lt;br /&gt;
rplus=rplus[0:Tplus].copy()      #Remove zeros from rplus&lt;br /&gt;
rminus=rminus[1:Tminus].copy()   #Remove zeros from rminus&lt;br /&gt;
meanplus=psum/Tplus              # Compute mean of positive returns &lt;br /&gt;
meanminus=nsum/Tminus            # Compute mean of negative returns &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
We can create an alternative algorithm that only uses vector operations, using the following algorithm.&lt;br /&gt;
# Create an array &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; containing the positive values from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;&lt;br /&gt;
# Create an array &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; containing the negative values from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the length of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and assign to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the length of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; and assign to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Calculate &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the mean of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; using vectorised functions&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
rminus=r[r&amp;lt;0].copy()         # Create an array containing negative returns&lt;br /&gt;
Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This version is much shorter and cleaner, and therefore easier to create and maintain.&lt;br /&gt;
&lt;br /&gt;
== Running Python programs ==&lt;br /&gt;
For people who are familiar with MATLAB it may be surprising to discover there is no simple way of running a Python program from within Python. If you want to run Python code using the standard Python interpreter, your choices are either&lt;br /&gt;
# Launch it from outside Python, e.g. save to a file &amp;lt;code&amp;gt;myscript.py&amp;lt;/code&amp;gt; and at the command line enter &amp;lt;code&amp;gt;python myscript.py&amp;lt;/code&amp;gt;&lt;br /&gt;
# Convert the program to a function and use the [http://docs.python.org/3/tutorial/modules.html Python module functionality], e.g. save the function to a file &amp;lt;code&amp;gt;myfunctions.py&amp;lt;/code&amp;gt; and use Python&amp;#039;s &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;import&amp;lt;/source&amp;gt; to make the functions available.&lt;br /&gt;
&lt;br /&gt;
The first method can be demonstrated by creating a text file &amp;lt;code&amp;gt;ReturnAnalysis.py&amp;lt;/code&amp;gt; containing the following program.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
n=500000000&lt;br /&gt;
r=numpy.random.rand(n)*10-5&lt;br /&gt;
&lt;br /&gt;
import time&lt;br /&gt;
time1 = time.clock()&lt;br /&gt;
rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
rminus=r[r&amp;lt;0].copy()        # Create an array containing negative returns&lt;br /&gt;
Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
time2 = time.clock()&lt;br /&gt;
print(time2-time1)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In this example the array of values &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt; is generated using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rand&amp;lt;/source&amp;gt; function, in a real scenario these values might be loaded from a file. To run this from the &amp;#039;&amp;#039;&amp;#039;operating system command line&amp;#039;&amp;#039;&amp;#039; we can enter &amp;lt;code&amp;gt;python ReturnAnalysis.py&amp;lt;/code&amp;gt;. Note that this program outputs how long it takes to run, and on my desktop takes around 12.3s to complete (using the Anaconda Python distribution with the Accelerate package).&lt;br /&gt;
&lt;br /&gt;
Using the second method we can create a function, for example the following that undertakes the computation and returns the values of the two means. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;def returnanalysis(r):&lt;br /&gt;
   import numpy&lt;br /&gt;
   n=len(r)&lt;br /&gt;
   rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
   rminus=r[r&amp;lt;0].copy()        # Create an array containing negative returns&lt;br /&gt;
   Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
   Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
   Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
   meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
   meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
   return meanplus, meanminus&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
If this is saved to a file called &amp;lt;code&amp;gt;myfunctions.py&amp;lt;/code&amp;gt;, after importing we can use the function from the Python prompt as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; n=500000000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=numpy.random.rand(n)*10-5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import myfunctions&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; mplus, mminus = myfunctions.returnanalysis(r)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; mplus&lt;br /&gt;
1.7391992566830077&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; mminus&lt;br /&gt;
-2.7456183947241075&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The Python prompt has various other limitations which mean it isn&amp;#039;t ideal for interactive work. For example, it doesn&amp;#039;t include commands like &amp;lt;source enclose=none&amp;gt;pwd&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;cd&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;pwd&amp;lt;/source&amp;gt;, etc. An improved command line is included in [http://ipython.org/ IPython (Interactive Python)] which behaves far more like MATLAB. For example if we save the first Python code snippet from the [[Python/Program_Flow_and_Logicals#More_advanced_example|&amp;#039;&amp;#039;&amp;#039;More advanced Example&amp;#039;&amp;#039;&amp;#039; section (see above)]] to a file called &amp;lt;code&amp;gt;ReturnAnalysis1.py&amp;lt;/code&amp;gt;, we can execute this program from within IPython using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;run&amp;lt;/source&amp;gt;. For MATLAB-like behaviour, where a script can see the variables in the interactive namespace, we need to use &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;run&amp;lt;/source&amp;gt; with the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;-i&amp;lt;/source&amp;gt; flag. The &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;-t&amp;lt;/source&amp;gt; flag is useful too, as it times how long the script takes to run.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
In [1]: n=500000000&lt;br /&gt;
In [2]: from numpy.random import rand&lt;br /&gt;
In [3]: r=rand(n)*10-5&lt;br /&gt;
In [4]: run -i -t ReturnAnalysis1&lt;br /&gt;
&lt;br /&gt;
IPython CPU timings (estimated):&lt;br /&gt;
  User   :     978.66 s.&lt;br /&gt;
  System :       0.00 s.&lt;br /&gt;
Wall time:     978.71 s.&lt;br /&gt;
&lt;br /&gt;
In [5]: meanplus&lt;br /&gt;
Out[5]: 2.5001402997170192&lt;br /&gt;
&lt;br /&gt;
In [6]: meanminus&lt;br /&gt;
Out[6]: -2.5000714107736286&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The above example used the unvectorised version, and to demonstrate the importance of vectorisation in getting good performance we compare with the vectorised version (saved in &amp;lt;code&amp;gt;ReturnAnalysis2.py&amp;lt;/code&amp;gt;).&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
In [7]: run -i -t ReturnAnalysis2&lt;br /&gt;
&lt;br /&gt;
IPython CPU timings (estimated):&lt;br /&gt;
  User   :      12.18 s.&lt;br /&gt;
  System :       0.00 s.&lt;br /&gt;
Wall time:      12.18 s.&lt;br /&gt;
&lt;br /&gt;
In [8]: meanplus&lt;br /&gt;
Out[8]: 2.5001402997170192&lt;br /&gt;
&lt;br /&gt;
In [9]: meanminus&lt;br /&gt;
Out[9]: -2.5000714107736286&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Finally to compare with MATLAB, when running the vectorised MATLAB version (saved to a file called &amp;lt;code&amp;gt;ReturnAnalysis2.m&amp;lt;/code&amp;gt;, the run time is as follows.&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; n=500000000;&lt;br /&gt;
&amp;gt;&amp;gt; r=rand(n,1)*10-5;&lt;br /&gt;
&amp;gt;&amp;gt; tic,ReturnAnalysis2,toc&lt;br /&gt;
Elapsed time is 11.193218 seconds.&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3211</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3211"/>
				<updated>2013-10-16T13:00:52Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The following assumes use of Python version 3, as opposed to Python 2. No more major releases are planned for Python 2, and so version 3 is expected to be the future of Python. The two versions of Python, although similar, are not compatible in a forwards or backwards direction&amp;lt;ref&amp;gt;Although Python 2 and 3 are not totally compatible, Python 2.7 is close to Python 3. If you have to use Python 2, it is recommended using version 2.7, writing code as close to Python 3 as possible, and using tools like &amp;#039;&amp;#039;2to3&amp;#039;&amp;#039; to port to Python 3. Alternatively there is a Python compatibility packages called &amp;#039;&amp;#039;six&amp;#039;&amp;#039;.&amp;lt;/ref&amp;gt;, and some legacy code exists only as Python 2. Some differences between the two versions are discussed in the footnotes.&lt;br /&gt;
&lt;br /&gt;
= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list/range to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python 3. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. The advantage of a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; object over a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; is that the sequence of values are not stored in memory with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;. &amp;lt;ref&amp;gt;In Python 3 the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object. However the Python 2 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a list, i.e. stores every integer value required in memory which is very inefficient if simply looping through a long sequence of integers in a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for&amp;lt;/source&amp;gt; loop. Python 2 has &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;xrange&amp;lt;/source&amp;gt; that behaves like the Python 3 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;.&amp;lt;/ref&amp;gt;. For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists and ranges are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm. Remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;. Namespaces are useful because they allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a function called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt;, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespaces within the function call, e.g. to use &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; at the Python command prompt to generate 5 random numbers&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, rather than assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;does not copy&amp;#039;&amp;#039;&amp;#039; values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; to a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array of values. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; does copy all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
NumPy array (and Python list) slices work in subtly different ways to MATLAB&amp;#039;s too. For example, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A[m:n]&amp;lt;/source&amp;gt; returns all values from the element with the index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;m&amp;lt;/source&amp;gt; to the element with index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;n-1&amp;lt;/source&amp;gt;, and because the first element has index 0, we receive the (m+1)&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; to n&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; values, e.g. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[0:10]&lt;br /&gt;
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[4:6]&lt;br /&gt;
[5, 6]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Compare this to MATLAB&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
r =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(1:10)&lt;br /&gt;
ans =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(4:6)&lt;br /&gt;
ans =&lt;br /&gt;
     4     5     6&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on whole arrays are much faster than the equivalent code using loops, as they allow optimal use of the processor (such code optimisation is often called vectorisation). In addition code using vector and matrix operations is often shorter and easier to read that the equivalent using loops.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, this operation is much quicker than growing arrays on-the-fly. In this example we preallocate two arrays at the Python prompt with 10,000 elements each, the first array contains integers and the second contains double precision floating point numbers.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced example ===&lt;br /&gt;
We now look at the Python equivalent of the [[Program_Flow_and_Logicals#Relevant_example|Relevant example on the MATLAB page]], which assumes we have &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; returns in a vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to:&lt;br /&gt;
&lt;br /&gt;
# Count the number of positive, negative and zero returns&lt;br /&gt;
# Create an array holding only the positive values&lt;br /&gt;
# Create another array holding only the negative values&lt;br /&gt;
# Compute the means of the positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The naive algorithm using a loop in Python is as follows.&lt;br /&gt;
# Find the length of the NumPy array holding  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=numpy.size(r)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus=0; Tzero=0; Tminus=0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate two sum variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;psum=0.0; nsum=0.0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Preallocate NumPy arrays &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=numpy.zeros(T)&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=numpy.zeros(T)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe)&lt;br /&gt;
# Set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;i=0&amp;lt;/source&amp;gt; &lt;br /&gt;
# Check whether &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; is greater, smaller or equal to 0&lt;br /&gt;
#* If &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;gt;0&amp;lt;/source&amp;gt;, set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus[Tplus]=r[i]&amp;lt;/source&amp;gt;, add &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;psum&amp;lt;/source&amp;gt;, and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;0&amp;lt;/source&amp;gt; set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus[Tminus]=r[i]&amp;lt;/source&amp;gt;, add &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;nsum&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Repeat 6 for &amp;lt;math&amp;gt;i=1,\ldots,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove spare zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=rplus[0:Tplus].copy()&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=rminus[0:Tminus].copy()&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus (the number of positive, negative and zero returns are stored in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;)&lt;br /&gt;
&lt;br /&gt;
The Python code is as follows, however note that this code isn&amp;#039;t completely free of vector operations, since removal of zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; is vectorised.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
T=numpy.size(r)&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0&lt;br /&gt;
psum=0.0;nsum=0.0&lt;br /&gt;
rplus=numpy.zeros(T);rminus=numpy.zeros(T)&lt;br /&gt;
for i in range(T):&lt;br /&gt;
   if r[i]&amp;gt;0:&lt;br /&gt;
      rplus[Tplus]=r[i]   #Store positive return in array rplus&lt;br /&gt;
      Tplus+=1            #Increase Tplus by one if return is positive&lt;br /&gt;
      psum+=r[i]          #Add return to sum of positive values&lt;br /&gt;
   elif r[i]&amp;lt;0:&lt;br /&gt;
      rminus[Tminus]=r[i] #Store negative return in array rminus&lt;br /&gt;
      Tminus+=1           #Increase Tminus by one if return is negative&lt;br /&gt;
      nsum+=r[i]          #Add return to sum of negative values&lt;br /&gt;
   else:&lt;br /&gt;
      Tzero+=1            #Increase Tzero by one if return is zero&lt;br /&gt;
rplus=rplus[0:Tplus].copy()      #Remove zeros from rplus&lt;br /&gt;
rminus=rminus[1:Tminus].copy()   #Remove zeros from rminus&lt;br /&gt;
meanplus=psum/Tplus              # Compute mean of positive returns &lt;br /&gt;
meanminus=nsum/Tminus            # Compute mean of negative returns &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
We can create an alternative algorithm that only uses vector operations, using the following algorithm.&lt;br /&gt;
# Create an array &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; containing the positive values from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;&lt;br /&gt;
# Create an array &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; containing the negative values from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the length of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and assign to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the length of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; and assign to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Calculate &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the mean of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; using vectorised functions&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
rminus=r[r&amp;lt;0].copy()         # Create an array containing negative returns&lt;br /&gt;
Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This version is much shorter and cleaner, and therefore easier to create and maintain.&lt;br /&gt;
&lt;br /&gt;
== Running Python programs ==&lt;br /&gt;
For people who are familiar with MATLAB it may be surprising to discover there is no simple way of running a Python program from within Python. If you want to run Python code using the standard Python interpreter, your choices are either&lt;br /&gt;
# Launch it from outside Python, e.g. save to a file &amp;lt;code&amp;gt;myscript.py&amp;lt;/code&amp;gt; and at the command line enter &amp;lt;code&amp;gt;python myscript.py&amp;lt;/code&amp;gt;&lt;br /&gt;
# Convert the program to a function and use the [http://docs.python.org/3/tutorial/modules.html Python module functionality], e.g. save the function to a file &amp;lt;code&amp;gt;myfunctions.py&amp;lt;/code&amp;gt; and use Python&amp;#039;s &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;import&amp;lt;/source&amp;gt; to make the functions available.&lt;br /&gt;
&lt;br /&gt;
The first method can be demonstrated by creating a text file &amp;lt;code&amp;gt;ReturnAnalysis.py&amp;lt;/code&amp;gt; containing the following program.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
n=500000000&lt;br /&gt;
r=numpy.random.rand(n)*10-5&lt;br /&gt;
&lt;br /&gt;
import time&lt;br /&gt;
time1 = time.clock()&lt;br /&gt;
rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
rminus=r[r&amp;lt;0].copy()        # Create an array containing negative returns&lt;br /&gt;
Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
time2 = time.clock()&lt;br /&gt;
print(time2-time1)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In this example the array of values &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt; is generated using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rand&amp;lt;/source&amp;gt; function, in a real scenario these values might be loaded from a file. To run this from the &amp;#039;&amp;#039;&amp;#039;operating system command line&amp;#039;&amp;#039;&amp;#039; we can enter &amp;lt;code&amp;gt;python ReturnAnalysis.py&amp;lt;/code&amp;gt;. Note that this program outputs how long it takes to run, and on my desktop takes around 12.3s to complete (using the Anaconda Python distribution with the Accelerate package).&lt;br /&gt;
&lt;br /&gt;
Using the second method we can create a function, for example the following that undertakes the computation and returns the values of the two means. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;def returnanalysis(r):&lt;br /&gt;
   import numpy&lt;br /&gt;
   n=len(r)&lt;br /&gt;
   rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
   rminus=r[r&amp;lt;0].copy()        # Create an array containing negative returns&lt;br /&gt;
   Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
   Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
   Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
   meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
   meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
   return meanplus, meanminus&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
If this is saved to a file called &amp;lt;code&amp;gt;myfunctions.py&amp;lt;/code&amp;gt;, after importing we can use the function from the Python prompt as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; n=500000000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=numpy.random.rand(n)*10-5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import myfunctions&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; mplus, mminus = myfunctions.returnanalysis(r)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; mplus&lt;br /&gt;
1.7391992566830077&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; mminus&lt;br /&gt;
-2.7456183947241075&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The Python prompt has various other limitations which mean it isn&amp;#039;t ideal for interactive work. For example, it doesn&amp;#039;t include commands like &amp;lt;source enclose=none&amp;gt;pwd&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;cd&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;pwd&amp;lt;/source&amp;gt;, etc. An improved command line is included in [http://ipython.org/ IPython (Interactive Python)] which behaves far more like MATLAB. For example if we save the first Python code snippet from the [[Python/Program_Flow_and_Logicals#More_advanced_example|&amp;#039;&amp;#039;&amp;#039;More advanced Example&amp;#039;&amp;#039;&amp;#039; section (see above)]] to a file called &amp;lt;code&amp;gt;ReturnAnalysis1.py&amp;lt;/code&amp;gt;, we can execute this program from within IPython using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;run&amp;lt;/source&amp;gt;. For MATLAB-like behaviour, where a script can see the variables in the interactive namespace, we need to use &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;run&amp;lt;/source&amp;gt; with the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;-i&amp;lt;/source&amp;gt; flag. The &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;-t&amp;lt;/source&amp;gt; flag is useful too, as it times how long the script takes to run.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
In [1]: n=500000000&lt;br /&gt;
In [2]: from numpy.random import rand&lt;br /&gt;
In [3]: r=rand(n)*10-5&lt;br /&gt;
In [4]: run -i -t ReturnAnalysis1&lt;br /&gt;
&lt;br /&gt;
IPython CPU timings (estimated):&lt;br /&gt;
  User   :     978.66 s.&lt;br /&gt;
  System :       0.00 s.&lt;br /&gt;
Wall time:     978.71 s.&lt;br /&gt;
&lt;br /&gt;
In [5]: meanplus&lt;br /&gt;
Out[5]: 2.5001402997170192&lt;br /&gt;
&lt;br /&gt;
In [6]: meanminus&lt;br /&gt;
Out[6]: -2.5000714107736286&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The above example used the unvectorised version, and to demonstrate the importance of vectorisation in getting good performance we compare with the vectorised version (saved in &amp;lt;code&amp;gt;ReturnAnalysis2.py&amp;lt;/code&amp;gt;).&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
In [7]: run -i -t ReturnAnalysis2&lt;br /&gt;
&lt;br /&gt;
IPython CPU timings (estimated):&lt;br /&gt;
  User   :      12.18 s.&lt;br /&gt;
  System :       0.00 s.&lt;br /&gt;
Wall time:      12.18 s.&lt;br /&gt;
&lt;br /&gt;
In [8]: meanplus&lt;br /&gt;
Out[8]: 2.5001402997170192&lt;br /&gt;
&lt;br /&gt;
In [9]: meanminus&lt;br /&gt;
Out[9]: -2.5000714107736286&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Finally to compare with MATLAB, when running the vectorised MATLAB version (saved to a file called &amp;lt;code&amp;gt;ReturnAnalysis2.m&amp;lt;/code&amp;gt;, the run time is as follows.&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; n=500000000;&lt;br /&gt;
&amp;gt;&amp;gt; r=rand(n,1)*10-5;&lt;br /&gt;
&amp;gt;&amp;gt; tic,ReturnAnalysis2,toc&lt;br /&gt;
Elapsed time is 11.193218 seconds.&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3210</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3210"/>
				<updated>2013-10-16T13:00:07Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The following assumes use of Python version 3 as opposed to Python 2, since no more major releases are planned for version 2, version 3 is expected to be the future of Python. The two versions of Python, although similar, are not compatible in a forwards or backwards direction&amp;lt;ref&amp;gt;Although Python 2 and 3 are not totally compatible, Python 2.7 is close to Python 3. If you have to use Python 2, it is recommended using version 2.7, writing code as close to Python 3 as possible, and using tools like &amp;#039;&amp;#039;2to3&amp;#039;&amp;#039; to port to Python 3. Alternatively there is a Python compatibility packages called &amp;#039;&amp;#039;six&amp;#039;&amp;#039;.&amp;lt;/ref&amp;gt;, and some legacy code exists only as Python 2. Some differences between the two versions are discussed in the footnotes.&lt;br /&gt;
&lt;br /&gt;
= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list/range to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python 3. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. The advantage of a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; object over a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; is that the sequence of values are not stored in memory with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;. &amp;lt;ref&amp;gt;In Python 3 the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object. However the Python 2 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a list, i.e. stores every integer value required in memory which is very inefficient if simply looping through a long sequence of integers in a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for&amp;lt;/source&amp;gt; loop. Python 2 has &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;xrange&amp;lt;/source&amp;gt; that behaves like the Python 3 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;.&amp;lt;/ref&amp;gt;. For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists and ranges are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm. Remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;. Namespaces are useful because they allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a function called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt;, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespaces within the function call, e.g. to use &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; at the Python command prompt to generate 5 random numbers&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, rather than assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;does not copy&amp;#039;&amp;#039;&amp;#039; values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; to a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array of values. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; does copy all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
NumPy array (and Python list) slices work in subtly different ways to MATLAB&amp;#039;s too. For example, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A[m:n]&amp;lt;/source&amp;gt; returns all values from the element with the index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;m&amp;lt;/source&amp;gt; to the element with index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;n-1&amp;lt;/source&amp;gt;, and because the first element has index 0, we receive the (m+1)&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; to n&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; values, e.g. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[0:10]&lt;br /&gt;
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[4:6]&lt;br /&gt;
[5, 6]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Compare this to MATLAB&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
r =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(1:10)&lt;br /&gt;
ans =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(4:6)&lt;br /&gt;
ans =&lt;br /&gt;
     4     5     6&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on whole arrays are much faster than the equivalent code using loops, as they allow optimal use of the processor (such code optimisation is often called vectorisation). In addition code using vector and matrix operations is often shorter and easier to read that the equivalent using loops.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, this operation is much quicker than growing arrays on-the-fly. In this example we preallocate two arrays at the Python prompt with 10,000 elements each, the first array contains integers and the second contains double precision floating point numbers.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced example ===&lt;br /&gt;
We now look at the Python equivalent of the [[Program_Flow_and_Logicals#Relevant_example|Relevant example on the MATLAB page]], which assumes we have &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; returns in a vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to:&lt;br /&gt;
&lt;br /&gt;
# Count the number of positive, negative and zero returns&lt;br /&gt;
# Create an array holding only the positive values&lt;br /&gt;
# Create another array holding only the negative values&lt;br /&gt;
# Compute the means of the positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The naive algorithm using a loop in Python is as follows.&lt;br /&gt;
# Find the length of the NumPy array holding  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=numpy.size(r)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus=0; Tzero=0; Tminus=0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate two sum variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;psum=0.0; nsum=0.0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Preallocate NumPy arrays &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=numpy.zeros(T)&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=numpy.zeros(T)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe)&lt;br /&gt;
# Set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;i=0&amp;lt;/source&amp;gt; &lt;br /&gt;
# Check whether &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; is greater, smaller or equal to 0&lt;br /&gt;
#* If &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;gt;0&amp;lt;/source&amp;gt;, set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus[Tplus]=r[i]&amp;lt;/source&amp;gt;, add &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;psum&amp;lt;/source&amp;gt;, and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;0&amp;lt;/source&amp;gt; set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus[Tminus]=r[i]&amp;lt;/source&amp;gt;, add &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;nsum&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Repeat 6 for &amp;lt;math&amp;gt;i=1,\ldots,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove spare zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=rplus[0:Tplus].copy()&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=rminus[0:Tminus].copy()&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus (the number of positive, negative and zero returns are stored in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;)&lt;br /&gt;
&lt;br /&gt;
The Python code is as follows, however note that this code isn&amp;#039;t completely free of vector operations, since removal of zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; is vectorised.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
T=numpy.size(r)&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0&lt;br /&gt;
psum=0.0;nsum=0.0&lt;br /&gt;
rplus=numpy.zeros(T);rminus=numpy.zeros(T)&lt;br /&gt;
for i in range(T):&lt;br /&gt;
   if r[i]&amp;gt;0:&lt;br /&gt;
      rplus[Tplus]=r[i]   #Store positive return in array rplus&lt;br /&gt;
      Tplus+=1            #Increase Tplus by one if return is positive&lt;br /&gt;
      psum+=r[i]          #Add return to sum of positive values&lt;br /&gt;
   elif r[i]&amp;lt;0:&lt;br /&gt;
      rminus[Tminus]=r[i] #Store negative return in array rminus&lt;br /&gt;
      Tminus+=1           #Increase Tminus by one if return is negative&lt;br /&gt;
      nsum+=r[i]          #Add return to sum of negative values&lt;br /&gt;
   else:&lt;br /&gt;
      Tzero+=1            #Increase Tzero by one if return is zero&lt;br /&gt;
rplus=rplus[0:Tplus].copy()      #Remove zeros from rplus&lt;br /&gt;
rminus=rminus[1:Tminus].copy()   #Remove zeros from rminus&lt;br /&gt;
meanplus=psum/Tplus              # Compute mean of positive returns &lt;br /&gt;
meanminus=nsum/Tminus            # Compute mean of negative returns &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
We can create an alternative algorithm that only uses vector operations, using the following algorithm.&lt;br /&gt;
# Create an array &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; containing the positive values from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;&lt;br /&gt;
# Create an array &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; containing the negative values from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the length of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and assign to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the length of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; and assign to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Calculate &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the mean of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; using vectorised functions&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
rminus=r[r&amp;lt;0].copy()         # Create an array containing negative returns&lt;br /&gt;
Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This version is much shorter and cleaner, and therefore easier to create and maintain.&lt;br /&gt;
&lt;br /&gt;
== Running Python programs ==&lt;br /&gt;
For people who are familiar with MATLAB it may be surprising to discover there is no simple way of running a Python program from within Python. If you want to run Python code using the standard Python interpreter, your choices are either&lt;br /&gt;
# Launch it from outside Python, e.g. save to a file &amp;lt;code&amp;gt;myscript.py&amp;lt;/code&amp;gt; and at the command line enter &amp;lt;code&amp;gt;python myscript.py&amp;lt;/code&amp;gt;&lt;br /&gt;
# Convert the program to a function and use the [http://docs.python.org/3/tutorial/modules.html Python module functionality], e.g. save the function to a file &amp;lt;code&amp;gt;myfunctions.py&amp;lt;/code&amp;gt; and use Python&amp;#039;s &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;import&amp;lt;/source&amp;gt; to make the functions available.&lt;br /&gt;
&lt;br /&gt;
The first method can be demonstrated by creating a text file &amp;lt;code&amp;gt;ReturnAnalysis.py&amp;lt;/code&amp;gt; containing the following program.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
n=500000000&lt;br /&gt;
r=numpy.random.rand(n)*10-5&lt;br /&gt;
&lt;br /&gt;
import time&lt;br /&gt;
time1 = time.clock()&lt;br /&gt;
rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
rminus=r[r&amp;lt;0].copy()        # Create an array containing negative returns&lt;br /&gt;
Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
time2 = time.clock()&lt;br /&gt;
print(time2-time1)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In this example the array of values &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt; is generated using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rand&amp;lt;/source&amp;gt; function, in a real scenario these values might be loaded from a file. To run this from the &amp;#039;&amp;#039;&amp;#039;operating system command line&amp;#039;&amp;#039;&amp;#039; we can enter &amp;lt;code&amp;gt;python ReturnAnalysis.py&amp;lt;/code&amp;gt;. Note that this program outputs how long it takes to run, and on my desktop takes around 12.3s to complete (using the Anaconda Python distribution with the Accelerate package).&lt;br /&gt;
&lt;br /&gt;
Using the second method we can create a function, for example the following that undertakes the computation and returns the values of the two means. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;def returnanalysis(r):&lt;br /&gt;
   import numpy&lt;br /&gt;
   n=len(r)&lt;br /&gt;
   rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
   rminus=r[r&amp;lt;0].copy()        # Create an array containing negative returns&lt;br /&gt;
   Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
   Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
   Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
   meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
   meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
   return meanplus, meanminus&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
If this is saved to a file called &amp;lt;code&amp;gt;myfunctions.py&amp;lt;/code&amp;gt;, after importing we can use the function from the Python prompt as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; n=500000000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=numpy.random.rand(n)*10-5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import myfunctions&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; mplus, mminus = myfunctions.returnanalysis(r)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; mplus&lt;br /&gt;
1.7391992566830077&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; mminus&lt;br /&gt;
-2.7456183947241075&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The Python prompt has various other limitations which mean it isn&amp;#039;t ideal for interactive work. For example, it doesn&amp;#039;t include commands like &amp;lt;source enclose=none&amp;gt;pwd&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;cd&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;pwd&amp;lt;/source&amp;gt;, etc. An improved command line is included in [http://ipython.org/ IPython (Interactive Python)] which behaves far more like MATLAB. For example if we save the first Python code snippet from the [[Python/Program_Flow_and_Logicals#More_advanced_example|&amp;#039;&amp;#039;&amp;#039;More advanced Example&amp;#039;&amp;#039;&amp;#039; section (see above)]] to a file called &amp;lt;code&amp;gt;ReturnAnalysis1.py&amp;lt;/code&amp;gt;, we can execute this program from within IPython using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;run&amp;lt;/source&amp;gt;. For MATLAB-like behaviour, where a script can see the variables in the interactive namespace, we need to use &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;run&amp;lt;/source&amp;gt; with the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;-i&amp;lt;/source&amp;gt; flag. The &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;-t&amp;lt;/source&amp;gt; flag is useful too, as it times how long the script takes to run.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
In [1]: n=500000000&lt;br /&gt;
In [2]: from numpy.random import rand&lt;br /&gt;
In [3]: r=rand(n)*10-5&lt;br /&gt;
In [4]: run -i -t ReturnAnalysis1&lt;br /&gt;
&lt;br /&gt;
IPython CPU timings (estimated):&lt;br /&gt;
  User   :     978.66 s.&lt;br /&gt;
  System :       0.00 s.&lt;br /&gt;
Wall time:     978.71 s.&lt;br /&gt;
&lt;br /&gt;
In [5]: meanplus&lt;br /&gt;
Out[5]: 2.5001402997170192&lt;br /&gt;
&lt;br /&gt;
In [6]: meanminus&lt;br /&gt;
Out[6]: -2.5000714107736286&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The above example used the unvectorised version, and to demonstrate the importance of vectorisation in getting good performance we compare with the vectorised version (saved in &amp;lt;code&amp;gt;ReturnAnalysis2.py&amp;lt;/code&amp;gt;).&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
In [7]: run -i -t ReturnAnalysis2&lt;br /&gt;
&lt;br /&gt;
IPython CPU timings (estimated):&lt;br /&gt;
  User   :      12.18 s.&lt;br /&gt;
  System :       0.00 s.&lt;br /&gt;
Wall time:      12.18 s.&lt;br /&gt;
&lt;br /&gt;
In [8]: meanplus&lt;br /&gt;
Out[8]: 2.5001402997170192&lt;br /&gt;
&lt;br /&gt;
In [9]: meanminus&lt;br /&gt;
Out[9]: -2.5000714107736286&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Finally to compare with MATLAB, when running the vectorised MATLAB version (saved to a file called &amp;lt;code&amp;gt;ReturnAnalysis2.m&amp;lt;/code&amp;gt;, the run time is as follows.&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; n=500000000;&lt;br /&gt;
&amp;gt;&amp;gt; r=rand(n,1)*10-5;&lt;br /&gt;
&amp;gt;&amp;gt; tic,ReturnAnalysis2,toc&lt;br /&gt;
Elapsed time is 11.193218 seconds.&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3209</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3209"/>
				<updated>2013-10-16T12:46:21Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* Running Python programs */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The following assumes use of Python 3 (version 3 of Python) as opposed to Python 2, since no more major releases are planned for version 2, version 3 is expected to be the future of Python. The two versions of Python, although similar, are not compatible in a forwards or backwards direction&amp;lt;ref&amp;gt;Although Python 2 and 3 are not totally compatible, Python 2.7 is close to Python 3. If you have to use Python 2, it is recommended using version 2.7, writing code as close to Python 3 as possible, and using tools like &amp;#039;&amp;#039;2to3&amp;#039;&amp;#039; to port to Python 3. Alternatively there is a Python compatibility packages called &amp;#039;&amp;#039;six&amp;#039;&amp;#039;.&amp;lt;/ref&amp;gt;, and some legacy code exists only as Python 2. Some differences between the two versions are discussed in the footnotes.&lt;br /&gt;
&lt;br /&gt;
= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list/range to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python 3. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. The advantage of a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; object over a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; is that the sequence of values are not stored in memory with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;. &amp;lt;ref&amp;gt;In Python 3 the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object. However the Python 2 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a list, i.e. stores every integer value required in memory which is very inefficient if simply looping through a long sequence of integers in a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for&amp;lt;/source&amp;gt; loop. Python 2 has &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;xrange&amp;lt;/source&amp;gt; that behaves like the Python 3 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;.&amp;lt;/ref&amp;gt;. For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists and ranges are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm. Remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;. Namespaces are useful because they allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a function called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt;, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespaces within the function call, e.g. to use &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; at the Python command prompt to generate 5 random numbers&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, rather than assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;does not copy&amp;#039;&amp;#039;&amp;#039; values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; to a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array of values. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; does copy all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
NumPy array (and Python list) slices work in subtly different ways to MATLAB&amp;#039;s too. For example, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A[m:n]&amp;lt;/source&amp;gt; returns all values from the element with the index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;m&amp;lt;/source&amp;gt; to the element with index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;n-1&amp;lt;/source&amp;gt;, and because the first element has index 0, we receive the (m+1)&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; to n&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; values, e.g. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[0:10]&lt;br /&gt;
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[4:6]&lt;br /&gt;
[5, 6]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Compare this to MATLAB&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
r =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(1:10)&lt;br /&gt;
ans =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(4:6)&lt;br /&gt;
ans =&lt;br /&gt;
     4     5     6&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on whole arrays are much faster than the equivalent code using loops, as they allow optimal use of the processor (such code optimisation is often called vectorisation). In addition code using vector and matrix operations is often shorter and easier to read that the equivalent using loops.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, this operation is much quicker than growing arrays on-the-fly. In this example we preallocate two arrays at the Python prompt with 10,000 elements each, the first array contains integers and the second contains double precision floating point numbers.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced example ===&lt;br /&gt;
We now look at the Python equivalent of the [[Program_Flow_and_Logicals#Relevant_example|Relevant example on the MATLAB page]], which assumes we have &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; returns in a vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to:&lt;br /&gt;
&lt;br /&gt;
# Count the number of positive, negative and zero returns&lt;br /&gt;
# Create an array holding only the positive values&lt;br /&gt;
# Create another array holding only the negative values&lt;br /&gt;
# Compute the means of the positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The naive algorithm using a loop in Python is as follows.&lt;br /&gt;
# Find the length of the NumPy array holding  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=numpy.size(r)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus=0; Tzero=0; Tminus=0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate two sum variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;psum=0.0; nsum=0.0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Preallocate NumPy arrays &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=numpy.zeros(T)&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=numpy.zeros(T)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe)&lt;br /&gt;
# Set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;i=0&amp;lt;/source&amp;gt; &lt;br /&gt;
# Check whether &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; is greater, smaller or equal to 0&lt;br /&gt;
#* If &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;gt;0&amp;lt;/source&amp;gt;, set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus[Tplus]=r[i]&amp;lt;/source&amp;gt;, add &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;psum&amp;lt;/source&amp;gt;, and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;0&amp;lt;/source&amp;gt; set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus[Tminus]=r[i]&amp;lt;/source&amp;gt;, add &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;nsum&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Repeat 6 for &amp;lt;math&amp;gt;i=1,\ldots,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove spare zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=rplus[0:Tplus].copy()&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=rminus[0:Tminus].copy()&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus (the number of positive, negative and zero returns are stored in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;)&lt;br /&gt;
&lt;br /&gt;
The Python code is as follows, however note that this code isn&amp;#039;t completely free of vector operations, since removal of zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; is vectorised.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
T=numpy.size(r)&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0&lt;br /&gt;
psum=0.0;nsum=0.0&lt;br /&gt;
rplus=numpy.zeros(T);rminus=numpy.zeros(T)&lt;br /&gt;
for i in range(T):&lt;br /&gt;
   if r[i]&amp;gt;0:&lt;br /&gt;
      rplus[Tplus]=r[i]   #Store positive return in array rplus&lt;br /&gt;
      Tplus+=1            #Increase Tplus by one if return is positive&lt;br /&gt;
      psum+=r[i]          #Add return to sum of positive values&lt;br /&gt;
   elif r[i]&amp;lt;0:&lt;br /&gt;
      rminus[Tminus]=r[i] #Store negative return in array rminus&lt;br /&gt;
      Tminus+=1           #Increase Tminus by one if return is negative&lt;br /&gt;
      nsum+=r[i]          #Add return to sum of negative values&lt;br /&gt;
   else:&lt;br /&gt;
      Tzero+=1            #Increase Tzero by one if return is zero&lt;br /&gt;
rplus=rplus[0:Tplus].copy()      #Remove zeros from rplus&lt;br /&gt;
rminus=rminus[1:Tminus].copy()   #Remove zeros from rminus&lt;br /&gt;
meanplus=psum/Tplus              # Compute mean of positive returns &lt;br /&gt;
meanminus=nsum/Tminus            # Compute mean of negative returns &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
We can create an alternative algorithm that only uses vector operations, using the following algorithm.&lt;br /&gt;
# Create an array &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; containing the positive values from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;&lt;br /&gt;
# Create an array &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; containing the negative values from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the length of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and assign to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the length of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; and assign to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Calculate &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the mean of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; using vectorised functions&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
rminus=r[r&amp;lt;0].copy()         # Create an array containing negative returns&lt;br /&gt;
Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This version is much shorter and cleaner, and therefore easier to create and maintain.&lt;br /&gt;
&lt;br /&gt;
== Running Python programs ==&lt;br /&gt;
For people who are familiar with MATLAB it may be surprising to discover there is no simple way of running a Python program from within Python. If you want to run Python code using the standard Python interpreter, your choices are either&lt;br /&gt;
# Launch it from outside Python, e.g. save to a file &amp;lt;code&amp;gt;myscript.py&amp;lt;/code&amp;gt; and at the command line enter &amp;lt;code&amp;gt;python myscript.py&amp;lt;/code&amp;gt;&lt;br /&gt;
# Convert the program to a function and use the [http://docs.python.org/3/tutorial/modules.html Python module functionality], e.g. save the function to a file &amp;lt;code&amp;gt;myfunctions.py&amp;lt;/code&amp;gt; and use Python&amp;#039;s &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;import&amp;lt;/source&amp;gt; to make the functions available.&lt;br /&gt;
&lt;br /&gt;
The first method can be demonstrated by creating a text file &amp;lt;code&amp;gt;ReturnAnalysis.py&amp;lt;/code&amp;gt; containing the following program.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
n=500000000&lt;br /&gt;
r=numpy.random.rand(n)*10-5&lt;br /&gt;
&lt;br /&gt;
import time&lt;br /&gt;
time1 = time.clock()&lt;br /&gt;
rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
rminus=r[r&amp;lt;0].copy()        # Create an array containing negative returns&lt;br /&gt;
Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
time2 = time.clock()&lt;br /&gt;
print(time2-time1)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In this example the array of values &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt; is generated using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rand&amp;lt;/source&amp;gt; function, in a real scenario these values might be loaded from a file. To run this from the &amp;#039;&amp;#039;&amp;#039;operating system command line&amp;#039;&amp;#039;&amp;#039; we can enter &amp;lt;code&amp;gt;python ReturnAnalysis.py&amp;lt;/code&amp;gt;. Note that this program outputs how long it takes to run, and on my desktop takes around 12.3s to complete (using the Anaconda Python distribution with the Accelerate package).&lt;br /&gt;
&lt;br /&gt;
Using the second method we can create a function, for example the following that undertakes the computation and returns the values of the two means. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;def returnanalysis(r):&lt;br /&gt;
   import numpy&lt;br /&gt;
   n=len(r)&lt;br /&gt;
   rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
   rminus=r[r&amp;lt;0].copy()        # Create an array containing negative returns&lt;br /&gt;
   Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
   Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
   Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
   meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
   meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
   return meanplus, meanminus&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
If this is saved to a file called &amp;lt;code&amp;gt;myfunctions.py&amp;lt;/code&amp;gt;, after importing we can use the function from the Python prompt as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; n=500000000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=numpy.random.rand(n)*10-5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import myfunctions&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; mplus, mminus = myfunctions.returnanalysis(r)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; mplus&lt;br /&gt;
1.7391992566830077&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; mminus&lt;br /&gt;
-2.7456183947241075&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The Python prompt has various other limitations which mean it isn&amp;#039;t ideal for interactive work. For example, it doesn&amp;#039;t include commands like &amp;lt;source enclose=none&amp;gt;pwd&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;cd&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;pwd&amp;lt;/source&amp;gt;, etc. An improved command line is included in [http://ipython.org/ IPython (Interactive Python)] which behaves far more like MATLAB. For example if we save the first Python code snippet from the [[Python/Program_Flow_and_Logicals#More_advanced_example|&amp;#039;&amp;#039;&amp;#039;More advanced Example&amp;#039;&amp;#039;&amp;#039; section (see above)]] to a file called &amp;lt;code&amp;gt;ReturnAnalysis1.py&amp;lt;/code&amp;gt;, we can execute this program from within IPython using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;run&amp;lt;/source&amp;gt;. For MATLAB-like behaviour, where a script can see the variables in the interactive namespace, we need to use &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;run&amp;lt;/source&amp;gt; with the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;-i&amp;lt;/source&amp;gt; flag. The &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;-t&amp;lt;/source&amp;gt; flag is useful too, as it times how long the script takes to run.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
In [1]: n=500000000&lt;br /&gt;
In [2]: from numpy.random import rand&lt;br /&gt;
In [3]: r=rand(n)*10-5&lt;br /&gt;
In [4]: run -i -t ReturnAnalysis1&lt;br /&gt;
&lt;br /&gt;
IPython CPU timings (estimated):&lt;br /&gt;
  User   :     978.66 s.&lt;br /&gt;
  System :       0.00 s.&lt;br /&gt;
Wall time:     978.71 s.&lt;br /&gt;
&lt;br /&gt;
In [5]: meanplus&lt;br /&gt;
Out[5]: 2.5001402997170192&lt;br /&gt;
&lt;br /&gt;
In [6]: meanminus&lt;br /&gt;
Out[6]: -2.5000714107736286&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The above example used the unvectorised version, and to demonstrate the importance of vectorisation in getting good performance we compare with the vectorised version (saved in &amp;lt;code&amp;gt;ReturnAnalysis2.py&amp;lt;/code&amp;gt;).&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
In [7]: run -i -t ReturnAnalysis2&lt;br /&gt;
&lt;br /&gt;
IPython CPU timings (estimated):&lt;br /&gt;
  User   :      12.18 s.&lt;br /&gt;
  System :       0.00 s.&lt;br /&gt;
Wall time:      12.18 s.&lt;br /&gt;
&lt;br /&gt;
In [8]: meanplus&lt;br /&gt;
Out[8]: 2.5001402997170192&lt;br /&gt;
&lt;br /&gt;
In [9]: meanminus&lt;br /&gt;
Out[9]: -2.5000714107736286&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Finally to compare with MATLAB, when running the vectorised MATLAB version (saved to a file called &amp;lt;code&amp;gt;ReturnAnalysis2.m&amp;lt;/code&amp;gt;, the run time is as follows.&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; n=500000000;&lt;br /&gt;
&amp;gt;&amp;gt; r=rand(n,1)*10-5;&lt;br /&gt;
&amp;gt;&amp;gt; tic,ReturnAnalysis2,toc&lt;br /&gt;
Elapsed time is 11.193218 seconds.&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3208</id>
		<title>Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Program_Flow_and_Logicals&amp;diff=3208"/>
				<updated>2013-10-15T15:54:03Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* Relevant example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
Very often in your life you have to repeat the same operation many times (move your right and left leg sequentially while walking/running) or behave differently depending on external conditions (there is or there isn’t a bus on the bus stop). Quite often these two are combined together. Say, if there is a bus on the bus stop, then you run trying to catch it, otherwise walk or stop and enjoy the usual Manchester weather. The same is true for programming. Quite often you want to repeat the same operation many times, or you want to change the way you process your data depending on some conditions. We start with conditional statements. They execute different pieces of the code depending on whether &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true or false. There are several ways you can formulate it. The shortest&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;if condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
, executes &amp;lt;source enclose=none&amp;gt;statement1;statement2,...&amp;lt;/source&amp;gt; only if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is satisfied. &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be anything that generate non-zero or 0 (True or False), say &amp;lt;source enclose=none&amp;gt;i&amp;gt;0&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;size(y,1)\sim=40&amp;lt;/source&amp;gt;, or &amp;lt;source enclose=none&amp;gt;5-i&amp;lt;/source&amp;gt;. The last condition is True always but for &amp;lt;source enclose=none&amp;gt;i=5&amp;lt;/source&amp;gt;. A slightly longer version&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  end&lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
runs &amp;lt;source enclose=none&amp;gt;statement1;statement2;...&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true and &amp;lt;source enclose=none&amp;gt;statement1a;statement2a;...&amp;lt;/source&amp;gt; otherwise. The most general specification is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  if condition1&lt;br /&gt;
  statement1;&lt;br /&gt;
  statement2;&lt;br /&gt;
  ...&lt;br /&gt;
  elseif condition2&lt;br /&gt;
  statement1a;&lt;br /&gt;
  statement2a;&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  ...&lt;br /&gt;
  elseif conditionN&lt;br /&gt;
  statement1b;&lt;br /&gt;
  statement2b;&lt;br /&gt;
  ...&lt;br /&gt;
  else&lt;br /&gt;
  statement1c;&lt;br /&gt;
  statement2c;&lt;br /&gt;
  ...&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
In this case, however, you have to ensure that &amp;lt;source enclose=none&amp;gt;condition1, condition2, …, conditionN&amp;lt;/source&amp;gt; are mutually disjoint. As an example, you might think about different actions depending on your final grade. Condition1: &amp;lt;source enclose=none&amp;gt;grade&amp;lt;30&amp;lt;/source&amp;gt;; Condition 2: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=30)&amp;amp;&amp;amp;(grade&amp;lt;40)&amp;lt;/source&amp;gt;; Condition 3: &amp;lt;source enclose=none&amp;gt;(grade&amp;gt;=40)&amp;amp;&amp;amp;(grade&amp;lt;50)&amp;lt;/source&amp;gt;; etc.&lt;br /&gt;
&lt;br /&gt;
MATLAB has two statements that create a loop. First, it is unconditional loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;for CounterVariable=[range of values]&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
It repeats at most as many times as many elements it has in the &amp;lt;source enclose=none&amp;gt;[range of values]&amp;lt;/source&amp;gt;. If the range of values is empty, this loop does not run. Say, if you define a range &amp;lt;source enclose=none&amp;gt;10:1&amp;lt;/source&amp;gt;, MATLAB creates an empty range. Thus, this loop will not be executed. If you define a range &amp;lt;source enclose=none&amp;gt;1:3:10&amp;lt;/source&amp;gt;, MATLAB creates a range of four values &amp;lt;source enclose=none&amp;gt;[1 4 7 10]&amp;lt;/source&amp;gt;, and the loop runs four times. During the first iteration, &amp;lt;source enclose=none&amp;gt;CounterVariable=1&amp;lt;/source&amp;gt;, during the second &amp;lt;source enclose=none&amp;gt;CounterVariable=4&amp;lt;/source&amp;gt;, etc. After the end of the loop &amp;lt;source enclose=none&amp;gt;CounterVariable=10&amp;lt;/source&amp;gt;. Please note, it is very unwise to modify the counter inside the loop. All modifications will disappear after the next iteration. Please also note, that the values in the range could be anything, including filenames or matrices from a cell vector. There are two commands that can modify the execution of the loop. &amp;lt;source enclose=none&amp;gt;continue&amp;lt;/source&amp;gt; breaks the current &amp;#039;&amp;#039;iteration&amp;#039;&amp;#039; of the loop. Once it is executed, the loop continues skipping current iteration. &amp;lt;source enclose=none&amp;gt;break&amp;lt;/source&amp;gt; stops the execution of the loop and your program continues after this point. These commands are used inside &amp;lt;source enclose=none&amp;gt;if&amp;lt;/source&amp;gt; statements. For example, &amp;lt;source enclose=none&amp;gt;if CounterVariable == 10 continue;end&amp;lt;/source&amp;gt; skips the loop iteration for &amp;lt;source enclose=none&amp;gt;CounterVariable = 10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Second, it is the conditional loop&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;while condition&lt;br /&gt;
statement1;&lt;br /&gt;
statement2;&lt;br /&gt;
...&lt;br /&gt;
end&amp;lt;/source&amp;gt;&lt;br /&gt;
This version of the loop executes statements as long as &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is true. If &amp;lt;source enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is always true, your loop runs forever.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
A standard application for &amp;lt;source enclose=none&amp;gt;for ... end&amp;lt;/source&amp;gt; loop is the reconstruction of AR(p) series once AR(p) coefficients and the vector of error terms is known. &amp;lt;math&amp;gt;y_t=\phi_0+\sum_{i=1}^p \phi_i y_{t-i}+e_t.&amp;lt;/math&amp;gt; For simplicity, we assume that &amp;lt;math&amp;gt;p=1&amp;lt;/math&amp;gt;. Also, to be able to compute &amp;lt;math&amp;gt;y_1&amp;lt;/math&amp;gt;, we need to provide &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;. Since we don’t know &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt;,the best guess for &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt;. For stationary AR(1) process, that is for the case &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;E(y_0)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;. Thus, knowing &amp;lt;math&amp;gt;y_0&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;e_t&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;t=1,\ldots,T&amp;lt;/math&amp;gt;, we can reconstruct &amp;lt;math&amp;gt;y_t,\ t=1\ldots,T&amp;lt;/math&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\begin{aligned}&lt;br /&gt;
y_1=&amp;amp;\phi_0+\phi_1 y_0+e_1\\&lt;br /&gt;
y_2=&amp;amp;\phi_0+\phi_1 y_1+e_2\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_t=&amp;amp;\phi_0+\phi_1 y_{t-1}+e_t\\&lt;br /&gt;
&amp;amp;\ldots\\&lt;br /&gt;
y_T=&amp;amp;\phi_0+\phi_1 y_{T-1}+e_T\end{aligned}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Definitely, if you are patient enough and &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is not very large, you can create your m file with &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; lines in it. However, once &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is unknown, this approach would not work. Fortunately, there is a better alternative for this type of operations. All these computations can be summarized using the following algorithm, assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is already created:&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*(phi0/(1-phi1))+e(1)&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&amp;lt;source&amp;gt;  &lt;br /&gt;
  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
However, if &amp;lt;source enclose=none&amp;gt;phi1=1&amp;lt;/source&amp;gt;, &amp;lt;math&amp;gt;E(y_t)&amp;lt;/math&amp;gt; is not constant, then the formula we use in the code does not work and will create either a series &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of &amp;lt;math&amp;gt;\pm\infty&amp;lt;/math&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0 \ne 0&amp;lt;/source&amp;gt; or a series of not a numbers &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;, if &amp;lt;source enclose=none&amp;gt;phi0=0&amp;lt;/source&amp;gt;&amp;lt;ref&amp;gt;There are two special numerical values in MATLAB. One is infinity &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt;, and another is not a number &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;. A value of a variable becomes &amp;lt;source enclose=none&amp;gt;Inf&amp;lt;/source&amp;gt; if the number is too big in absolute value (&amp;lt;math&amp;gt;\approx \pm 2e308&amp;lt;/math&amp;gt;). Also, infinity is generated once you have expressions like &amp;lt;math&amp;gt;x/0&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;x\ne0&amp;lt;/math&amp;gt;. After that, infinity can only change a sign or become not a number. Not a number appears when there is an uncertainty of a kind of &amp;lt;math&amp;gt;0/0&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;\infty-\infty&amp;lt;/math&amp;gt; and such. Any algebraic operations with &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt; result &amp;lt;source enclose=none&amp;gt;NaN&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;if else end&amp;lt;/source&amp;gt; or &amp;lt;source enclose=none&amp;gt;if end&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
To avoid these inconveniences, we have to consider separately two cases:&lt;br /&gt;
&lt;br /&gt;
# AR(1) process is stationary, i.e. &amp;lt;math&amp;gt;|\phi_1|&amp;lt;1&amp;lt;/math&amp;gt;&lt;br /&gt;
# AR(1) process is nonstationary, i.e.  &amp;lt;math&amp;gt;|\phi_1|\ge 1&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For the latter, we have to acknowledge the fact that &amp;lt;math&amp;gt;E(y_t)=\mu_t&amp;lt;/math&amp;gt;, i.e. unconditional expectation is a function of time. In this case we have to set &amp;lt;math&amp;gt;E(y_0)&amp;lt;/math&amp;gt; to some value. A standard assumption for non-stationary series is to assume that &amp;lt;math&amp;gt;E(y_0)=0&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The algorithm in this case would look like:&lt;br /&gt;
&lt;br /&gt;
# Find a length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(1)=phi0+phi1*y0+e(1)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=2&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=3,...,T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
If you don’t like the word else, you can skip it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source enclose=none&amp;gt;while end&amp;lt;/source&amp;gt; loop ==&lt;br /&gt;
&lt;br /&gt;
An alternative way of running the same code is to use a conditional loop (purely for demonstration purposes). Usually the conditional loop is used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find the length of a vector of error terms &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;T=size(e,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a vector &amp;lt;source enclose=none&amp;gt;y&amp;lt;/source&amp;gt; of the same length as vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;y=zeros(T,1)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Set $i=1$. &lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y0+e(i)&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Increase $i$ by one, i.e. $i=i+1$ (please note, in programming this is a valid statement)&lt;br /&gt;
# Check whether &amp;lt;math&amp;gt;i&amp;lt;=T&amp;lt;/math&amp;gt;. If yes, proceed. Else go to line 11.&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;y(i)=phi0+phi1*y(i-1)+e(i)&amp;lt;/source&amp;gt; &lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt; &lt;br /&gt;
# Repeat lines 7 $-$ 9&lt;br /&gt;
# ... (continuing the script)&lt;br /&gt;
Assuming vector &amp;lt;source enclose=none&amp;gt;e&amp;lt;/source&amp;gt; is known in advance  (for the sake of certainty, let &amp;lt;source enclose=none&amp;gt;e=randn(1000,1)&amp;lt;/source&amp;gt;), the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
    y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
    y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Imperfect substitutes of the above ==&lt;br /&gt;
&lt;br /&gt;
MATLAB has two powerful tools that make programmer’s life much easier and utilization of loops/if less frequent. In addition, quite often it makes the code run faster. In particular,&lt;br /&gt;
&lt;br /&gt;
# Logical expressions work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays.&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays.&lt;br /&gt;
&lt;br /&gt;
=== Irrelevant but useful example ===&lt;br /&gt;
&lt;br /&gt;
typing &amp;lt;source enclose=none&amp;gt;a=1:5&amp;lt;/source&amp;gt; in MATLAB command window creates a &amp;lt;math&amp;gt;1\times5&amp;lt;/math&amp;gt; row-vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[1\ 2\ 3\ 4\ 5]&amp;lt;/math&amp;gt;. Logical expression &amp;lt;source enclose=none&amp;gt;ind=(a&amp;gt;3.5)&amp;lt;/source&amp;gt; will create a so called logical vector &amp;lt;source enclose=none&amp;gt;ind&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[0\ 0\ 0\ 1\ 1]&amp;lt;/math&amp;gt;, i.e. it is 1 if the according element is greater than 3.5 and 0 otherwise. Now, typing &amp;lt;source enclose=none&amp;gt;b=a(ind)&amp;lt;/source&amp;gt; will generate a &amp;lt;math&amp;gt;2\times1&amp;lt;/math&amp;gt; subvector &amp;lt;source enclose=none&amp;gt;b&amp;lt;/source&amp;gt; with values &amp;lt;math&amp;gt;[4\ 5]&amp;lt;/math&amp;gt;. You can also create some vectors or matrices with specific values changed: the command &amp;lt;source enclose=none&amp;gt;a(ind)=a(ind)*2&amp;lt;/source&amp;gt; replace the last two values of the original vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt;. As a result, the vector &amp;lt;source enclose=none&amp;gt;a&amp;lt;/source&amp;gt; becomes &amp;lt;math&amp;gt;[1 \ 2\ 3\ 8\ 10]&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Slightly less irrelevant example ===&lt;br /&gt;
&lt;br /&gt;
In some occasions you would like to modify the matrix of interest. Say, in some surveys “no answer” is coded as 999. Once you import the whole dataset in &amp;lt;source enclose=none&amp;gt;X&amp;lt;/source&amp;gt;, you might want to replace these with, say, NaN. It can be done for the whole matrix of interest: &amp;lt;source enclose=none&amp;gt;X(X==999)=NaN&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Relevant example ===&lt;br /&gt;
&lt;br /&gt;
To demonstrate these capabilities in a more relevant environment, let’s run a very simple example. Assume that we have &amp;lt;math&amp;gt;T\times1&amp;lt;/math&amp;gt; vector of returns &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The algorithm for this is quite straightforward:&lt;br /&gt;
&lt;br /&gt;
# Find out the length of vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, T&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source enclose=none&amp;gt;Tplus=0, Tzero=0, Tminus=0&amp;lt;/source&amp;gt;, and vectors &amp;lt;source enclose=none&amp;gt;rplus=zeros(T,1), rminus=zeros(T,1)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe&lt;br /&gt;
# Check whether r(i) is greater, smaller or equal to 0 for i=1&lt;br /&gt;
# If &amp;lt;source enclose=none&amp;gt;r(i)&amp;gt;0&amp;lt;/source&amp;gt;, add 1 to Tplus, set &amp;lt;source enclose=none&amp;gt;rplus(Tplus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else if &amp;lt;source enclose=none&amp;gt;r(i)&amp;lt;0&amp;lt;/source&amp;gt; add 1 to Tminus, set &amp;lt;source enclose=none&amp;gt;rminus(Tminus)=r(i)&amp;lt;/source&amp;gt;;&lt;br /&gt;
# Else add 1 to Tzero&lt;br /&gt;
# Repeat 3-6 for &amp;lt;math&amp;gt;i=2,\ldots,T&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none&amp;gt;rplus=rplus(1:Tplus);&amp;lt;/source&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;source enclose=none&amp;gt;rminus=rminus(1:Tminus);&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
MATLAB translation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;T=size(r,1);&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0;&lt;br /&gt;
rplus=zeros(T,1);rminus=zeros(T,1);&lt;br /&gt;
for i=1:T&lt;br /&gt;
    if r(i)&amp;gt;0&lt;br /&gt;
        Tplus=Tplus+1;%increasing Tplus by one if return is positive&lt;br /&gt;
        rplus(Tplus)=r(i);%storing positive return in the proper subvector&lt;br /&gt;
    elseif r(i)&amp;lt;0&lt;br /&gt;
        Tminus=Tminus+1;%increasing Tminus by one if return is negative&lt;br /&gt;
        rminus(Tminus)=r(i);%storing negative return in the proper subvector&lt;br /&gt;
    else&lt;br /&gt;
        Tzero=Tzero+1;%increasing Tzero by one if return is neither positive nor negative&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
rplus=rplus(1:Tplus);%removing excessive zeros from a subvector of positive returns&lt;br /&gt;
rminus=rminus(1:Tminus);%removing excessive zeros from a subvector of negative returns&lt;br /&gt;
meanplus=mean(rplus);%computing mean of positive returns&lt;br /&gt;
meanminus=sum(rminus)/Tminus;%computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Using MATLAB capabilities mentioned in this section, the algorithm can be reduced to:&lt;br /&gt;
&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt; that has 1 for positive returns and 0 for negative returns&lt;br /&gt;
# Construct a vector &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt; that has 1 for negative returns and 0 for positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indplus&amp;lt;/source&amp;gt;. This is the number of positive returns&lt;br /&gt;
# Assign to &amp;lt;source enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt; a sum of elements of &amp;lt;source enclose=none&amp;gt;indminus&amp;lt;/source&amp;gt;. This is the number of negative returns&lt;br /&gt;
# Compute &amp;lt;source enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt; which is &amp;lt;source enclose=none&amp;gt;T-Tplus-Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Construct a vector of positive returns &amp;lt;source enclose=none&amp;gt;rplus=r(indplus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
# Construct a vector of negative returns &amp;lt;source enclose=none&amp;gt;rminus=r(indminus)&amp;lt;/source&amp;gt; and compute its mean&lt;br /&gt;
&lt;br /&gt;
MATLAB implementation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  indplus  = r&amp;gt;0;%constructing an indicator vector with 1s if r(i)&amp;gt;0, 0 otherwise&lt;br /&gt;
  indminus = r&amp;lt;0;%constructing an indicator vector with 1s if r(i)&amp;lt;0, 0 otherwise&lt;br /&gt;
  Tplus=sum(indplus);%computing a number of positive returns&lt;br /&gt;
  Tminus=sum(indminus);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  rplus=r(indplus);%constructing a vector of positive returns&lt;br /&gt;
  rminus=r(indminus);%constructing a vector of negative returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
Or, a slightly shorter version of the same thing&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(r,1);&lt;br /&gt;
  rplus  = r(r&amp;gt;0);%constructing a vector of positive returns&lt;br /&gt;
  rminus = r(r&amp;lt;0);%%constructing a vector of negative returns&lt;br /&gt;
  Tplus=size(rplus,1);%computing a number of positive returns&lt;br /&gt;
  Tminus=size(rminus,1);%computing a number of negative returns&lt;br /&gt;
  Tzero=T-Tplus-Tminus;%computing a number of zero returns&lt;br /&gt;
  meanplus=sum(rplus)/Tplus; %computing mean of positive returns&lt;br /&gt;
  meanminus=mean(rminus); %computing mean of negative returns&amp;lt;/source&amp;gt;&lt;br /&gt;
A shorter code is less exposed to errors and easier to read (at least after some practice).&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3207</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3207"/>
				<updated>2013-10-15T13:17:53Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* More advanced example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The following assumes use of Python 3 (version 3 of Python) as opposed to Python 2, since no more major releases are planned for version 2, version 3 is expected to be the future of Python. The two versions of Python, although similar, are not compatible in a forwards or backwards direction&amp;lt;ref&amp;gt;Although Python 2 and 3 are not totally compatible, Python 2.7 is close to Python 3. If you have to use Python 2, it is recommended using version 2.7, writing code as close to Python 3 as possible, and using tools like &amp;#039;&amp;#039;2to3&amp;#039;&amp;#039; to port to Python 3. Alternatively there is a Python compatibility packages called &amp;#039;&amp;#039;six&amp;#039;&amp;#039;.&amp;lt;/ref&amp;gt;, and some legacy code exists only as Python 2. Some differences between the two versions are discussed in the footnotes.&lt;br /&gt;
&lt;br /&gt;
= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list/range to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python 3. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. The advantage of a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; object over a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; is that the sequence of values are not stored in memory with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;. &amp;lt;ref&amp;gt;In Python 3 the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object. However the Python 2 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a list, i.e. stores every integer value required in memory which is very inefficient if simply looping through a long sequence of integers in a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for&amp;lt;/source&amp;gt; loop. Python 2 has &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;xrange&amp;lt;/source&amp;gt; that behaves like the Python 3 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;.&amp;lt;/ref&amp;gt;. For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists and ranges are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm. Remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;. Namespaces are useful because they allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a function called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt;, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespaces within the function call, e.g. to use &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; at the Python command prompt to generate 5 random numbers&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, rather than assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;does not copy&amp;#039;&amp;#039;&amp;#039; values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; to a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array of values. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; does copy all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
NumPy array (and Python list) slices work in subtly different ways to MATLAB&amp;#039;s too. For example, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A[m:n]&amp;lt;/source&amp;gt; returns all values from the element with the index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;m&amp;lt;/source&amp;gt; to the element with index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;n-1&amp;lt;/source&amp;gt;, and because the first element has index 0, we receive the (m+1)&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; to n&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; values, e.g. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[0:10]&lt;br /&gt;
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[4:6]&lt;br /&gt;
[5, 6]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Compare this to MATLAB&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
r =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(1:10)&lt;br /&gt;
ans =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(4:6)&lt;br /&gt;
ans =&lt;br /&gt;
     4     5     6&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on whole arrays are much faster than the equivalent code using loops, as they allow optimal use of the processor (such code optimisation is often called vectorisation). In addition code using vector and matrix operations is often shorter and easier to read that the equivalent using loops.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, this operation is much quicker than growing arrays on-the-fly. In this example we preallocate two arrays at the Python prompt with 10,000 elements each, the first array contains integers and the second contains double precision floating point numbers.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced example ===&lt;br /&gt;
We now look at the Python equivalent of the [[Program_Flow_and_Logicals#Relevant_example|Relevant example on the MATLAB page]], which assumes we have &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; returns in a vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to:&lt;br /&gt;
&lt;br /&gt;
# Count the number of positive, negative and zero returns&lt;br /&gt;
# Create an array holding only the positive values&lt;br /&gt;
# Create another array holding only the negative values&lt;br /&gt;
# Compute the means of the positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The naive algorithm using a loop in Python is as follows.&lt;br /&gt;
# Find the length of the NumPy array holding  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=numpy.size(r)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus=0; Tzero=0; Tminus=0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate two sum variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;psum=0.0; nsum=0.0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Preallocate NumPy arrays &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=numpy.zeros(T)&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=numpy.zeros(T)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe)&lt;br /&gt;
# Set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;i=0&amp;lt;/source&amp;gt; &lt;br /&gt;
# Check whether &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; is greater, smaller or equal to 0&lt;br /&gt;
#* If &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;gt;0&amp;lt;/source&amp;gt;, set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus[Tplus]=r[i]&amp;lt;/source&amp;gt;, add &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;psum&amp;lt;/source&amp;gt;, and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;0&amp;lt;/source&amp;gt; set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus[Tminus]=r[i]&amp;lt;/source&amp;gt;, add &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;nsum&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Repeat 6 for &amp;lt;math&amp;gt;i=1,\ldots,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove spare zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=rplus[0:Tplus].copy()&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=rminus[0:Tminus].copy()&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus (the number of positive, negative and zero returns are stored in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;)&lt;br /&gt;
&lt;br /&gt;
The Python code is as follows, however note that this code isn&amp;#039;t completely free of vector operations, since removal of zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; is vectorised.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
T=numpy.size(r)&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0&lt;br /&gt;
psum=0.0;nsum=0.0&lt;br /&gt;
rplus=numpy.zeros(T);rminus=numpy.zeros(T)&lt;br /&gt;
for i in range(T):&lt;br /&gt;
   if r[i]&amp;gt;0:&lt;br /&gt;
      rplus[Tplus]=r[i]   #Store positive return in array rplus&lt;br /&gt;
      Tplus+=1            #Increase Tplus by one if return is positive&lt;br /&gt;
      psum+=r[i]          #Add return to sum of positive values&lt;br /&gt;
   elif r[i]&amp;lt;0:&lt;br /&gt;
      rminus[Tminus]=r[i] #Store negative return in array rminus&lt;br /&gt;
      Tminus+=1           #Increase Tminus by one if return is negative&lt;br /&gt;
      nsum+=r[i]          #Add return to sum of negative values&lt;br /&gt;
   else:&lt;br /&gt;
      Tzero+=1            #Increase Tzero by one if return is zero&lt;br /&gt;
rplus=rplus[0:Tplus].copy()      #Remove zeros from rplus&lt;br /&gt;
rminus=rminus[1:Tminus].copy()   #Remove zeros from rminus&lt;br /&gt;
meanplus=psum/Tplus              # Compute mean of positive returns &lt;br /&gt;
meanminus=nsum/Tminus            # Compute mean of negative returns &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
We can create an alternative algorithm that only uses vector operations, using the following algorithm.&lt;br /&gt;
# Create an array &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; containing the positive values from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;&lt;br /&gt;
# Create an array &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; containing the negative values from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the length of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and assign to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the length of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; and assign to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Calculate &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the mean of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; using vectorised functions&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
rminus=r[r&amp;lt;0].copy()         # Create an array containing negative returns&lt;br /&gt;
Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This version is much shorter and cleaner, and therefore easier to create and maintain.&lt;br /&gt;
&lt;br /&gt;
== Running Python programs ==&lt;br /&gt;
For people who are familiar with MATLAB it may be surprising to discover there is no simple way of running a Python program from within Python. If you want to run Python code using standard Python, your choices are either&lt;br /&gt;
# Launch it from outside Python, e.g. save to a file &amp;lt;code&amp;gt;myscript.py&amp;lt;/code&amp;gt; and at the command line enter &amp;lt;code&amp;gt;python myscript.py&amp;lt;/code&amp;gt;&lt;br /&gt;
# Convert the program to a function and use the [http://docs.python.org/3/tutorial/modules.html Python module functionality], e.g. save to a file &amp;lt;code&amp;gt;myfunctions.py&amp;lt;/code&amp;gt; and use Python&amp;#039;s &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;import&amp;lt;/source&amp;gt; to make the functions available.&lt;br /&gt;
&lt;br /&gt;
The Python prompt has other limitations too, for example, it doesn&amp;#039;t include commands like &amp;lt;source enclose=none&amp;gt;pwd&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;cd&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;pwd&amp;lt;/source&amp;gt;, etc. To avoid these limitations we can use [http://ipython.org/ IPython (Interactive Python)], which provides an command line interface which behaves far more like MATLAB. For example, if we save the first code snippet in [[Python/Program_Flow_and_Logicals#More_advanced_example|&amp;#039;&amp;#039;&amp;#039;More advanced Example&amp;#039;&amp;#039;&amp;#039; (see above)]] to a file called &amp;lt;code&amp;gt;ReturnAnalysis1.py&amp;lt;/code&amp;gt; then from within IPython we can create a vector of random values to be analysed, and use run to execute the program, noting that for MATLAB like behaviour where the script can see the current interactive namespace requies the -i flag. The -t flag is useful too, as it times how long the script takes to run.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
In [1]: n=100000000&lt;br /&gt;
In [2]: from numpy.random import rand&lt;br /&gt;
In [3]: r=rand(n)*10-5&lt;br /&gt;
In [4]: r=rand(n)*10-5&lt;br /&gt;
In [5]: run -i -t ReturnAnalysis1&lt;br /&gt;
&lt;br /&gt;
IPython CPU timings (estimated):&lt;br /&gt;
  User   :     150.84 s.&lt;br /&gt;
  System :       0.44 s.&lt;br /&gt;
Wall time:     151.72 s.&lt;br /&gt;
&lt;br /&gt;
In [6]: meanplus&lt;br /&gt;
Out[6]: 2.4998243901335169&lt;br /&gt;
&lt;br /&gt;
In [7]: meanminus&lt;br /&gt;
Out[7]: -2.4999279003309622&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In comparison, when running the vectorised version of the code (saved in &amp;lt;code&amp;gt;ReturnAnalysis2.py&amp;lt;/code&amp;gt;).....&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
In [8]: run -i -t ReturnAnalysis2&lt;br /&gt;
&lt;br /&gt;
IPython CPU timings (estimated):&lt;br /&gt;
  User   :       1.84 s.&lt;br /&gt;
  System :       0.34 s.&lt;br /&gt;
Wall time:       2.19 s.&lt;br /&gt;
&lt;br /&gt;
In [9]: meanplus&lt;br /&gt;
Out[9]: 2.4998243901335169&lt;br /&gt;
&lt;br /&gt;
In [10]: meanminus&lt;br /&gt;
Out[10]: -2.4999279003309622&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, when running the vectorised matlab version (saved to ReturnAnalysis2.m) the run time is......&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3206</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3206"/>
				<updated>2013-10-15T13:16:54Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* Running Python programs */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The following assumes use of Python 3 (version 3 of Python) as opposed to Python 2, since no more major releases are planned for version 2, version 3 is expected to be the future of Python. The two versions of Python, although similar, are not compatible in a forwards or backwards direction&amp;lt;ref&amp;gt;Although Python 2 and 3 are not totally compatible, Python 2.7 is close to Python 3. If you have to use Python 2, it is recommended using version 2.7, writing code as close to Python 3 as possible, and using tools like &amp;#039;&amp;#039;2to3&amp;#039;&amp;#039; to port to Python 3. Alternatively there is a Python compatibility packages called &amp;#039;&amp;#039;six&amp;#039;&amp;#039;.&amp;lt;/ref&amp;gt;, and some legacy code exists only as Python 2. Some differences between the two versions are discussed in the footnotes.&lt;br /&gt;
&lt;br /&gt;
= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list/range to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python 3. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. The advantage of a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; object over a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; is that the sequence of values are not stored in memory with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;. &amp;lt;ref&amp;gt;In Python 3 the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object. However the Python 2 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a list, i.e. stores every integer value required in memory which is very inefficient if simply looping through a long sequence of integers in a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for&amp;lt;/source&amp;gt; loop. Python 2 has &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;xrange&amp;lt;/source&amp;gt; that behaves like the Python 3 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;.&amp;lt;/ref&amp;gt;. For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists and ranges are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm. Remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;. Namespaces are useful because they allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a function called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt;, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespaces within the function call, e.g. to use &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; at the Python command prompt to generate 5 random numbers&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, rather than assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;does not copy&amp;#039;&amp;#039;&amp;#039; values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; to a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array of values. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; does copy all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
NumPy array (and Python list) slices work in subtly different ways to MATLAB&amp;#039;s too. For example, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A[m:n]&amp;lt;/source&amp;gt; returns all values from the element with the index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;m&amp;lt;/source&amp;gt; to the element with index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;n-1&amp;lt;/source&amp;gt;, and because the first element has index 0, we receive the (m+1)&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; to n&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; values, e.g. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[0:10]&lt;br /&gt;
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[4:6]&lt;br /&gt;
[5, 6]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Compare this to MATLAB&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
r =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(1:10)&lt;br /&gt;
ans =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(4:6)&lt;br /&gt;
ans =&lt;br /&gt;
     4     5     6&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on whole arrays are much faster than the equivalent code using loops, as they allow optimal use of the processor (such code optimisation is often called vectorisation). In addition code using vector and matrix operations is often shorter and easier to read that the equivalent using loops.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, this operation is much quicker than growing arrays on-the-fly. In this example we preallocate two arrays at the Python prompt with 10,000 elements each, the first array contains integers and the second contains double precision floating point numbers.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced example ===&lt;br /&gt;
We now look at the Python equivalent of the [[Program_Flow_and_Logicals#Relevant_example|Relevant example on the MATLAB page]], which assumes we have &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; returns in a vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to:&lt;br /&gt;
&lt;br /&gt;
# Count the number of positive, negative and zero returns&lt;br /&gt;
# Create an array holding only the positive values&lt;br /&gt;
# Create another array holding only the negative values&lt;br /&gt;
# Compute the means of the positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The naive algorithm using a loop in Python is as follows.&lt;br /&gt;
# Find the length of the NumPy array holding  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=numpy.size(r)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus=0; Tzero=0; Tminus=0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate two sum variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;psum=0.0; nsum=0.0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Preallocate NumPy arrays &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=numpy.zeros(T)&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=numpy.zeros(T)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe)&lt;br /&gt;
# Set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;i=0&amp;lt;/source&amp;gt; &lt;br /&gt;
# Check whether &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; is greater, smaller or equal to 0&lt;br /&gt;
#* If &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;gt;0&amp;lt;/source&amp;gt;, set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus[Tplus]=r[i]&amp;lt;/source&amp;gt;, add &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;psum&amp;lt;/source&amp;gt;, and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;0&amp;lt;/source&amp;gt; set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus[Tminus]=r[i]&amp;lt;/source&amp;gt;, add &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;nsum&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Repeat 6 for &amp;lt;math&amp;gt;i=1,\ldots,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove spare zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=rplus[0:Tplus].copy()&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=rminus[0:Tminus].copy()&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus (the number of positive, negative and zero returns are stored in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;)&lt;br /&gt;
&lt;br /&gt;
The Python code is as follows, however note that this code isn&amp;#039;t completely free of vector operations, since removal of zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; is vectorised.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
T=numpy.size(r)&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0&lt;br /&gt;
psum=0.0;nsum=0.0&lt;br /&gt;
rplus=numpy.zeros(T);rminus=numpy.zeros(T)&lt;br /&gt;
for i in range(T):&lt;br /&gt;
   if r[i]&amp;gt;0:&lt;br /&gt;
      rplus[Tplus]=r[i]   #Store positive return in array rplus&lt;br /&gt;
      Tplus+=1            #Increase Tplus by one if return is positive&lt;br /&gt;
      psum+=r[i]          #Add return to sum of positive values&lt;br /&gt;
   elif r[i]&amp;lt;0:&lt;br /&gt;
      rminus[Tminus]=r[i] #Store negative return in array rminus&lt;br /&gt;
      Tminus+=1           #Increase Tminus by one if return is negative&lt;br /&gt;
      nsum+=r[i]          #Add return to sum of negative values&lt;br /&gt;
   else:&lt;br /&gt;
      Tzero+=1            #Increase Tzero by one if return is zero&lt;br /&gt;
rplus=rplus[0:Tplus].copy()      #Remove zeros from rplus&lt;br /&gt;
rminus=rminus[1:Tminus].copy()   #Remove zeros from rminus&lt;br /&gt;
meanplus=psum/Tplus              # Compute mean of positive returns &lt;br /&gt;
meanminus=nsum/Tminus            # Compute mean of negative returns &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
We can create an alternative algorithm that only uses vector operations, using the following algorithm.&lt;br /&gt;
# Create an array &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; containing the positive values from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;&lt;br /&gt;
# Create an array &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; containing the negative values from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the length of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and assign to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the length of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; and assign to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Calculate &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the mean of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; using vectorised functions&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
rplus=r[r&amp;lt;0].copy()         # Create an array containing negative returns&lt;br /&gt;
Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This version is much shorter and cleaner, and therefore easier to create and maintain.&lt;br /&gt;
&lt;br /&gt;
== Running Python programs ==&lt;br /&gt;
For people who are familiar with MATLAB it may be surprising to discover there is no simple way of running a Python program from within Python. If you want to run Python code using standard Python, your choices are either&lt;br /&gt;
# Launch it from outside Python, e.g. save to a file &amp;lt;code&amp;gt;myscript.py&amp;lt;/code&amp;gt; and at the command line enter &amp;lt;code&amp;gt;python myscript.py&amp;lt;/code&amp;gt;&lt;br /&gt;
# Convert the program to a function and use the [http://docs.python.org/3/tutorial/modules.html Python module functionality], e.g. save to a file &amp;lt;code&amp;gt;myfunctions.py&amp;lt;/code&amp;gt; and use Python&amp;#039;s &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;import&amp;lt;/source&amp;gt; to make the functions available.&lt;br /&gt;
&lt;br /&gt;
The Python prompt has other limitations too, for example, it doesn&amp;#039;t include commands like &amp;lt;source enclose=none&amp;gt;pwd&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;cd&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;pwd&amp;lt;/source&amp;gt;, etc. To avoid these limitations we can use [http://ipython.org/ IPython (Interactive Python)], which provides an command line interface which behaves far more like MATLAB. For example, if we save the first code snippet in [[Python/Program_Flow_and_Logicals#More_advanced_example|&amp;#039;&amp;#039;&amp;#039;More advanced Example&amp;#039;&amp;#039;&amp;#039; (see above)]] to a file called &amp;lt;code&amp;gt;ReturnAnalysis1.py&amp;lt;/code&amp;gt; then from within IPython we can create a vector of random values to be analysed, and use run to execute the program, noting that for MATLAB like behaviour where the script can see the current interactive namespace requies the -i flag. The -t flag is useful too, as it times how long the script takes to run.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
In [1]: n=100000000&lt;br /&gt;
In [2]: from numpy.random import rand&lt;br /&gt;
In [3]: r=rand(n)*10-5&lt;br /&gt;
In [4]: r=rand(n)*10-5&lt;br /&gt;
In [5]: run -i -t ReturnAnalysis1&lt;br /&gt;
&lt;br /&gt;
IPython CPU timings (estimated):&lt;br /&gt;
  User   :     150.84 s.&lt;br /&gt;
  System :       0.44 s.&lt;br /&gt;
Wall time:     151.72 s.&lt;br /&gt;
&lt;br /&gt;
In [6]: meanplus&lt;br /&gt;
Out[6]: 2.4998243901335169&lt;br /&gt;
&lt;br /&gt;
In [7]: meanminus&lt;br /&gt;
Out[7]: -2.4999279003309622&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In comparison, when running the vectorised version of the code (saved in &amp;lt;code&amp;gt;ReturnAnalysis2.py&amp;lt;/code&amp;gt;).....&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
In [8]: run -i -t ReturnAnalysis2&lt;br /&gt;
&lt;br /&gt;
IPython CPU timings (estimated):&lt;br /&gt;
  User   :       1.84 s.&lt;br /&gt;
  System :       0.34 s.&lt;br /&gt;
Wall time:       2.19 s.&lt;br /&gt;
&lt;br /&gt;
In [9]: meanplus&lt;br /&gt;
Out[9]: 2.4998243901335169&lt;br /&gt;
&lt;br /&gt;
In [10]: meanminus&lt;br /&gt;
Out[10]: -2.4999279003309622&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, when running the vectorised matlab version (saved to ReturnAnalysis2.m) the run time is......&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3205</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3205"/>
				<updated>2013-10-15T12:47:27Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* More advanced example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The following assumes use of Python 3 (version 3 of Python) as opposed to Python 2, since no more major releases are planned for version 2, version 3 is expected to be the future of Python. The two versions of Python, although similar, are not compatible in a forwards or backwards direction&amp;lt;ref&amp;gt;Although Python 2 and 3 are not totally compatible, Python 2.7 is close to Python 3. If you have to use Python 2, it is recommended using version 2.7, writing code as close to Python 3 as possible, and using tools like &amp;#039;&amp;#039;2to3&amp;#039;&amp;#039; to port to Python 3. Alternatively there is a Python compatibility packages called &amp;#039;&amp;#039;six&amp;#039;&amp;#039;.&amp;lt;/ref&amp;gt;, and some legacy code exists only as Python 2. Some differences between the two versions are discussed in the footnotes.&lt;br /&gt;
&lt;br /&gt;
= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list/range to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python 3. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. The advantage of a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; object over a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; is that the sequence of values are not stored in memory with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;. &amp;lt;ref&amp;gt;In Python 3 the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object. However the Python 2 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a list, i.e. stores every integer value required in memory which is very inefficient if simply looping through a long sequence of integers in a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for&amp;lt;/source&amp;gt; loop. Python 2 has &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;xrange&amp;lt;/source&amp;gt; that behaves like the Python 3 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;.&amp;lt;/ref&amp;gt;. For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists and ranges are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm. Remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;. Namespaces are useful because they allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a function called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt;, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespaces within the function call, e.g. to use &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; at the Python command prompt to generate 5 random numbers&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, rather than assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;does not copy&amp;#039;&amp;#039;&amp;#039; values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; to a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array of values. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; does copy all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
NumPy array (and Python list) slices work in subtly different ways to MATLAB&amp;#039;s too. For example, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A[m:n]&amp;lt;/source&amp;gt; returns all values from the element with the index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;m&amp;lt;/source&amp;gt; to the element with index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;n-1&amp;lt;/source&amp;gt;, and because the first element has index 0, we receive the (m+1)&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; to n&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; values, e.g. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[0:10]&lt;br /&gt;
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[4:6]&lt;br /&gt;
[5, 6]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Compare this to MATLAB&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
r =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(1:10)&lt;br /&gt;
ans =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(4:6)&lt;br /&gt;
ans =&lt;br /&gt;
     4     5     6&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on whole arrays are much faster than the equivalent code using loops, as they allow optimal use of the processor (such code optimisation is often called vectorisation). In addition code using vector and matrix operations is often shorter and easier to read that the equivalent using loops.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, this operation is much quicker than growing arrays on-the-fly. In this example we preallocate two arrays at the Python prompt with 10,000 elements each, the first array contains integers and the second contains double precision floating point numbers.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced example ===&lt;br /&gt;
We now look at the Python equivalent of the [[Program_Flow_and_Logicals#Relevant_example|Relevant example on the MATLAB page]], which assumes we have &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; returns in a vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to:&lt;br /&gt;
&lt;br /&gt;
# Count the number of positive, negative and zero returns&lt;br /&gt;
# Create an array holding only the positive values&lt;br /&gt;
# Create another array holding only the negative values&lt;br /&gt;
# Compute the means of the positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The naive algorithm using a loop in Python is as follows.&lt;br /&gt;
# Find the length of the NumPy array holding  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=numpy.size(r)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus=0; Tzero=0; Tminus=0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate two sum variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;psum=0.0; nsum=0.0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Preallocate NumPy arrays &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=numpy.zeros(T)&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=numpy.zeros(T)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe)&lt;br /&gt;
# Set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;i=0&amp;lt;/source&amp;gt; &lt;br /&gt;
# Check whether &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; is greater, smaller or equal to 0&lt;br /&gt;
#* If &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;gt;0&amp;lt;/source&amp;gt;, set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus[Tplus]=r[i]&amp;lt;/source&amp;gt;, add &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;psum&amp;lt;/source&amp;gt;, and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;0&amp;lt;/source&amp;gt; set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus[Tminus]=r[i]&amp;lt;/source&amp;gt;, add &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;nsum&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Repeat 6 for &amp;lt;math&amp;gt;i=1,\ldots,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove spare zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=rplus[0:Tplus].copy()&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=rminus[0:Tminus].copy()&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus (the number of positive, negative and zero returns are stored in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;)&lt;br /&gt;
&lt;br /&gt;
The Python code is as follows, however note that this code isn&amp;#039;t completely free of vector operations, since removal of zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; is vectorised.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
T=numpy.size(r)&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0&lt;br /&gt;
psum=0.0;nsum=0.0&lt;br /&gt;
rplus=numpy.zeros(T);rminus=numpy.zeros(T)&lt;br /&gt;
for i in range(T):&lt;br /&gt;
   if r[i]&amp;gt;0:&lt;br /&gt;
      rplus[Tplus]=r[i]   #Store positive return in array rplus&lt;br /&gt;
      Tplus+=1            #Increase Tplus by one if return is positive&lt;br /&gt;
      psum+=r[i]          #Add return to sum of positive values&lt;br /&gt;
   elif r[i]&amp;lt;0:&lt;br /&gt;
      rminus[Tminus]=r[i] #Store negative return in array rminus&lt;br /&gt;
      Tminus+=1           #Increase Tminus by one if return is negative&lt;br /&gt;
      nsum+=r[i]          #Add return to sum of negative values&lt;br /&gt;
   else:&lt;br /&gt;
      Tzero+=1            #Increase Tzero by one if return is zero&lt;br /&gt;
rplus=rplus[0:Tplus].copy()      #Remove zeros from rplus&lt;br /&gt;
rminus=rminus[1:Tminus].copy()   #Remove zeros from rminus&lt;br /&gt;
meanplus=psum/Tplus              # Compute mean of positive returns &lt;br /&gt;
meanminus=nsum/Tminus            # Compute mean of negative returns &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
We can create an alternative algorithm that only uses vector operations, using the following algorithm.&lt;br /&gt;
# Create an array &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; containing the positive values from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;&lt;br /&gt;
# Create an array &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; containing the negative values from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the length of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and assign to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the length of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; and assign to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Calculate &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the mean of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; using vectorised functions&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
rplus=r[r&amp;lt;0].copy()         # Create an array containing negative returns&lt;br /&gt;
Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This version is much shorter and cleaner, and therefore easier to create and maintain.&lt;br /&gt;
&lt;br /&gt;
== Running Python programs ==&lt;br /&gt;
For people who are familiar with MATLAB it may be surprising to discover there is no simple way of running a Python program from within Python. If you want to run Python code using standard Python, your choices are either&lt;br /&gt;
# Launch it from outside Python, e.g. save to a file &amp;lt;code&amp;gt;myscript.py&amp;lt;/code&amp;gt; and at the command line enter &amp;lt;code&amp;gt;python myscript.py&amp;lt;/code&amp;gt;&lt;br /&gt;
# Convert the program to a function and use the [http://docs.python.org/3/tutorial/modules.html Python module functionality], e.g. save to a file &amp;lt;code&amp;gt;myfunctions.py&amp;lt;/code&amp;gt; and use Python&amp;#039;s &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;import&amp;lt;/source&amp;gt; to make the functions available.&lt;br /&gt;
&lt;br /&gt;
The Python prompt has other limitations too, for example, it doesn&amp;#039;t include commands like &amp;lt;source enclose=none&amp;gt;pwd&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;cd&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;pwd&amp;lt;/source&amp;gt;, etc. To avoid these limitations we can use [http://ipython.org/ IPython (Interactive Python)], which provides an command line interface which behaves far more like MATLAB. For example, if we save the first code snippet in [[Python/Program_Flow_and_Logicals#More_advanced_example|&amp;#039;&amp;#039;&amp;#039;More advanced Example&amp;#039;&amp;#039;&amp;#039; (see above)]] to a file called &amp;lt;code&amp;gt;ReturnAnalysis1.py&amp;lt;/code&amp;gt; then from within IPython we can create a vector of random values to be analysed, and&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3204</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3204"/>
				<updated>2013-10-15T12:46:46Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* More advanced example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The following assumes use of Python 3 (version 3 of Python) as opposed to Python 2, since no more major releases are planned for version 2, version 3 is expected to be the future of Python. The two versions of Python, although similar, are not compatible in a forwards or backwards direction&amp;lt;ref&amp;gt;Although Python 2 and 3 are not totally compatible, Python 2.7 is close to Python 3. If you have to use Python 2, it is recommended using version 2.7, writing code as close to Python 3 as possible, and using tools like &amp;#039;&amp;#039;2to3&amp;#039;&amp;#039; to port to Python 3. Alternatively there is a Python compatibility packages called &amp;#039;&amp;#039;six&amp;#039;&amp;#039;.&amp;lt;/ref&amp;gt;, and some legacy code exists only as Python 2. Some differences between the two versions are discussed in the footnotes.&lt;br /&gt;
&lt;br /&gt;
= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list/range to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python 3. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. The advantage of a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; object over a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; is that the sequence of values are not stored in memory with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;. &amp;lt;ref&amp;gt;In Python 3 the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object. However the Python 2 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a list, i.e. stores every integer value required in memory which is very inefficient if simply looping through a long sequence of integers in a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for&amp;lt;/source&amp;gt; loop. Python 2 has &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;xrange&amp;lt;/source&amp;gt; that behaves like the Python 3 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;.&amp;lt;/ref&amp;gt;. For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists and ranges are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm. Remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;. Namespaces are useful because they allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a function called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt;, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespaces within the function call, e.g. to use &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; at the Python command prompt to generate 5 random numbers&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, rather than assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;does not copy&amp;#039;&amp;#039;&amp;#039; values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; to a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array of values. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; does copy all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
NumPy array (and Python list) slices work in subtly different ways to MATLAB&amp;#039;s too. For example, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A[m:n]&amp;lt;/source&amp;gt; returns all values from the element with the index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;m&amp;lt;/source&amp;gt; to the element with index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;n-1&amp;lt;/source&amp;gt;, and because the first element has index 0, we receive the (m+1)&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; to n&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; values, e.g. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[0:10]&lt;br /&gt;
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[4:6]&lt;br /&gt;
[5, 6]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Compare this to MATLAB&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
r =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(1:10)&lt;br /&gt;
ans =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(4:6)&lt;br /&gt;
ans =&lt;br /&gt;
     4     5     6&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on whole arrays are much faster than the equivalent code using loops, as they allow optimal use of the processor (such code optimisation is often called vectorisation). In addition code using vector and matrix operations is often shorter and easier to read that the equivalent using loops.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, this operation is much quicker than growing arrays on-the-fly. In this example we preallocate two arrays at the Python prompt with 10,000 elements each, the first array contains integers and the second contains double precision floating point numbers.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced example ===&lt;br /&gt;
We now look at the Python equivalent of the [[Program_Flow_and_Logicals#Relevant_example|Relevant example on the MATLAB page]], which assumes we have &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; returns in a vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to:&lt;br /&gt;
&lt;br /&gt;
# Count the number of positive, negative and zero returns&lt;br /&gt;
# Create an array holding only the positive values&lt;br /&gt;
# Create another array holding only the negative values&lt;br /&gt;
# Compute the means of the positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The naive algorithm using a loop in Python is as follows.&lt;br /&gt;
# Find the length of the NumPy array holding  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=numpy.size(r)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus=0; Tzero=0; Tminus=0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate two sum variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;psum=0.0; nsum=0.0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Preallocate NumPy arrays &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=numpy.zeros(T)&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=numpy.zeros(T)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe)&lt;br /&gt;
# Set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;i=0&amp;lt;/source&amp;gt; &lt;br /&gt;
# Check whether &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; is greater, smaller or equal to 0&lt;br /&gt;
#* If &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;gt;0&amp;lt;/source&amp;gt;, set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus[Tplus]=r[i]&amp;lt;/source&amp;gt;, add &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;psum&amp;lt;/source&amp;gt;, and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;0&amp;lt;/source&amp;gt; set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus[Tminus]=r[i]&amp;lt;/source&amp;gt;, add &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;nsum&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Repeat 6 for &amp;lt;math&amp;gt;i=1,\ldots,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove spare zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=rplus[0:Tplus].copy()&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=rminus[0:Tminus].copy()&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus (the number of positive, negative and zero returns are stored in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;)&lt;br /&gt;
&lt;br /&gt;
The Python code is as follows, however note that this code isn&amp;#039;t completely free of vector operations, since removal of zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; is vectorised.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=numpy.size(r)&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0&lt;br /&gt;
psum=0.0;nsum=0.0&lt;br /&gt;
rplus=numpy.zeros(T);rminus=numpy.zeros(T)&lt;br /&gt;
for i in range(T):&lt;br /&gt;
   if r[i]&amp;gt;0:&lt;br /&gt;
      rplus[Tplus]=r[i]   #Store positive return in array rplus&lt;br /&gt;
      Tplus+=1            #Increase Tplus by one if return is positive&lt;br /&gt;
      psum+=r[i]          #Add return to sum of positive values&lt;br /&gt;
   elif r[i]&amp;lt;0:&lt;br /&gt;
      rminus[Tminus]=r[i] #Store negative return in array rminus&lt;br /&gt;
      Tminus+=1           #Increase Tminus by one if return is negative&lt;br /&gt;
      nsum+=r[i]          #Add return to sum of negative values&lt;br /&gt;
   else:&lt;br /&gt;
      Tzero+=1            #Increase Tzero by one if return is zero&lt;br /&gt;
rplus=rplus[0:Tplus].copy()      #Remove zeros from rplus&lt;br /&gt;
rminus=rminus[1:Tminus].copy()   #Remove zeros from rminus&lt;br /&gt;
meanplus=psum/Tplus              # Compute mean of positive returns &lt;br /&gt;
meanminus=nsum/Tminus            # Compute mean of negative returns &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
We can create an alternative algorithm that only uses vector operations, using the following algorithm.&lt;br /&gt;
# Create an array &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; containing the positive values from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;&lt;br /&gt;
# Create an array &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; containing the negative values from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the length of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and assign to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the length of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; and assign to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Calculate &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the mean of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; using vectorised functions&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
rplus=r[r&amp;lt;0].copy()         # Create an array containing negative returns&lt;br /&gt;
Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This version is much shorter and cleaner, and therefore easier to create and maintain.&lt;br /&gt;
&lt;br /&gt;
== Running Python programs ==&lt;br /&gt;
For people who are familiar with MATLAB it may be surprising to discover there is no simple way of running a Python program from within Python. If you want to run Python code using standard Python, your choices are either&lt;br /&gt;
# Launch it from outside Python, e.g. save to a file &amp;lt;code&amp;gt;myscript.py&amp;lt;/code&amp;gt; and at the command line enter &amp;lt;code&amp;gt;python myscript.py&amp;lt;/code&amp;gt;&lt;br /&gt;
# Convert the program to a function and use the [http://docs.python.org/3/tutorial/modules.html Python module functionality], e.g. save to a file &amp;lt;code&amp;gt;myfunctions.py&amp;lt;/code&amp;gt; and use Python&amp;#039;s &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;import&amp;lt;/source&amp;gt; to make the functions available.&lt;br /&gt;
&lt;br /&gt;
The Python prompt has other limitations too, for example, it doesn&amp;#039;t include commands like &amp;lt;source enclose=none&amp;gt;pwd&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;cd&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;pwd&amp;lt;/source&amp;gt;, etc. To avoid these limitations we can use [http://ipython.org/ IPython (Interactive Python)], which provides an command line interface which behaves far more like MATLAB. For example, if we save the first code snippet in [[Python/Program_Flow_and_Logicals#More_advanced_example|&amp;#039;&amp;#039;&amp;#039;More advanced Example&amp;#039;&amp;#039;&amp;#039; (see above)]] to a file called &amp;lt;code&amp;gt;ReturnAnalysis1.py&amp;lt;/code&amp;gt; then from within IPython we can create a vector of random values to be analysed, and&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3203</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3203"/>
				<updated>2013-10-15T12:46:23Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* Running code in Python */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The following assumes use of Python 3 (version 3 of Python) as opposed to Python 2, since no more major releases are planned for version 2, version 3 is expected to be the future of Python. The two versions of Python, although similar, are not compatible in a forwards or backwards direction&amp;lt;ref&amp;gt;Although Python 2 and 3 are not totally compatible, Python 2.7 is close to Python 3. If you have to use Python 2, it is recommended using version 2.7, writing code as close to Python 3 as possible, and using tools like &amp;#039;&amp;#039;2to3&amp;#039;&amp;#039; to port to Python 3. Alternatively there is a Python compatibility packages called &amp;#039;&amp;#039;six&amp;#039;&amp;#039;.&amp;lt;/ref&amp;gt;, and some legacy code exists only as Python 2. Some differences between the two versions are discussed in the footnotes.&lt;br /&gt;
&lt;br /&gt;
= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list/range to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python 3. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. The advantage of a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; object over a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; is that the sequence of values are not stored in memory with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;. &amp;lt;ref&amp;gt;In Python 3 the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object. However the Python 2 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a list, i.e. stores every integer value required in memory which is very inefficient if simply looping through a long sequence of integers in a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for&amp;lt;/source&amp;gt; loop. Python 2 has &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;xrange&amp;lt;/source&amp;gt; that behaves like the Python 3 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;.&amp;lt;/ref&amp;gt;. For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists and ranges are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm. Remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;. Namespaces are useful because they allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a function called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt;, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespaces within the function call, e.g. to use &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; at the Python command prompt to generate 5 random numbers&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, rather than assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;does not copy&amp;#039;&amp;#039;&amp;#039; values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; to a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array of values. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; does copy all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
NumPy array (and Python list) slices work in subtly different ways to MATLAB&amp;#039;s too. For example, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A[m:n]&amp;lt;/source&amp;gt; returns all values from the element with the index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;m&amp;lt;/source&amp;gt; to the element with index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;n-1&amp;lt;/source&amp;gt;, and because the first element has index 0, we receive the (m+1)&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; to n&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; values, e.g. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[0:10]&lt;br /&gt;
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[4:6]&lt;br /&gt;
[5, 6]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Compare this to MATLAB&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
r =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(1:10)&lt;br /&gt;
ans =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(4:6)&lt;br /&gt;
ans =&lt;br /&gt;
     4     5     6&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on whole arrays are much faster than the equivalent code using loops, as they allow optimal use of the processor (such code optimisation is often called vectorisation). In addition code using vector and matrix operations is often shorter and easier to read that the equivalent using loops.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, this operation is much quicker than growing arrays on-the-fly. In this example we preallocate two arrays at the Python prompt with 10,000 elements each, the first array contains integers and the second contains double precision floating point numbers.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced example ===&lt;br /&gt;
We now look at the Python equivalent of the [[Program_Flow_and_Logicals#Relevant_example|Relevant example on the MATLAB page]], which assumes we have &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; returns in a vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to:&lt;br /&gt;
&lt;br /&gt;
# Count the number of positive, negative and zero returns&lt;br /&gt;
# Create an array holding only the positive values&lt;br /&gt;
# Create another array holding only the negative values&lt;br /&gt;
# Compute the means of the positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The naive algorithm using a loop in Python is as follows.&lt;br /&gt;
# Find the length of the NumPy array holding  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=numpy.size(r)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus=0; Tzero=0; Tminus=0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate two sum variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;psum=0.0; nsum=0.0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Preallocate NumPy arrays &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=numpy.zeros(T)&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=numpy.zeros(T)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe)&lt;br /&gt;
# Set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;i=0&amp;lt;/source&amp;gt; &lt;br /&gt;
# Check whether &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; is greater, smaller or equal to 0&lt;br /&gt;
#* If &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;gt;0&amp;lt;/source&amp;gt;, set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus[Tplus]=r[i]&amp;lt;/source&amp;gt;, add &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;psum&amp;lt;/source&amp;gt;, and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;0&amp;lt;/source&amp;gt; set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus[Tminus]=r[i]&amp;lt;/source&amp;gt;, add &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;nsum&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Repeat 6 for &amp;lt;math&amp;gt;i=1,\ldots,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove spare zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=rplus[0:Tplus].copy()&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=rminus[0:Tminus].copy()&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus (the number of positive, negative and zero returns are stored in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;)&lt;br /&gt;
&lt;br /&gt;
The Python code is as follows, however note that this code isn&amp;#039;t completely free of vector operations, since removal of zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; is vectorised.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=numpy,size(r)&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0&lt;br /&gt;
psum=0.0;nsum=0.0&lt;br /&gt;
rplus=numpy.zeros(T);rminus=numpy.zeros(T)&lt;br /&gt;
for i in range(T):&lt;br /&gt;
   if r[i]&amp;gt;0:&lt;br /&gt;
      rplus[Tplus]=r[i]   #Store positive return in array rplus&lt;br /&gt;
      Tplus+=1            #Increase Tplus by one if return is positive&lt;br /&gt;
      psum+=r[i]          #Add return to sum of positive values&lt;br /&gt;
   elif r[i]&amp;lt;0:&lt;br /&gt;
      rminus[Tminus]=r[i] #Store negative return in array rminus&lt;br /&gt;
      Tminus+=1           #Increase Tminus by one if return is negative&lt;br /&gt;
      nsum+=r[i]          #Add return to sum of negative values&lt;br /&gt;
   else:&lt;br /&gt;
      Tzero+=1            #Increase Tzero by one if return is zero&lt;br /&gt;
rplus=rplus[0:Tplus].copy()      #Remove zeros from rplus&lt;br /&gt;
rminus=rminus[1:Tminus].copy()   #Remove zeros from rminus&lt;br /&gt;
meanplus=psum/Tplus              # Compute mean of positive returns &lt;br /&gt;
meanminus=nsum/Tminus            # Compute mean of negative returns &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
We can create an alternative algorithm that only uses vector operations, using the following algorithm.&lt;br /&gt;
# Create an array &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; containing the positive values from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;&lt;br /&gt;
# Create an array &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; containing the negative values from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the length of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and assign to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the length of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; and assign to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Calculate &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the mean of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; using vectorised functions&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
rplus=r[r&amp;lt;0].copy()         # Create an array containing negative returns&lt;br /&gt;
Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This version is much shorter and cleaner, and therefore easier to create and maintain.&lt;br /&gt;
&lt;br /&gt;
== Running Python programs ==&lt;br /&gt;
For people who are familiar with MATLAB it may be surprising to discover there is no simple way of running a Python program from within Python. If you want to run Python code using standard Python, your choices are either&lt;br /&gt;
# Launch it from outside Python, e.g. save to a file &amp;lt;code&amp;gt;myscript.py&amp;lt;/code&amp;gt; and at the command line enter &amp;lt;code&amp;gt;python myscript.py&amp;lt;/code&amp;gt;&lt;br /&gt;
# Convert the program to a function and use the [http://docs.python.org/3/tutorial/modules.html Python module functionality], e.g. save to a file &amp;lt;code&amp;gt;myfunctions.py&amp;lt;/code&amp;gt; and use Python&amp;#039;s &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;import&amp;lt;/source&amp;gt; to make the functions available.&lt;br /&gt;
&lt;br /&gt;
The Python prompt has other limitations too, for example, it doesn&amp;#039;t include commands like &amp;lt;source enclose=none&amp;gt;pwd&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;cd&amp;lt;/source&amp;gt;, &amp;lt;source enclose=none&amp;gt;pwd&amp;lt;/source&amp;gt;, etc. To avoid these limitations we can use [http://ipython.org/ IPython (Interactive Python)], which provides an command line interface which behaves far more like MATLAB. For example, if we save the first code snippet in [[Python/Program_Flow_and_Logicals#More_advanced_example|&amp;#039;&amp;#039;&amp;#039;More advanced Example&amp;#039;&amp;#039;&amp;#039; (see above)]] to a file called &amp;lt;code&amp;gt;ReturnAnalysis1.py&amp;lt;/code&amp;gt; then from within IPython we can create a vector of random values to be analysed, and&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3202</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3202"/>
				<updated>2013-10-15T12:04:05Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* More advanced example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The following assumes use of Python 3 (version 3 of Python) as opposed to Python 2, since no more major releases are planned for version 2, version 3 is expected to be the future of Python. The two versions of Python, although similar, are not compatible in a forwards or backwards direction&amp;lt;ref&amp;gt;Although Python 2 and 3 are not totally compatible, Python 2.7 is close to Python 3. If you have to use Python 2, it is recommended using version 2.7, writing code as close to Python 3 as possible, and using tools like &amp;#039;&amp;#039;2to3&amp;#039;&amp;#039; to port to Python 3. Alternatively there is a Python compatibility packages called &amp;#039;&amp;#039;six&amp;#039;&amp;#039;.&amp;lt;/ref&amp;gt;, and some legacy code exists only as Python 2. Some differences between the two versions are discussed in the footnotes.&lt;br /&gt;
&lt;br /&gt;
= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list/range to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python 3. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. The advantage of a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; object over a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; is that the sequence of values are not stored in memory with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;. &amp;lt;ref&amp;gt;In Python 3 the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object. However the Python 2 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a list, i.e. stores every integer value required in memory which is very inefficient if simply looping through a long sequence of integers in a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for&amp;lt;/source&amp;gt; loop. Python 2 has &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;xrange&amp;lt;/source&amp;gt; that behaves like the Python 3 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;.&amp;lt;/ref&amp;gt;. For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists and ranges are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm. Remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;. Namespaces are useful because they allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a function called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt;, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespaces within the function call, e.g. to use &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; at the Python command prompt to generate 5 random numbers&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, rather than assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;does not copy&amp;#039;&amp;#039;&amp;#039; values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; to a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array of values. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; does copy all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
NumPy array (and Python list) slices work in subtly different ways to MATLAB&amp;#039;s too. For example, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A[m:n]&amp;lt;/source&amp;gt; returns all values from the element with the index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;m&amp;lt;/source&amp;gt; to the element with index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;n-1&amp;lt;/source&amp;gt;, and because the first element has index 0, we receive the (m+1)&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; to n&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; values, e.g. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[0:10]&lt;br /&gt;
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[4:6]&lt;br /&gt;
[5, 6]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Compare this to MATLAB&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
r =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(1:10)&lt;br /&gt;
ans =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(4:6)&lt;br /&gt;
ans =&lt;br /&gt;
     4     5     6&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on whole arrays are much faster than the equivalent code using loops, as they allow optimal use of the processor (such code optimisation is often called vectorisation). In addition code using vector and matrix operations is often shorter and easier to read that the equivalent using loops.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, this operation is much quicker than growing arrays on-the-fly. In this example we preallocate two arrays at the Python prompt with 10,000 elements each, the first array contains integers and the second contains double precision floating point numbers.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced example ===&lt;br /&gt;
We now look at the Python equivalent of the [[Program_Flow_and_Logicals#Relevant_example|Relevant example on the MATLAB page]], which assumes we have &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; returns in a vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to:&lt;br /&gt;
&lt;br /&gt;
# Count the number of positive, negative and zero returns&lt;br /&gt;
# Create an array holding only the positive values&lt;br /&gt;
# Create another array holding only the negative values&lt;br /&gt;
# Compute the means of the positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The naive algorithm using a loop in Python is as follows.&lt;br /&gt;
# Find the length of the NumPy array holding  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=numpy.size(r)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus=0; Tzero=0; Tminus=0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate two sum variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;psum=0.0; nsum=0.0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Preallocate NumPy arrays &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=numpy.zeros(T)&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=numpy.zeros(T)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe)&lt;br /&gt;
# Set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;i=0&amp;lt;/source&amp;gt; &lt;br /&gt;
# Check whether &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; is greater, smaller or equal to 0&lt;br /&gt;
#* If &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;gt;0&amp;lt;/source&amp;gt;, set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus[Tplus]=r[i]&amp;lt;/source&amp;gt;, add &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;psum&amp;lt;/source&amp;gt;, and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;0&amp;lt;/source&amp;gt; set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus[Tminus]=r[i]&amp;lt;/source&amp;gt;, add &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;nsum&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Repeat 6 for &amp;lt;math&amp;gt;i=1,\ldots,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove spare zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=rplus[0:Tplus].copy()&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=rminus[0:Tminus].copy()&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus (the number of positive, negative and zero returns are stored in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;)&lt;br /&gt;
&lt;br /&gt;
The Python code is as follows, however note that this code isn&amp;#039;t completely free of vector operations, since removal of zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; is vectorised.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=numpy,size(r)&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0&lt;br /&gt;
psum=0.0;nsum=0.0&lt;br /&gt;
rplus=numpy.zeros(T);rminus=numpy.zeros(T)&lt;br /&gt;
for i in range(T):&lt;br /&gt;
   if r[i]&amp;gt;0:&lt;br /&gt;
      rplus[Tplus]=r[i]   #Store positive return in array rplus&lt;br /&gt;
      Tplus+=1            #Increase Tplus by one if return is positive&lt;br /&gt;
      psum+=r[i]          #Add return to sum of positive values&lt;br /&gt;
   elif r[i]&amp;lt;0:&lt;br /&gt;
      rminus[Tminus]=r[i] #Store negative return in array rminus&lt;br /&gt;
      Tminus+=1           #Increase Tminus by one if return is negative&lt;br /&gt;
      nsum+=r[i]          #Add return to sum of negative values&lt;br /&gt;
   else:&lt;br /&gt;
      Tzero+=1            #Increase Tzero by one if return is zero&lt;br /&gt;
rplus=rplus[0:Tplus].copy()      #Remove zeros from rplus&lt;br /&gt;
rminus=rminus[1:Tminus].copy()   #Remove zeros from rminus&lt;br /&gt;
meanplus=psum/Tplus              # Compute mean of positive returns &lt;br /&gt;
meanminus=nsum/Tminus            # Compute mean of negative returns &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
We can create an alternative algorithm that only uses vector operations, using the following algorithm.&lt;br /&gt;
# Create an array &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; containing the positive values from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;&lt;br /&gt;
# Create an array &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; containing the negative values from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the length of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and assign to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the length of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; and assign to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Calculate &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the mean of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; using vectorised functions&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
rplus=r[r&amp;lt;0].copy()         # Create an array containing negative returns&lt;br /&gt;
Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This version is much shorter and cleaner, and therefore easier to create and maintain.&lt;br /&gt;
&lt;br /&gt;
== Running code in Python ==&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3201</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3201"/>
				<updated>2013-10-15T11:29:41Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* More advanced example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The following assumes use of Python 3 (version 3 of Python) as opposed to Python 2, since no more major releases are planned for version 2, version 3 is expected to be the future of Python. The two versions of Python, although similar, are not compatible in a forwards or backwards direction&amp;lt;ref&amp;gt;Although Python 2 and 3 are not totally compatible, Python 2.7 is close to Python 3. If you have to use Python 2, it is recommended using version 2.7, writing code as close to Python 3 as possible, and using tools like &amp;#039;&amp;#039;2to3&amp;#039;&amp;#039; to port to Python 3. Alternatively there is a Python compatibility packages called &amp;#039;&amp;#039;six&amp;#039;&amp;#039;.&amp;lt;/ref&amp;gt;, and some legacy code exists only as Python 2. Some differences between the two versions are discussed in the footnotes.&lt;br /&gt;
&lt;br /&gt;
= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list/range to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python 3. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. The advantage of a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; object over a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; is that the sequence of values are not stored in memory with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;. &amp;lt;ref&amp;gt;In Python 3 the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object. However the Python 2 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a list, i.e. stores every integer value required in memory which is very inefficient if simply looping through a long sequence of integers in a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for&amp;lt;/source&amp;gt; loop. Python 2 has &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;xrange&amp;lt;/source&amp;gt; that behaves like the Python 3 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;.&amp;lt;/ref&amp;gt;. For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists and ranges are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm. Remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;. Namespaces are useful because they allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a function called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt;, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespaces within the function call, e.g. to use &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; at the Python command prompt to generate 5 random numbers&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, rather than assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;does not copy&amp;#039;&amp;#039;&amp;#039; values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; to a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array of values. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; does copy all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
NumPy array (and Python list) slices work in subtly different ways to MATLAB&amp;#039;s too. For example, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A[m:n]&amp;lt;/source&amp;gt; returns all values from the element with the index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;m&amp;lt;/source&amp;gt; to the element with index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;n-1&amp;lt;/source&amp;gt;, and because the first element has index 0, we receive the (m+1)&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; to n&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; values, e.g. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[0:10]&lt;br /&gt;
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[4:6]&lt;br /&gt;
[5, 6]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Compare this to MATLAB&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
r =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(1:10)&lt;br /&gt;
ans =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(4:6)&lt;br /&gt;
ans =&lt;br /&gt;
     4     5     6&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on whole arrays are much faster than the equivalent code using loops, as they allow optimal use of the processor (such code optimisation is often called vectorisation). In addition code using vector and matrix operations is often shorter and easier to read that the equivalent using loops.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, this operation is much quicker than growing arrays on-the-fly. In this example we preallocate two arrays at the Python prompt with 10,000 elements each, the first array contains integers and the second contains double precision floating point numbers.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced example ===&lt;br /&gt;
We now look at the Python equivalent of the [[Program_Flow_and_Logicals#Relevant_example|Relevant example on the MATLAB page]], which assumes we have &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; returns in a vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The naive algorithm using loops in Python is as follows.&lt;br /&gt;
# Find the length of the NumPy array holding  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=numpy.size(r)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus=0; Tzero=0; Tminus=0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Preallocate NumPy arrays &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=numpy.zeros(T)&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=numpy.zeros(T)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe)&lt;br /&gt;
# Set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;i=0&amp;lt;/source&amp;gt; &lt;br /&gt;
# Check whether &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; is greater, smaller or equal to 0&lt;br /&gt;
#* If &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;gt;0&amp;lt;/source&amp;gt;, set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus[Tplus]=r[i]&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;0&amp;lt;/source&amp;gt; set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus[Tminus]=r[i]&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Repeat 5 for &amp;lt;math&amp;gt;i=1,\ldots,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove spare zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=rplus[0:Tplus].copy()&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=rminus[0:Tminus].copy()&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus (the number of positive, negative and zero returns are stored in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;)&lt;br /&gt;
&lt;br /&gt;
The Python code is as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=numpy,size(r)&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0&lt;br /&gt;
rplus=numpy.zeros(T);rminus=numpy.zeros(T)&lt;br /&gt;
for i in range(T):&lt;br /&gt;
   if r[i]&amp;gt;0:&lt;br /&gt;
      rplus[Tplus]=r[i]   #Store positive return in array rplus&lt;br /&gt;
      Tplus+=1            #Increase Tplus by one if return is positive&lt;br /&gt;
   elif r[i]&amp;lt;0:&lt;br /&gt;
      rminus[Tminus]=r[i] #Store negative return in array rminus&lt;br /&gt;
      Tminus+=1           #Increase Tminus by one if return is negative&lt;br /&gt;
   else:&lt;br /&gt;
      Tzero+=1            #Increase Tzero by one if return is zero&lt;br /&gt;
rplus=rplus[0:Tplus].copy()        #Remove zeros from rplus&lt;br /&gt;
rminus=rminus[1:Tminus].copy()     #Remove zeros from rminus&lt;br /&gt;
meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Using vectorisation we can create an alternative algorithm&lt;br /&gt;
# Create an array &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; containing the positive values &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;&lt;br /&gt;
# Create an array &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; containing the negative values &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the length of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and assign to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the length of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt; and assign to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
# Calculate &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Find the mean of &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
rplus=r[r&amp;gt;0].copy()         # Create an array containing positive returns&lt;br /&gt;
rplus=r[r&amp;lt;0].copy()         # Create an array containing negative returns&lt;br /&gt;
Tplus=len(rplus)            # Count how many positive returns there are &lt;br /&gt;
Tminus=len(rminus)          # Count how many negative returns there are &lt;br /&gt;
Tzero=len(r)-Tplus-Tminus   # Calculate the number of zero returns&lt;br /&gt;
meanplus=numpy.mean(rplus)         # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This version is much shorter and cleaner, and therefore easier to create and maintain.&lt;br /&gt;
&lt;br /&gt;
== Running code in Python ==&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3200</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3200"/>
				<updated>2013-10-15T10:56:51Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* Simple example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The following assumes use of Python 3 (version 3 of Python) as opposed to Python 2, since no more major releases are planned for version 2, version 3 is expected to be the future of Python. The two versions of Python, although similar, are not compatible in a forwards or backwards direction&amp;lt;ref&amp;gt;Although Python 2 and 3 are not totally compatible, Python 2.7 is close to Python 3. If you have to use Python 2, it is recommended using version 2.7, writing code as close to Python 3 as possible, and using tools like &amp;#039;&amp;#039;2to3&amp;#039;&amp;#039; to port to Python 3. Alternatively there is a Python compatibility packages called &amp;#039;&amp;#039;six&amp;#039;&amp;#039;.&amp;lt;/ref&amp;gt;, and some legacy code exists only as Python 2. Some differences between the two versions are discussed in the footnotes.&lt;br /&gt;
&lt;br /&gt;
= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list/range to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python 3. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. The advantage of a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; object over a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; is that the sequence of values are not stored in memory with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;. &amp;lt;ref&amp;gt;In Python 3 the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object. However the Python 2 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a list, i.e. stores every integer value required in memory which is very inefficient if simply looping through a long sequence of integers in a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for&amp;lt;/source&amp;gt; loop. Python 2 has &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;xrange&amp;lt;/source&amp;gt; that behaves like the Python 3 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;.&amp;lt;/ref&amp;gt;. For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists and ranges are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm. Remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;. Namespaces are useful because they allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a function called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt;, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespaces within the function call, e.g. to use &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; at the Python command prompt to generate 5 random numbers&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, rather than assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;does not copy&amp;#039;&amp;#039;&amp;#039; values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; to a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array of values. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; does copy all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
NumPy array (and Python list) slices work in subtly different ways to MATLAB&amp;#039;s too. For example, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A[m:n]&amp;lt;/source&amp;gt; returns all values from the element with the index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;m&amp;lt;/source&amp;gt; to the element with index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;n-1&amp;lt;/source&amp;gt;, and because the first element has index 0, we receive the (m+1)&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; to n&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; values, e.g. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[0:10]&lt;br /&gt;
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[4:6]&lt;br /&gt;
[5, 6]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Compare this to MATLAB&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
r =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(1:10)&lt;br /&gt;
ans =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(4:6)&lt;br /&gt;
ans =&lt;br /&gt;
     4     5     6&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on whole arrays are much faster than the equivalent code using loops, as they allow optimal use of the processor (such code optimisation is often called vectorisation). In addition code using vector and matrix operations is often shorter and easier to read that the equivalent using loops.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, this operation is much quicker than growing arrays on-the-fly. In this example we preallocate two arrays at the Python prompt with 10,000 elements each, the first array contains integers and the second contains double precision floating point numbers.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced example ===&lt;br /&gt;
We now look at the Python equivalent of the [[Program_Flow_and_Logicals#Relevant_example|Relevant example on the MATLAB page]], which assumed we have &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; returns in a vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The naive algorithm using loops in Python is as follows.&lt;br /&gt;
# Find the length of the NumPy array holding  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=numpy.size(r)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus=0; Tzero=0; Tminus=0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Preallocate NumPy arrays &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=numpy.zeros(T)&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=numpy.zeros(T)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe)&lt;br /&gt;
# Set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;i=0&amp;lt;/source&amp;gt; &lt;br /&gt;
# Check whether &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; is greater, smaller or equal to 0&lt;br /&gt;
#* If &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;gt;0&amp;lt;/source&amp;gt;, set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus[Tplus]=r[i]&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;0&amp;lt;/source&amp;gt; set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus[Tminus]=r[i]&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Repeat 5 for &amp;lt;math&amp;gt;i=1,\ldots,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=rplus[0:Tplus].copy()&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=rminus[0:Tminus].copy()&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=numpy,size(r)&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0&lt;br /&gt;
rplus=numpy.zeros(T);rminus=numpy.zeros(T)&lt;br /&gt;
for i in range(T):&lt;br /&gt;
   if r[i]&amp;gt;0:&lt;br /&gt;
      rplus[Tplus]=r[i]   #Store positive return in array rplus&lt;br /&gt;
      Tplus+=1            #Increase Tplus by one if return is positive&lt;br /&gt;
   elif r[i]&amp;lt;0:&lt;br /&gt;
      rminus[Tminus]=r[i] #Store negative return in array rminus&lt;br /&gt;
      Tminus+=1           #Increase Tminus by one if return is negative&lt;br /&gt;
   else:&lt;br /&gt;
      Tzero+=1            #Increase Tzero by one if return is zero&lt;br /&gt;
rplus=rplus[0:Tplus].copy()        #Remove zeros from rplus&lt;br /&gt;
rminus=rminus[1:Tminus].copy()     #Remove zeros from rminus&lt;br /&gt;
meanplus=mean(rplus)               # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Running code in Python ==&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3199</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3199"/>
				<updated>2013-10-15T10:43:21Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* Using Python Packages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The following assumes use of Python 3 (version 3 of Python) as opposed to Python 2, since no more major releases are planned for version 2, version 3 is expected to be the future of Python. The two versions of Python, although similar, are not compatible in a forwards or backwards direction&amp;lt;ref&amp;gt;Although Python 2 and 3 are not totally compatible, Python 2.7 is close to Python 3. If you have to use Python 2, it is recommended using version 2.7, writing code as close to Python 3 as possible, and using tools like &amp;#039;&amp;#039;2to3&amp;#039;&amp;#039; to port to Python 3. Alternatively there is a Python compatibility packages called &amp;#039;&amp;#039;six&amp;#039;&amp;#039;.&amp;lt;/ref&amp;gt;, and some legacy code exists only as Python 2. Some differences between the two versions are discussed in the footnotes.&lt;br /&gt;
&lt;br /&gt;
= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list/range to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python 3. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. The advantage of a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; object over a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; is that the sequence of values are not stored in memory with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;. &amp;lt;ref&amp;gt;In Python 3 the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object. However the Python 2 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a list, i.e. stores every integer value required in memory which is very inefficient if simply looping through a long sequence of integers in a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for&amp;lt;/source&amp;gt; loop. Python 2 has &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;xrange&amp;lt;/source&amp;gt; that behaves like the Python 3 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;.&amp;lt;/ref&amp;gt;. For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists and ranges are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm. Remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;. Namespaces are useful because they allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a function called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt;, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespaces within the function call, e.g. to use &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; at the Python command prompt to generate 5 random numbers&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, rather than assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;does not&amp;#039;&amp;#039;&amp;#039; copy values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; to a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; does copy all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s array (and list) slices work in subtly different ways to MATLAB&amp;#039;s too. For example, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A[m:n]&amp;lt;/source&amp;gt; returns all values from the element with the index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;m&amp;lt;/source&amp;gt; to the element with index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;n-1&amp;lt;/source&amp;gt;, and because the first element has index 0, we receive the (m+1)&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; to n&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; values, e.g. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[0:10]&lt;br /&gt;
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[4:6]&lt;br /&gt;
[5, 6]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Compare this to MATLAB&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
r =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(1:10)&lt;br /&gt;
ans =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(4:6)&lt;br /&gt;
ans =&lt;br /&gt;
     4     5     6&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on whole arrays are much faster than the equivalent code using loops, as they allow optimal use of the processor. Such code optimisation is often called vectorisation. In addition code using vector and matrix operations avoids loops and is often shorter and easier to read.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, as this is much quicker than growing arrays on-the-fly. In this example at the Python prompt we preallocate two arrays with 10,000 elements each, the first array is to contain integers and the second to contain double precision floating point numbers.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced example ===&lt;br /&gt;
We now look at the Python equivalent of the [[Program_Flow_and_Logicals#Relevant_example|Relevant example on the MATLAB page]], which assumed we have &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; returns in a vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The naive algorithm using loops in Python is as follows.&lt;br /&gt;
# Find the length of the NumPy array holding  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=numpy.size(r)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus=0; Tzero=0; Tminus=0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Preallocate NumPy arrays &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=numpy.zeros(T)&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=numpy.zeros(T)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe)&lt;br /&gt;
# Set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;i=0&amp;lt;/source&amp;gt; &lt;br /&gt;
# Check whether &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; is greater, smaller or equal to 0&lt;br /&gt;
#* If &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;gt;0&amp;lt;/source&amp;gt;, set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus[Tplus]=r[i]&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;0&amp;lt;/source&amp;gt; set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus[Tminus]=r[i]&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Repeat 5 for &amp;lt;math&amp;gt;i=1,\ldots,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=rplus[0:Tplus].copy()&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=rminus[0:Tminus].copy()&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=numpy,size(r)&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0&lt;br /&gt;
rplus=numpy.zeros(T);rminus=numpy.zeros(T)&lt;br /&gt;
for i in range(T):&lt;br /&gt;
   if r[i]&amp;gt;0:&lt;br /&gt;
      rplus[Tplus]=r[i]   #Store positive return in array rplus&lt;br /&gt;
      Tplus+=1            #Increase Tplus by one if return is positive&lt;br /&gt;
   elif r[i]&amp;lt;0:&lt;br /&gt;
      rminus[Tminus]=r[i] #Store negative return in array rminus&lt;br /&gt;
      Tminus+=1           #Increase Tminus by one if return is negative&lt;br /&gt;
   else:&lt;br /&gt;
      Tzero+=1            #Increase Tzero by one if return is zero&lt;br /&gt;
rplus=rplus[0:Tplus].copy()        #Remove zeros from rplus&lt;br /&gt;
rminus=rminus[1:Tminus].copy()     #Remove zeros from rminus&lt;br /&gt;
meanplus=mean(rplus)               # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Running code in Python ==&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3198</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3198"/>
				<updated>2013-10-15T10:36:38Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* while */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The following assumes use of Python 3 (version 3 of Python) as opposed to Python 2, since no more major releases are planned for version 2, version 3 is expected to be the future of Python. The two versions of Python, although similar, are not compatible in a forwards or backwards direction&amp;lt;ref&amp;gt;Although Python 2 and 3 are not totally compatible, Python 2.7 is close to Python 3. If you have to use Python 2, it is recommended using version 2.7, writing code as close to Python 3 as possible, and using tools like &amp;#039;&amp;#039;2to3&amp;#039;&amp;#039; to port to Python 3. Alternatively there is a Python compatibility packages called &amp;#039;&amp;#039;six&amp;#039;&amp;#039;.&amp;lt;/ref&amp;gt;, and some legacy code exists only as Python 2. Some differences between the two versions are discussed in the footnotes.&lt;br /&gt;
&lt;br /&gt;
= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list/range to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python 3. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. The advantage of a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; object over a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; is that the sequence of values are not stored in memory with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;. &amp;lt;ref&amp;gt;In Python 3 the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object. However the Python 2 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a list, i.e. stores every integer value required in memory which is very inefficient if simply looping through a long sequence of integers in a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for&amp;lt;/source&amp;gt; loop. Python 2 has &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;xrange&amp;lt;/source&amp;gt; that behaves like the Python 3 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;.&amp;lt;/ref&amp;gt;. For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists and ranges are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm. Remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance.&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org/ NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package to must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;, and namespaces allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a rand function, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespace within the function call, e.g. to use at the Python command prompt&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, rather than assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;does not&amp;#039;&amp;#039;&amp;#039; copy values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; to a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; does copy all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s array (and list) slices work in subtly different ways to MATLAB&amp;#039;s too. For example, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A[m:n]&amp;lt;/source&amp;gt; returns all values from the element with the index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;m&amp;lt;/source&amp;gt; to the element with index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;n-1&amp;lt;/source&amp;gt;, and because the first element has index 0, we receive the (m+1)&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; to n&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; values, e.g. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[0:10]&lt;br /&gt;
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[4:6]&lt;br /&gt;
[5, 6]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Compare this to MATLAB&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
r =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(1:10)&lt;br /&gt;
ans =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(4:6)&lt;br /&gt;
ans =&lt;br /&gt;
     4     5     6&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on whole arrays are much faster than the equivalent code using loops, as they allow optimal use of the processor. Such code optimisation is often called vectorisation. In addition code using vector and matrix operations avoids loops and is often shorter and easier to read.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, as this is much quicker than growing arrays on-the-fly. In this example at the Python prompt we preallocate two arrays with 10,000 elements each, the first array is to contain integers and the second to contain double precision floating point numbers.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced example ===&lt;br /&gt;
We now look at the Python equivalent of the [[Program_Flow_and_Logicals#Relevant_example|Relevant example on the MATLAB page]], which assumed we have &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; returns in a vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The naive algorithm using loops in Python is as follows.&lt;br /&gt;
# Find the length of the NumPy array holding  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=numpy.size(r)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus=0; Tzero=0; Tminus=0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Preallocate NumPy arrays &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=numpy.zeros(T)&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=numpy.zeros(T)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe)&lt;br /&gt;
# Set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;i=0&amp;lt;/source&amp;gt; &lt;br /&gt;
# Check whether &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; is greater, smaller or equal to 0&lt;br /&gt;
#* If &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;gt;0&amp;lt;/source&amp;gt;, set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus[Tplus]=r[i]&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;0&amp;lt;/source&amp;gt; set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus[Tminus]=r[i]&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Repeat 5 for &amp;lt;math&amp;gt;i=1,\ldots,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=rplus[0:Tplus].copy()&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=rminus[0:Tminus].copy()&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=numpy,size(r)&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0&lt;br /&gt;
rplus=numpy.zeros(T);rminus=numpy.zeros(T)&lt;br /&gt;
for i in range(T):&lt;br /&gt;
   if r[i]&amp;gt;0:&lt;br /&gt;
      rplus[Tplus]=r[i]   #Store positive return in array rplus&lt;br /&gt;
      Tplus+=1            #Increase Tplus by one if return is positive&lt;br /&gt;
   elif r[i]&amp;lt;0:&lt;br /&gt;
      rminus[Tminus]=r[i] #Store negative return in array rminus&lt;br /&gt;
      Tminus+=1           #Increase Tminus by one if return is negative&lt;br /&gt;
   else:&lt;br /&gt;
      Tzero+=1            #Increase Tzero by one if return is zero&lt;br /&gt;
rplus=rplus[0:Tplus].copy()        #Remove zeros from rplus&lt;br /&gt;
rminus=rminus[1:Tminus].copy()     #Remove zeros from rminus&lt;br /&gt;
meanplus=mean(rplus)               # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Running code in Python ==&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3197</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3197"/>
				<updated>2013-10-15T10:35:24Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* for  */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The following assumes use of Python 3 (version 3 of Python) as opposed to Python 2, since no more major releases are planned for version 2, version 3 is expected to be the future of Python. The two versions of Python, although similar, are not compatible in a forwards or backwards direction&amp;lt;ref&amp;gt;Although Python 2 and 3 are not totally compatible, Python 2.7 is close to Python 3. If you have to use Python 2, it is recommended using version 2.7, writing code as close to Python 3 as possible, and using tools like &amp;#039;&amp;#039;2to3&amp;#039;&amp;#039; to port to Python 3. Alternatively there is a Python compatibility packages called &amp;#039;&amp;#039;six&amp;#039;&amp;#039;.&amp;lt;/ref&amp;gt;, and some legacy code exists only as Python 2. Some differences between the two versions are discussed in the footnotes.&lt;br /&gt;
&lt;br /&gt;
= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list/range to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python 3. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. The advantage of a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; object over a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; is that the sequence of values are not stored in memory with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;. &amp;lt;ref&amp;gt;In Python 3 the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object. However the Python 2 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a list, i.e. stores every integer value required in memory which is very inefficient if simply looping through a long sequence of integers in a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for&amp;lt;/source&amp;gt; loop. Python 2 has &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;xrange&amp;lt;/source&amp;gt; that behaves like the Python 3 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;.&amp;lt;/ref&amp;gt;. For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists and ranges are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm (remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance).&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org/ NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package to must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;, and namespaces allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a rand function, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespace within the function call, e.g. to use at the Python command prompt&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, rather than assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;does not&amp;#039;&amp;#039;&amp;#039; copy values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; to a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; does copy all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s array (and list) slices work in subtly different ways to MATLAB&amp;#039;s too. For example, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A[m:n]&amp;lt;/source&amp;gt; returns all values from the element with the index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;m&amp;lt;/source&amp;gt; to the element with index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;n-1&amp;lt;/source&amp;gt;, and because the first element has index 0, we receive the (m+1)&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; to n&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; values, e.g. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[0:10]&lt;br /&gt;
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[4:6]&lt;br /&gt;
[5, 6]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Compare this to MATLAB&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
r =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(1:10)&lt;br /&gt;
ans =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(4:6)&lt;br /&gt;
ans =&lt;br /&gt;
     4     5     6&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on whole arrays are much faster than the equivalent code using loops, as they allow optimal use of the processor. Such code optimisation is often called vectorisation. In addition code using vector and matrix operations avoids loops and is often shorter and easier to read.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, as this is much quicker than growing arrays on-the-fly. In this example at the Python prompt we preallocate two arrays with 10,000 elements each, the first array is to contain integers and the second to contain double precision floating point numbers.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced example ===&lt;br /&gt;
We now look at the Python equivalent of the [[Program_Flow_and_Logicals#Relevant_example|Relevant example on the MATLAB page]], which assumed we have &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; returns in a vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The naive algorithm using loops in Python is as follows.&lt;br /&gt;
# Find the length of the NumPy array holding  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=numpy.size(r)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus=0; Tzero=0; Tminus=0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Preallocate NumPy arrays &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=numpy.zeros(T)&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=numpy.zeros(T)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe)&lt;br /&gt;
# Set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;i=0&amp;lt;/source&amp;gt; &lt;br /&gt;
# Check whether &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; is greater, smaller or equal to 0&lt;br /&gt;
#* If &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;gt;0&amp;lt;/source&amp;gt;, set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus[Tplus]=r[i]&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;0&amp;lt;/source&amp;gt; set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus[Tminus]=r[i]&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Repeat 5 for &amp;lt;math&amp;gt;i=1,\ldots,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=rplus[0:Tplus].copy()&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=rminus[0:Tminus].copy()&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=numpy,size(r)&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0&lt;br /&gt;
rplus=numpy.zeros(T);rminus=numpy.zeros(T)&lt;br /&gt;
for i in range(T):&lt;br /&gt;
   if r[i]&amp;gt;0:&lt;br /&gt;
      rplus[Tplus]=r[i]   #Store positive return in array rplus&lt;br /&gt;
      Tplus+=1            #Increase Tplus by one if return is positive&lt;br /&gt;
   elif r[i]&amp;lt;0:&lt;br /&gt;
      rminus[Tminus]=r[i] #Store negative return in array rminus&lt;br /&gt;
      Tminus+=1           #Increase Tminus by one if return is negative&lt;br /&gt;
   else:&lt;br /&gt;
      Tzero+=1            #Increase Tzero by one if return is zero&lt;br /&gt;
rplus=rplus[0:Tplus].copy()        #Remove zeros from rplus&lt;br /&gt;
rminus=rminus[1:Tminus].copy()     #Remove zeros from rminus&lt;br /&gt;
meanplus=mean(rplus)               # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Running code in Python ==&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3196</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3196"/>
				<updated>2013-10-15T10:32:01Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The following assumes use of Python 3 (version 3 of Python) as opposed to Python 2, since no more major releases are planned for version 2, version 3 is expected to be the future of Python. The two versions of Python, although similar, are not compatible in a forwards or backwards direction&amp;lt;ref&amp;gt;Although Python 2 and 3 are not totally compatible, Python 2.7 is close to Python 3. If you have to use Python 2, it is recommended using version 2.7, writing code as close to Python 3 as possible, and using tools like &amp;#039;&amp;#039;2to3&amp;#039;&amp;#039; to port to Python 3. Alternatively there is a Python compatibility packages called &amp;#039;&amp;#039;six&amp;#039;&amp;#039;.&amp;lt;/ref&amp;gt;, and some legacy code exists only as Python 2. Some differences between the two versions are discussed in the footnotes.&lt;br /&gt;
&lt;br /&gt;
= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list/range to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python 3. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. The advantage of a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; object over a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; is that the sequence of values are not stored in memory with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;. &amp;lt;ref&amp;gt;In Python 3 the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object. However the Python 2 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a list, i.e. stores every integer value required in memory which is very inefficient if simply looping through a long sequence of integers in a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for&amp;lt;/source&amp;gt; loop. Python 2 has &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;xrange&amp;lt;/source&amp;gt; that behaves like the Python 3 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;.&amp;lt;/ref&amp;gt;. For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists and ranges are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
As Python 3 is expected to be the future of Python, we recommend using this version unless you have a good reason not to.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm (remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance).&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org/ NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package to must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;, and namespaces allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a rand function, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespace within the function call, e.g. to use at the Python command prompt&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, rather than assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;does not&amp;#039;&amp;#039;&amp;#039; copy values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; to a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; does copy all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s array (and list) slices work in subtly different ways to MATLAB&amp;#039;s too. For example, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A[m:n]&amp;lt;/source&amp;gt; returns all values from the element with the index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;m&amp;lt;/source&amp;gt; to the element with index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;n-1&amp;lt;/source&amp;gt;, and because the first element has index 0, we receive the (m+1)&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; to n&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; values, e.g. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[0:10]&lt;br /&gt;
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[4:6]&lt;br /&gt;
[5, 6]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Compare this to MATLAB&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
r =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(1:10)&lt;br /&gt;
ans =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(4:6)&lt;br /&gt;
ans =&lt;br /&gt;
     4     5     6&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on whole arrays are much faster than the equivalent code using loops, as they allow optimal use of the processor. Such code optimisation is often called vectorisation. In addition code using vector and matrix operations avoids loops and is often shorter and easier to read.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, as this is much quicker than growing arrays on-the-fly. In this example at the Python prompt we preallocate two arrays with 10,000 elements each, the first array is to contain integers and the second to contain double precision floating point numbers.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced example ===&lt;br /&gt;
We now look at the Python equivalent of the [[Program_Flow_and_Logicals#Relevant_example|Relevant example on the MATLAB page]], which assumed we have &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; returns in a vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The naive algorithm using loops in Python is as follows.&lt;br /&gt;
# Find the length of the NumPy array holding  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=numpy.size(r)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus=0; Tzero=0; Tminus=0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Preallocate NumPy arrays &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=numpy.zeros(T)&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=numpy.zeros(T)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe)&lt;br /&gt;
# Set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;i=0&amp;lt;/source&amp;gt; &lt;br /&gt;
# Check whether &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; is greater, smaller or equal to 0&lt;br /&gt;
#* If &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;gt;0&amp;lt;/source&amp;gt;, set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus[Tplus]=r[i]&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;0&amp;lt;/source&amp;gt; set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus[Tminus]=r[i]&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Repeat 5 for &amp;lt;math&amp;gt;i=1,\ldots,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=rplus[0:Tplus].copy()&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=rminus[0:Tminus].copy()&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=numpy,size(r)&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0&lt;br /&gt;
rplus=numpy.zeros(T);rminus=numpy.zeros(T)&lt;br /&gt;
for i in range(T):&lt;br /&gt;
   if r[i]&amp;gt;0:&lt;br /&gt;
      rplus[Tplus]=r[i]   #Store positive return in array rplus&lt;br /&gt;
      Tplus+=1            #Increase Tplus by one if return is positive&lt;br /&gt;
   elif r[i]&amp;lt;0:&lt;br /&gt;
      rminus[Tminus]=r[i] #Store negative return in array rminus&lt;br /&gt;
      Tminus+=1           #Increase Tminus by one if return is negative&lt;br /&gt;
   else:&lt;br /&gt;
      Tzero+=1            #Increase Tzero by one if return is zero&lt;br /&gt;
rplus=rplus[0:Tplus].copy()        #Remove zeros from rplus&lt;br /&gt;
rminus=rminus[1:Tminus].copy()     #Remove zeros from rminus&lt;br /&gt;
meanplus=mean(rplus)               # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Running code in Python ==&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3195</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3195"/>
				<updated>2013-10-15T10:06:53Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The following assumes use of Python 3 (version 3 of Python) as opposed to Python 2, since version 3 is expected to be the future of Python. The two versions of Python, although similar, are not compatible in a forwards or backwards direction, and some legacy code exists only as Python 2. Some differences between the two versions are discussed in the footnotes.&lt;br /&gt;
&lt;br /&gt;
= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list/range to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python 3. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. The advantage of a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; object over a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; is that the sequence of values are not stored in memory with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;. &amp;lt;ref&amp;gt;In Python 3 the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object. However the Python 2 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a list, i.e. stores every integer value required in memory which is very inefficient if simply looping through a long sequence of integers in a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for&amp;lt;/source&amp;gt; loop. Python 2 has &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;xrange&amp;lt;/source&amp;gt; that behaves like the Python 3 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;.&amp;lt;/ref&amp;gt;. For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists and ranges are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
As Python 3 is expected to be the future of Python, we recommend using this version unless you have a good reason not to.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm (remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance).&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org/ NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package to must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;, and namespaces allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a rand function, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespace within the function call, e.g. to use at the Python command prompt&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, rather than assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;does not&amp;#039;&amp;#039;&amp;#039; copy values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; to a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; does copy all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s array (and list) slices work in subtly different ways to MATLAB&amp;#039;s too. For example, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A[m:n]&amp;lt;/source&amp;gt; returns all values from the element with the index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;m&amp;lt;/source&amp;gt; to the element with index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;n-1&amp;lt;/source&amp;gt;, and because the first element has index 0, we receive the (m+1)&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; to n&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; values, e.g. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[0:10]&lt;br /&gt;
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[4:6]&lt;br /&gt;
[5, 6]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Compare this to MATLAB&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
r =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(1:10)&lt;br /&gt;
ans =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(4:6)&lt;br /&gt;
ans =&lt;br /&gt;
     4     5     6&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on whole arrays are much faster than the equivalent code using loops, as they allow optimal use of the processor. Such code optimisation is often called vectorisation. In addition code using vector and matrix operations avoids loops and is often shorter and easier to read.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, as this is much quicker than growing arrays on-the-fly. In this example at the Python prompt we preallocate two arrays with 10,000 elements each, the first array is to contain integers and the second to contain double precision floating point numbers.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced example ===&lt;br /&gt;
We now look at the Python equivalent of the [[Program_Flow_and_Logicals#Relevant_example|Relevant example on the MATLAB page]], which assumed we have &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; returns in a vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The naive algorithm using loops in Python is as follows.&lt;br /&gt;
# Find the length of the NumPy array holding  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=numpy.size(r)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus=0; Tzero=0; Tminus=0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Preallocate NumPy arrays &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=numpy.zeros(T)&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=numpy.zeros(T)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe)&lt;br /&gt;
# Set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;i=0&amp;lt;/source&amp;gt; &lt;br /&gt;
# Check whether &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; is greater, smaller or equal to 0&lt;br /&gt;
#* If &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;gt;0&amp;lt;/source&amp;gt;, set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus[Tplus]=r[i]&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;0&amp;lt;/source&amp;gt; set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus[Tminus]=r[i]&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Repeat 5 for &amp;lt;math&amp;gt;i=1,\ldots,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=rplus[0:Tplus].copy()&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=rminus[0:Tminus].copy()&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=numpy,size(r)&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0&lt;br /&gt;
rplus=numpy.zeros(T);rminus=numpy.zeros(T)&lt;br /&gt;
for i in range(T):&lt;br /&gt;
   if r[i]&amp;gt;0:&lt;br /&gt;
      rplus[Tplus]=r[i]   #Store positive return in array rplus&lt;br /&gt;
      Tplus+=1            #Increase Tplus by one if return is positive&lt;br /&gt;
   elif r[i]&amp;lt;0:&lt;br /&gt;
      rminus[Tminus]=r[i] #Store negative return in array rminus&lt;br /&gt;
      Tminus+=1           #Increase Tminus by one if return is negative&lt;br /&gt;
   else:&lt;br /&gt;
      Tzero+=1            #Increase Tzero by one if return is zero&lt;br /&gt;
rplus=rplus[0:Tplus].copy()        #Remove zeros from rplus&lt;br /&gt;
rminus=rminus[1:Tminus].copy()     #Remove zeros from rminus&lt;br /&gt;
meanplus=mean(rplus)               # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Running code in Python ==&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3194</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3194"/>
				<updated>2013-10-15T10:06:07Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* Preliminaries */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The following assumes use of Python 3 (version 3 of Python) as opposed to Python 2, since version 3 is expected to be the future of Python. The two versions of Python, although similar, are not compatible in a forwards or backwards direction, and some legacy code only exists in Python 2. Some differences between the two versions are discussed in the footnotes.&lt;br /&gt;
&lt;br /&gt;
= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list/range to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python 3. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. The advantage of a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; object over a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; is that the sequence of values are not stored in memory with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;. &amp;lt;ref&amp;gt;In Python 3 the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object. However the Python 2 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a list, i.e. stores every integer value required in memory which is very inefficient if simply looping through a long sequence of integers in a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for&amp;lt;/source&amp;gt; loop. Python 2 has &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;xrange&amp;lt;/source&amp;gt; that behaves like the Python 3 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;.&amp;lt;/ref&amp;gt;. For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists and ranges are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
As Python 3 is expected to be the future of Python, we recommend using this version unless you have a good reason not to.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm (remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance).&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org/ NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package to must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;, and namespaces allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a rand function, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespace within the function call, e.g. to use at the Python command prompt&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, rather than assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;does not&amp;#039;&amp;#039;&amp;#039; copy values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; to a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; does copy all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s array (and list) slices work in subtly different ways to MATLAB&amp;#039;s too. For example, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A[m:n]&amp;lt;/source&amp;gt; returns all values from the element with the index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;m&amp;lt;/source&amp;gt; to the element with index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;n-1&amp;lt;/source&amp;gt;, and because the first element has index 0, we receive the (m+1)&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; to n&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; values, e.g. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[0:10]&lt;br /&gt;
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[4:6]&lt;br /&gt;
[5, 6]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Compare this to MATLAB&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
r =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(1:10)&lt;br /&gt;
ans =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(4:6)&lt;br /&gt;
ans =&lt;br /&gt;
     4     5     6&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on whole arrays are much faster than the equivalent code using loops, as they allow optimal use of the processor. Such code optimisation is often called vectorisation. In addition code using vector and matrix operations avoids loops and is often shorter and easier to read.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, as this is much quicker than growing arrays on-the-fly. In this example at the Python prompt we preallocate two arrays with 10,000 elements each, the first array is to contain integers and the second to contain double precision floating point numbers.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced example ===&lt;br /&gt;
We now look at the Python equivalent of the [[Program_Flow_and_Logicals#Relevant_example|Relevant example on the MATLAB page]], which assumed we have &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; returns in a vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The naive algorithm using loops in Python is as follows.&lt;br /&gt;
# Find the length of the NumPy array holding  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=numpy.size(r)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus=0; Tzero=0; Tminus=0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Preallocate NumPy arrays &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=numpy.zeros(T)&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=numpy.zeros(T)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe)&lt;br /&gt;
# Set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;i=0&amp;lt;/source&amp;gt; &lt;br /&gt;
# Check whether &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; is greater, smaller or equal to 0&lt;br /&gt;
#* If &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;gt;0&amp;lt;/source&amp;gt;, set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus[Tplus]=r[i]&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;0&amp;lt;/source&amp;gt; set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus[Tminus]=r[i]&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Repeat 5 for &amp;lt;math&amp;gt;i=1,\ldots,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=rplus[0:Tplus].copy()&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=rminus[0:Tminus].copy()&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=numpy,size(r)&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0&lt;br /&gt;
rplus=numpy.zeros(T);rminus=numpy.zeros(T)&lt;br /&gt;
for i in range(T):&lt;br /&gt;
   if r[i]&amp;gt;0:&lt;br /&gt;
      rplus[Tplus]=r[i]   #Store positive return in array rplus&lt;br /&gt;
      Tplus+=1            #Increase Tplus by one if return is positive&lt;br /&gt;
   elif r[i]&amp;lt;0:&lt;br /&gt;
      rminus[Tminus]=r[i] #Store negative return in array rminus&lt;br /&gt;
      Tminus+=1           #Increase Tminus by one if return is negative&lt;br /&gt;
   else:&lt;br /&gt;
      Tzero+=1            #Increase Tzero by one if return is zero&lt;br /&gt;
rplus=rplus[0:Tplus].copy()        #Remove zeros from rplus&lt;br /&gt;
rminus=rminus[1:Tminus].copy()     #Remove zeros from rminus&lt;br /&gt;
meanplus=mean(rplus)               # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Running code in Python ==&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3193</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3193"/>
				<updated>2013-10-15T10:01:21Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* Preliminaries */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The following assumes use of Python 3 (version 3 of Python) as opposed to Python 2, since version 3 is expected to be the future of Python. The two versions of Python, although similar, are not compatible in a forwards or backwards direction, and some legacy code only exists in Python 2. Some differences between the two versions are discussed in the footnotes.&lt;br /&gt;
&lt;br /&gt;
= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list/range to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python 3. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. The advantage of a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; object over a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; is that the sequence of values are not stored in memory with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;. &amp;lt;ref&amp;gt;In Python 3 the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object. However the Python 2 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a list, i.e. stores every integer value required in memory, which is very inefficient if simply looping through a long sequence of integers in a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for&amp;lt;/source&amp;gt; loop. Python 2 has &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;xrange&amp;lt;/source&amp;gt; that behaves like the Python 3 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;.&amp;lt;/ref&amp;gt;. For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
As Python 3 is expected to be the future of Python, we recommend using this version unless you have a good reason not to.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm (remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance).&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org/ NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package to must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;, and namespaces allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a rand function, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespace within the function call, e.g. to use at the Python command prompt&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, rather than assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;does not&amp;#039;&amp;#039;&amp;#039; copy values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; to a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; does copy all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s array (and list) slices work in subtly different ways to MATLAB&amp;#039;s too. For example, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A[m:n]&amp;lt;/source&amp;gt; returns all values from the element with the index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;m&amp;lt;/source&amp;gt; to the element with index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;n-1&amp;lt;/source&amp;gt;, and because the first element has index 0, we receive the (m+1)&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; to n&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; values, e.g. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[0:10]&lt;br /&gt;
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[4:6]&lt;br /&gt;
[5, 6]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Compare this to MATLAB&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
r =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(1:10)&lt;br /&gt;
ans =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(4:6)&lt;br /&gt;
ans =&lt;br /&gt;
     4     5     6&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on whole arrays are much faster than the equivalent code using loops, as they allow optimal use of the processor. Such code optimisation is often called vectorisation. In addition code using vector and matrix operations avoids loops and is often shorter and easier to read.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, as this is much quicker than growing arrays on-the-fly. In this example at the Python prompt we preallocate two arrays with 10,000 elements each, the first array is to contain integers and the second to contain double precision floating point numbers.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced example ===&lt;br /&gt;
We now look at the Python equivalent of the [[Program_Flow_and_Logicals#Relevant_example|Relevant example on the MATLAB page]], which assumed we have &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; returns in a vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The naive algorithm using loops in Python is as follows.&lt;br /&gt;
# Find the length of the NumPy array holding  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=numpy.size(r)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus=0; Tzero=0; Tminus=0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Preallocate NumPy arrays &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=numpy.zeros(T)&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=numpy.zeros(T)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe)&lt;br /&gt;
# Set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;i=0&amp;lt;/source&amp;gt; &lt;br /&gt;
# Check whether &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; is greater, smaller or equal to 0&lt;br /&gt;
#* If &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;gt;0&amp;lt;/source&amp;gt;, set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus[Tplus]=r[i]&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;0&amp;lt;/source&amp;gt; set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus[Tminus]=r[i]&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Repeat 5 for &amp;lt;math&amp;gt;i=1,\ldots,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=rplus[0:Tplus].copy()&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=rminus[0:Tminus].copy()&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=numpy,size(r)&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0&lt;br /&gt;
rplus=numpy.zeros(T);rminus=numpy.zeros(T)&lt;br /&gt;
for i in range(T):&lt;br /&gt;
   if r[i]&amp;gt;0:&lt;br /&gt;
      rplus[Tplus]=r[i]   #Store positive return in array rplus&lt;br /&gt;
      Tplus+=1            #Increase Tplus by one if return is positive&lt;br /&gt;
   elif r[i]&amp;lt;0:&lt;br /&gt;
      rminus[Tminus]=r[i] #Store negative return in array rminus&lt;br /&gt;
      Tminus+=1           #Increase Tminus by one if return is negative&lt;br /&gt;
   else:&lt;br /&gt;
      Tzero+=1            #Increase Tzero by one if return is zero&lt;br /&gt;
rplus=rplus[0:Tplus].copy()        #Remove zeros from rplus&lt;br /&gt;
rminus=rminus[1:Tminus].copy()     #Remove zeros from rminus&lt;br /&gt;
meanplus=mean(rplus)               # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Running code in Python ==&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3192</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3192"/>
				<updated>2013-10-15T09:59:28Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* Preliminaries */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The following assumes use of Python 3 (version 3 of Python) as opposed to Python 2, since version 3 is expected to be the future of Python. The two versions of Python, although similar, are not compatible in a forwards or backwards direction, and some legacy code only exists in Python 2. Some differences between the two versions are discussed in the footnotes.&lt;br /&gt;
&lt;br /&gt;
= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list/range to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python 3. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. The advantage of a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; object over a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; is that the sequence of values are not stored in memory with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;. &amp;lt;ref&amp;gt;In Python 3 the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object, however Python 2 the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a list, i.e. stores every integer value required in memory, this is obviously very inefficient if simply looping through a long sequence of integers in a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for&amp;lt;/source&amp;gt; loop. Python 2 has &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;xrange&amp;lt;/source&amp;gt; that behaves like the Python 3 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;.&amp;lt;/ref&amp;gt;. For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
As Python 3 is expected to be the future of Python, we recommend using this version unless you have a good reason not to.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm (remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance).&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org/ NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package to must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;, and namespaces allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a rand function, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespace within the function call, e.g. to use at the Python command prompt&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, rather than assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;does not&amp;#039;&amp;#039;&amp;#039; copy values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; to a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; does copy all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s array (and list) slices work in subtly different ways to MATLAB&amp;#039;s too. For example, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A[m:n]&amp;lt;/source&amp;gt; returns all values from the element with the index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;m&amp;lt;/source&amp;gt; to the element with index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;n-1&amp;lt;/source&amp;gt;, and because the first element has index 0, we receive the (m+1)&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; to n&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; values, e.g. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[0:10]&lt;br /&gt;
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[4:6]&lt;br /&gt;
[5, 6]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Compare this to MATLAB&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
r =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(1:10)&lt;br /&gt;
ans =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(4:6)&lt;br /&gt;
ans =&lt;br /&gt;
     4     5     6&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on whole arrays are much faster than the equivalent code using loops, as they allow optimal use of the processor. Such code optimisation is often called vectorisation. In addition code using vector and matrix operations avoids loops and is often shorter and easier to read.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, as this is much quicker than growing arrays on-the-fly. In this example at the Python prompt we preallocate two arrays with 10,000 elements each, the first array is to contain integers and the second to contain double precision floating point numbers.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced example ===&lt;br /&gt;
We now look at the Python equivalent of the [[Program_Flow_and_Logicals#Relevant_example|Relevant example on the MATLAB page]], which assumed we have &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; returns in a vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The naive algorithm using loops in Python is as follows.&lt;br /&gt;
# Find the length of the NumPy array holding  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=numpy.size(r)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus=0; Tzero=0; Tminus=0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Preallocate NumPy arrays &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=numpy.zeros(T)&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=numpy.zeros(T)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe)&lt;br /&gt;
# Set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;i=0&amp;lt;/source&amp;gt; &lt;br /&gt;
# Check whether &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; is greater, smaller or equal to 0&lt;br /&gt;
#* If &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;gt;0&amp;lt;/source&amp;gt;, set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus[Tplus]=r[i]&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;0&amp;lt;/source&amp;gt; set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus[Tminus]=r[i]&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Repeat 5 for &amp;lt;math&amp;gt;i=1,\ldots,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=rplus[0:Tplus].copy()&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=rminus[0:Tminus].copy()&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=numpy,size(r)&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0&lt;br /&gt;
rplus=numpy.zeros(T);rminus=numpy.zeros(T)&lt;br /&gt;
for i in range(T):&lt;br /&gt;
   if r[i]&amp;gt;0:&lt;br /&gt;
      rplus[Tplus]=r[i]   #Store positive return in array rplus&lt;br /&gt;
      Tplus+=1            #Increase Tplus by one if return is positive&lt;br /&gt;
   elif r[i]&amp;lt;0:&lt;br /&gt;
      rminus[Tminus]=r[i] #Store negative return in array rminus&lt;br /&gt;
      Tminus+=1           #Increase Tminus by one if return is negative&lt;br /&gt;
   else:&lt;br /&gt;
      Tzero+=1            #Increase Tzero by one if return is zero&lt;br /&gt;
rplus=rplus[0:Tplus].copy()        #Remove zeros from rplus&lt;br /&gt;
rminus=rminus[1:Tminus].copy()     #Remove zeros from rminus&lt;br /&gt;
meanplus=mean(rplus)               # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Running code in Python ==&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3191</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3191"/>
				<updated>2013-10-15T09:57:37Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The following assumes use of Python 3 (version 3 of Python) as opposed to Python 2, since version 3 is expected to be the future of Python. The two versions of Python, although similar, are not compatible in a forwards or backwards direction, and some legacy code only exists in Python 2. Some differences between the two versions are discussed in the footnotes.&lt;br /&gt;
&lt;br /&gt;
= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list/range to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python 3. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. The advantage of a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; object over a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; is that the sequence of values are not stored in memory with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;. &amp;lt;ref&amp;gt;In Python 3 the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object, however Python 2 the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;list&amp;lt;/source&amp;gt;, i.e. storing every integer value required in memory, this is obviously very inefficient if simply looping through a long sequence of integers in a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for&amp;lt;/source&amp;gt; loop. Python 2 has &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;xrange&amp;lt;/source&amp;gt; that behaves like the Python 3 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;.&amp;lt;/ref&amp;gt;. For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
As Python 3 is expected to be the future of Python, we recommend using this version unless you have a good reason not to.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm (remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance).&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org/ NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package to must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;, and namespaces allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a rand function, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespace within the function call, e.g. to use at the Python command prompt&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, rather than assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;does not&amp;#039;&amp;#039;&amp;#039; copy values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; to a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; does copy all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s array (and list) slices work in subtly different ways to MATLAB&amp;#039;s too. For example, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A[m:n]&amp;lt;/source&amp;gt; returns all values from the element with the index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;m&amp;lt;/source&amp;gt; to the element with index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;n-1&amp;lt;/source&amp;gt;, and because the first element has index 0, we receive the (m+1)&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; to n&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; values, e.g. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[0:10]&lt;br /&gt;
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[4:6]&lt;br /&gt;
[5, 6]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Compare this to MATLAB&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
r =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(1:10)&lt;br /&gt;
ans =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(4:6)&lt;br /&gt;
ans =&lt;br /&gt;
     4     5     6&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on whole arrays are much faster than the equivalent code using loops, as they allow optimal use of the processor. Such code optimisation is often called vectorisation. In addition code using vector and matrix operations avoids loops and is often shorter and easier to read.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, as this is much quicker than growing arrays on-the-fly. In this example at the Python prompt we preallocate two arrays with 10,000 elements each, the first array is to contain integers and the second to contain double precision floating point numbers.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced example ===&lt;br /&gt;
We now look at the Python equivalent of the [[Program_Flow_and_Logicals#Relevant_example|Relevant example on the MATLAB page]], which assumed we have &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; returns in a vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The naive algorithm using loops in Python is as follows.&lt;br /&gt;
# Find the length of the NumPy array holding  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=numpy.size(r)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus=0; Tzero=0; Tminus=0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Preallocate NumPy arrays &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=numpy.zeros(T)&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=numpy.zeros(T)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe)&lt;br /&gt;
# Set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;i=0&amp;lt;/source&amp;gt; &lt;br /&gt;
# Check whether &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; is greater, smaller or equal to 0&lt;br /&gt;
#* If &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;gt;0&amp;lt;/source&amp;gt;, set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus[Tplus]=r[i]&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;0&amp;lt;/source&amp;gt; set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus[Tminus]=r[i]&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Repeat 5 for &amp;lt;math&amp;gt;i=1,\ldots,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=rplus[0:Tplus].copy()&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=rminus[0:Tminus].copy()&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=numpy,size(r)&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0&lt;br /&gt;
rplus=numpy.zeros(T);rminus=numpy.zeros(T)&lt;br /&gt;
for i in range(T):&lt;br /&gt;
   if r[i]&amp;gt;0:&lt;br /&gt;
      rplus[Tplus]=r[i]   #Store positive return in array rplus&lt;br /&gt;
      Tplus+=1            #Increase Tplus by one if return is positive&lt;br /&gt;
   elif r[i]&amp;lt;0:&lt;br /&gt;
      rminus[Tminus]=r[i] #Store negative return in array rminus&lt;br /&gt;
      Tminus+=1           #Increase Tminus by one if return is negative&lt;br /&gt;
   else:&lt;br /&gt;
      Tzero+=1            #Increase Tzero by one if return is zero&lt;br /&gt;
rplus=rplus[0:Tplus].copy()        #Remove zeros from rplus&lt;br /&gt;
rminus=rminus[1:Tminus].copy()     #Remove zeros from rminus&lt;br /&gt;
meanplus=mean(rplus)               # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Running code in Python ==&lt;br /&gt;
&lt;br /&gt;
=Footnotes=&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3190</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3190"/>
				<updated>2013-10-15T09:57:11Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The following assumes use of Python 3 (version 3 of Python) as opposed to Python 2, since version 3 is expected to be the future of Python. The two versions of Python, although similar, are not compatible in a forwards or backwards direction, and some legacy code only exists in Python 2. Some differences between the two versions are discussed in the footnotes.&lt;br /&gt;
&lt;br /&gt;
= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list/range to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python 3. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. The advantage of a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; object over a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; is that the sequence of values are not stored in memory with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;. &amp;lt;ref&amp;gt;In Python 3 the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object, however Python 2 the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;list&amp;lt;/source&amp;gt;, i.e. storing every integer value required in memory, this is obviously very inefficient if simply looping through a long sequence of integers in a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for&amp;lt;/source&amp;gt; loop. Python 2 has &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;xrange&amp;lt;/source&amp;gt; that behaves like the Python 3 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;.&amp;lt;/ref&amp;gt;. For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
As Python 3 is expected to be the future of Python, we recommend using this version unless you have a good reason not to.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm (remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance).&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org/ NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package to must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;, and namespaces allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a rand function, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespace within the function call, e.g. to use at the Python command prompt&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, rather than assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;does not&amp;#039;&amp;#039;&amp;#039; copy values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; to a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; does copy all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s array (and list) slices work in subtly different ways to MATLAB&amp;#039;s too. For example, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A[m:n]&amp;lt;/source&amp;gt; returns all values from the element with the index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;m&amp;lt;/source&amp;gt; to the element with index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;n-1&amp;lt;/source&amp;gt;, and because the first element has index 0, we receive the (m+1)&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; to n&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; values, e.g. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[0:10]&lt;br /&gt;
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[4:6]&lt;br /&gt;
[5, 6]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Compare this to MATLAB&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
r =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(1:10)&lt;br /&gt;
ans =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(4:6)&lt;br /&gt;
ans =&lt;br /&gt;
     4     5     6&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on whole arrays are much faster than the equivalent code using loops, as they allow optimal use of the processor. Such code optimisation is often called vectorisation. In addition code using vector and matrix operations avoids loops and is often shorter and easier to read.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, as this is much quicker than growing arrays on-the-fly. In this example at the Python prompt we preallocate two arrays with 10,000 elements each, the first array is to contain integers and the second to contain double precision floating point numbers.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced example ===&lt;br /&gt;
We now look at the Python equivalent of the [[Program_Flow_and_Logicals#Relevant_example|Relevant example on the MATLAB page]], which assumed we have &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; returns in a vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The naive algorithm using loops in Python is as follows.&lt;br /&gt;
# Find the length of the NumPy array holding  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=numpy.size(r)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus=0; Tzero=0; Tminus=0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Preallocate NumPy arrays &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=numpy.zeros(T)&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=numpy.zeros(T)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe)&lt;br /&gt;
# Set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;i=0&amp;lt;/source&amp;gt; &lt;br /&gt;
# Check whether &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; is greater, smaller or equal to 0&lt;br /&gt;
#* If &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;gt;0&amp;lt;/source&amp;gt;, set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus[Tplus]=r[i]&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;0&amp;lt;/source&amp;gt; set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus[Tminus]=r[i]&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Repeat 5 for &amp;lt;math&amp;gt;i=1,\ldots,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=rplus[0:Tplus].copy()&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=rminus[0:Tminus].copy()&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=numpy,size(r)&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0&lt;br /&gt;
rplus=numpy.zeros(T);rminus=numpy.zeros(T)&lt;br /&gt;
for i in range(T):&lt;br /&gt;
   if r[i]&amp;gt;0:&lt;br /&gt;
      rplus[Tplus]=r[i]   #Store positive return in array rplus&lt;br /&gt;
      Tplus+=1            #Increase Tplus by one if return is positive&lt;br /&gt;
   elif r[i]&amp;lt;0:&lt;br /&gt;
      rminus[Tminus]=r[i] #Store negative return in array rminus&lt;br /&gt;
      Tminus+=1           #Increase Tminus by one if return is negative&lt;br /&gt;
   else:&lt;br /&gt;
      Tzero+=1            #Increase Tzero by one if return is zero&lt;br /&gt;
rplus=rplus[0:Tplus].copy()        #Remove zeros from rplus&lt;br /&gt;
rminus=rminus[1:Tminus].copy()     #Remove zeros from rminus&lt;br /&gt;
meanplus=mean(rplus)               # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Running code in Python ==&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3189</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3189"/>
				<updated>2013-10-15T09:51:46Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* Preliminaries */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list/range to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python 3. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. The advantage of a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; object over a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; is that the sequence of values are not stored in memory with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;. &amp;lt;ref&amp;gt;In Python 3 the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object, however Python 2 the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;list&amp;lt;/source&amp;gt;, i.e. storing every integer value required in memory, this is obviously very inefficient if simply looping through a long sequence of integers in a &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for&amp;lt;/source&amp;gt; loop. Python 2 has &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;xrange&amp;lt;/source&amp;gt; that behaves like the Python 3 &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt;.&amp;lt;/ref&amp;gt;. For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
As Python 3 is expected to be the future of Python, we recommend using this version unless you have a good reason not to.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm (remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance).&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org/ NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package to must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;, and namespaces allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a rand function, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespace within the function call, e.g. to use at the Python command prompt&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, rather than assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;does not&amp;#039;&amp;#039;&amp;#039; copy values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; to a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; does copy all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s array (and list) slices work in subtly different ways to MATLAB&amp;#039;s too. For example, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A[m:n]&amp;lt;/source&amp;gt; returns all values from the element with the index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;m&amp;lt;/source&amp;gt; to the element with index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;n-1&amp;lt;/source&amp;gt;, and because the first element has index 0, we receive the (m+1)&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; to n&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; values, e.g. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[0:10]&lt;br /&gt;
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[4:6]&lt;br /&gt;
[5, 6]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Compare this to MATLAB&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
r =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(1:10)&lt;br /&gt;
ans =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(4:6)&lt;br /&gt;
ans =&lt;br /&gt;
     4     5     6&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on whole arrays are much faster than the equivalent code using loops, as they allow optimal use of the processor. Such code optimisation is often called vectorisation. In addition code using vector and matrix operations avoids loops and is often shorter and easier to read.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, as this is much quicker than growing arrays on-the-fly. In this example at the Python prompt we preallocate two arrays with 10,000 elements each, the first array is to contain integers and the second to contain double precision floating point numbers.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced example ===&lt;br /&gt;
We now look at the Python equivalent of the [[Program_Flow_and_Logicals#Relevant_example|Relevant example on the MATLAB page]], which assumed we have &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; returns in a vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The naive algorithm using loops in Python is as follows.&lt;br /&gt;
# Find the length of the NumPy array holding  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=numpy.size(r)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus=0; Tzero=0; Tminus=0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Preallocate NumPy arrays &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=numpy.zeros(T)&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=numpy.zeros(T)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe)&lt;br /&gt;
# Set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;i=0&amp;lt;/source&amp;gt; &lt;br /&gt;
# Check whether &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; is greater, smaller or equal to 0&lt;br /&gt;
#* If &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;gt;0&amp;lt;/source&amp;gt;, set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus[Tplus]=r[i]&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;0&amp;lt;/source&amp;gt; set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus[Tminus]=r[i]&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Repeat 5 for &amp;lt;math&amp;gt;i=1,\ldots,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=rplus[0:Tplus].copy()&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=rminus[0:Tminus].copy()&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=numpy,size(r)&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0&lt;br /&gt;
rplus=numpy.zeros(T);rminus=numpy.zeros(T)&lt;br /&gt;
for i in range(T):&lt;br /&gt;
   if r[i]&amp;gt;0:&lt;br /&gt;
      rplus[Tplus]=r[i]   #Store positive return in array rplus&lt;br /&gt;
      Tplus+=1            #Increase Tplus by one if return is positive&lt;br /&gt;
   elif r[i]&amp;lt;0:&lt;br /&gt;
      rminus[Tminus]=r[i] #Store negative return in array rminus&lt;br /&gt;
      Tminus+=1           #Increase Tminus by one if return is negative&lt;br /&gt;
   else:&lt;br /&gt;
      Tzero+=1            #Increase Tzero by one if return is zero&lt;br /&gt;
rplus=rplus[0:Tplus].copy()        #Remove zeros from rplus&lt;br /&gt;
rminus=rminus[1:Tminus].copy()     #Remove zeros from rminus&lt;br /&gt;
meanplus=mean(rplus)               # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Running code in Python ==&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3188</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3188"/>
				<updated>2013-10-14T17:47:35Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* More advanced example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value (note: in version 3 of Python the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object, however in version 2 of Python this function creates a list object). For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
As Python 3 is expected to be the future of Python, we recommend using this version unless you have a good reason not to.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm (remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance).&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org/ NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package to must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;, and namespaces allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a rand function, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespace within the function call, e.g. to use at the Python command prompt&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, rather than assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;does not&amp;#039;&amp;#039;&amp;#039; copy values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; to a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; does copy all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s array (and list) slices work in subtly different ways to MATLAB&amp;#039;s too. For example, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A[m:n]&amp;lt;/source&amp;gt; returns all values from the element with the index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;m&amp;lt;/source&amp;gt; to the element with index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;n-1&amp;lt;/source&amp;gt;, and because the first element has index 0, we receive the (m+1)&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; to n&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; values, e.g. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[0:10]&lt;br /&gt;
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[4:6]&lt;br /&gt;
[5, 6]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Compare this to MATLAB&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
r =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(1:10)&lt;br /&gt;
ans =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(4:6)&lt;br /&gt;
ans =&lt;br /&gt;
     4     5     6&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on whole arrays are much faster than the equivalent code using loops, as they allow optimal use of the processor. Such code optimisation is often called vectorisation. In addition code using vector and matrix operations avoids loops and is often shorter and easier to read.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, as this is much quicker than growing arrays on-the-fly. In this example at the Python prompt we preallocate two arrays with 10,000 elements each, the first array is to contain integers and the second to contain double precision floating point numbers.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced example ===&lt;br /&gt;
We now look at the Python equivalent of the [[Program_Flow_and_Logicals#Relevant_example|Relevant example on the MATLAB page]], which assumed we have &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; returns in a vector &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The naive algorithm using loops in Python is as follows.&lt;br /&gt;
# Find the length of the NumPy array holding  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=numpy.size(r)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus=0; Tzero=0; Tminus=0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Preallocate NumPy arrays &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=numpy.zeros(T)&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=numpy.zeros(T)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe)&lt;br /&gt;
# Set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;i=0&amp;lt;/source&amp;gt; &lt;br /&gt;
# Check whether &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; is greater, smaller or equal to 0&lt;br /&gt;
#* If &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;gt;0&amp;lt;/source&amp;gt;, set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus[Tplus]=r[i]&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;0&amp;lt;/source&amp;gt; set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus[Tminus]=r[i]&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Repeat 5 for &amp;lt;math&amp;gt;i=1,\ldots,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=rplus[0:Tplus].copy()&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=rminus[0:Tminus].copy()&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=numpy,size(r)&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0&lt;br /&gt;
rplus=numpy.zeros(T);rminus=numpy.zeros(T)&lt;br /&gt;
for i in range(T):&lt;br /&gt;
   if r[i]&amp;gt;0:&lt;br /&gt;
      rplus[Tplus]=r[i]   #Store positive return in array rplus&lt;br /&gt;
      Tplus+=1            #Increase Tplus by one if return is positive&lt;br /&gt;
   elif r[i]&amp;lt;0:&lt;br /&gt;
      rminus[Tminus]=r[i] #Store negative return in array rminus&lt;br /&gt;
      Tminus+=1           #Increase Tminus by one if return is negative&lt;br /&gt;
   else:&lt;br /&gt;
      Tzero+=1            #Increase Tzero by one if return is zero&lt;br /&gt;
rplus=rplus[0:Tplus].copy()        #Remove zeros from rplus&lt;br /&gt;
rminus=rminus[1:Tminus].copy()     #Remove zeros from rminus&lt;br /&gt;
meanplus=mean(rplus)               # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Running code in Python ==&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3187</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3187"/>
				<updated>2013-10-14T17:36:09Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* Simple example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value (note: in version 3 of Python the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object, however in version 2 of Python this function creates a list object). For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
As Python 3 is expected to be the future of Python, we recommend using this version unless you have a good reason not to.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm (remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance).&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org/ NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package to must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;, and namespaces allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a rand function, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespace within the function call, e.g. to use at the Python command prompt&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, rather than assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;does not&amp;#039;&amp;#039;&amp;#039; copy values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; to a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; does copy all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Python&amp;#039;s array (and list) slices work in subtly different ways to MATLAB&amp;#039;s too. For example, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A[m:n]&amp;lt;/source&amp;gt; returns all values from the element with the index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;m&amp;lt;/source&amp;gt; to the element with index &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;n-1&amp;lt;/source&amp;gt;, and because the first element has index 0, we receive the (m+1)&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; to n&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; values, e.g. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[0:10]&lt;br /&gt;
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; r[4:6]&lt;br /&gt;
[5, 6]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Compare this to MATLAB&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; r=[1,2,3,4,5,6,7,8,9,10]&lt;br /&gt;
r =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(1:10)&lt;br /&gt;
ans =&lt;br /&gt;
     1     2     3     4     5     6     7     8     9    10&lt;br /&gt;
&amp;gt;&amp;gt; r(4:6)&lt;br /&gt;
ans =&lt;br /&gt;
     4     5     6&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on whole arrays are much faster than the equivalent code using loops, as they allow optimal use of the processor. Such code optimisation is often called vectorisation. In addition code using vector and matrix operations avoids loops and is often shorter and easier to read.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, as this is much quicker than growing arrays on-the-fly. In this example at the Python prompt we preallocate two arrays with 10,000 elements each, the first array is to contain integers and the second to contain double precision floating point numbers.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== More advanced example ===&lt;br /&gt;
We now look at the Python equivalent of the [Program_Flow_and_Logicals#Relevant_example|Relevant example on the MATLAB page], which assumed we have &amp;lt;math&amp;gt;T\times1&amp;lt;/math&amp;gt; vector of returns &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The naive algorithm using loops in Python is as follows.&lt;br /&gt;
# Find the length of the NumPy array holding  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=numpy.size(r)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus=0; Tzero=0; Tminus=0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Preallocate NumPy arrays &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=numpy.zeros(T)&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=numpy.zeros(T)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe)&lt;br /&gt;
# Set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;i=0&amp;lt;/source&amp;gt; &lt;br /&gt;
# Check whether &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; is greater, smaller or equal to 0&lt;br /&gt;
#* If &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;gt;0&amp;lt;/source&amp;gt;, set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus[Tplus]=r[i]&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;0&amp;lt;/source&amp;gt; set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus[Tminus]=r[i]&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Repeat 5 for &amp;lt;math&amp;gt;i=1,\ldots,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=rplus[0:Tplus].copy()&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=rminus[0:Tminus].copy();&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=numpy,size(r)&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0&lt;br /&gt;
rplus=numpy.zeros(T);rminus=numpy.zeros(T)&lt;br /&gt;
for i in range(T):&lt;br /&gt;
   if r[i]&amp;gt;0:&lt;br /&gt;
      rplus[Tplus]=r[i]   #Store positive return in array rplus&lt;br /&gt;
      Tplus=Tplus+1       #Increase Tplus by one if return is positive&lt;br /&gt;
   elif r[i]&amp;lt;0:&lt;br /&gt;
      rminus[Tminus]=r[i] #Store negative return in array rminus&lt;br /&gt;
      Tminus=Tminus+1     #Increase Tminus by one if return is negative&lt;br /&gt;
   else:&lt;br /&gt;
      Tzero=Tzero+1       #Increasing Tzero by one if return is zero&lt;br /&gt;
rplus=rplus[0:Tplus].copy()        #Remove zeros from rplus&lt;br /&gt;
rminus=rminus[1:Tminus].copy()     #Remove zeros from rminus&lt;br /&gt;
meanplus=mean(rplus)               # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Running code in Python ==&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3186</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3186"/>
				<updated>2013-10-14T17:15:45Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* More advanced example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value (note: in version 3 of Python the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object, however in version 2 of Python this function creates a list object). For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
As Python 3 is expected to be the future of Python, we recommend using this version unless you have a good reason not to.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm (remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance).&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org/ NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package to must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;, and namespaces allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a rand function, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespace within the function call, e.g. to use at the Python command prompt&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, and not a simple assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; does not make a copy of the values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; copies all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on a whole array are much faster than the equivalent code using loops, as they allow optimal use of the processor. Such code optimisation is often called vectorisation. In addition code avoiding loops is often shorter and easier to read.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, as this is much quicker than growing them on-the-fly. For example, preallocate two arrays of size 10,000, the first array containing integers and the second containing double precision floating point numbers, as follows at the Python prompt.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
=== More advanced example ===&lt;br /&gt;
We now look at the Python equivalent of the [Program_Flow_and_Logicals#Relevant_example|Relevant example on the MATLAB page], which assumed we have &amp;lt;math&amp;gt;T\times1&amp;lt;/math&amp;gt; vector of returns &amp;lt;source enclose=none&amp;gt;r&amp;lt;/source&amp;gt; and we want to&lt;br /&gt;
&lt;br /&gt;
# Compute number of positive, negative and zero returns&lt;br /&gt;
# Compute means of positive and negative returns&lt;br /&gt;
&lt;br /&gt;
The naive algorithm using loops in Python is as follows.&lt;br /&gt;
# Find the length of the NumPy array holding  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=numpy.size(r)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initiate three counter variables, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus=0; Tzero=0; Tminus=0&amp;lt;/source&amp;gt;&lt;br /&gt;
# Preallocate NumPy arrays &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=numpy.zeros(T)&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=numpy.zeros(T)&amp;lt;/source&amp;gt; (since we don’t know how many negative and positive returns we will observe)&lt;br /&gt;
# Set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;i=0&amp;lt;/source&amp;gt; &lt;br /&gt;
# Check whether &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;/source&amp;gt; is greater, smaller or equal to 0&lt;br /&gt;
#* If &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;gt;0&amp;lt;/source&amp;gt;, set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus[Tplus]=r[i]&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;r[i]&amp;lt;0&amp;lt;/source&amp;gt; set &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus[Tminus]=r[i]&amp;lt;/source&amp;gt; and add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tminus&amp;lt;/source&amp;gt;&lt;br /&gt;
#* Else add 1 to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
# Repeat 5 for &amp;lt;math&amp;gt;i=1,\ldots,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Remove excessive zeros from &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus&amp;lt;/source&amp;gt;, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rplus=rplus[0:Tplus].copy()&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;rminus=rminus[0:Tminus].copy();&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute means of rminus and rplus. Number of positive, negative and zero returns are stored in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;Tplus,Tminus,Tzero&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=numpy,size(r)&lt;br /&gt;
Tplus=0;Tminus=0;Tzero=0&lt;br /&gt;
rplus=numpy.zeros(T);rminus=numpy.zeros(T)&lt;br /&gt;
for i in range(T):&lt;br /&gt;
   if r[i]&amp;gt;0:&lt;br /&gt;
      rplus[Tplus]=r[i]   #Store positive return in array rplus&lt;br /&gt;
      Tplus=Tplus+1       #Increase Tplus by one if return is positive&lt;br /&gt;
   elif r[i]&amp;lt;0:&lt;br /&gt;
      rminus[Tminus]=r[i] #Store negative return in array rminus&lt;br /&gt;
      Tminus=Tminus+1     #Increase Tminus by one if return is negative&lt;br /&gt;
   else:&lt;br /&gt;
      Tzero=Tzero+1       #Increasing Tzero by one if return is zero&lt;br /&gt;
rplus=rplus[0:Tplus].copy()        #Remove zeros from rplus&lt;br /&gt;
rminus=rminus[1:Tminus].copy()     #Remove zeros from rminus&lt;br /&gt;
meanplus=mean(rplus)               # Compute mean of positive returns using numpy.mean&lt;br /&gt;
meanminus=numpy.sum(rminus)/Tminus # Compute mean of negative returns using numpy.sum&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Running code in Python ==&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3185</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3185"/>
				<updated>2013-10-14T16:29:33Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* Preliminaries */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value (note: in version 3 of Python the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object, however in version 2 of Python this function creates a list object). For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
As Python 3 is expected to be the future of Python, we recommend using this version unless you have a good reason not to.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm (remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance).&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org/ NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package to must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;, and namespaces allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a rand function, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespace within the function call, e.g. to use at the Python command prompt&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, and not a simple assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; does not make a copy of the values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; copies all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on a whole array are much faster than the equivalent code using loops, as they allow optimal use of the processor. Such code optimisation is often called vectorisation. In addition code avoiding loops is often shorter and easier to read.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, as this is much quicker than growing them on-the-fly. For example, preallocate two arrays of size 10,000, the first array containing integers and the second containing double precision floating point numbers, as follows at the Python prompt.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
=== More advanced example ===&lt;br /&gt;
We now look at the Python equivalent of the [[Program_Flow_and_Logicals#Relevant_example]] &lt;br /&gt;
&lt;br /&gt;
== Running code in Python ==&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3184</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3184"/>
				<updated>2013-10-14T16:25:33Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* Simple example of NumPy arrays */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value (note: in version 3 of Python the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object, however in version 2 of Python this function creates a list object). For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
As Python 3 is expected to be the future of Python, we recommend using this version unless you have a good reason not to.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm (remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance).&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org/ NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package to must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;, and namespaces allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a rand function, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespace within the function call, e.g. to use at the Python command prompt&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in a Numpy array, as can be demonstrated at the Python command line.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(10)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.64799452,  0.41578081,  0.11770639,  0.21143116,  0.98658862,&lt;br /&gt;
        0.35056233,  0.32420828,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy arrays have significant differences to MATLAB arrays (and NumPy also contains a matrix class) so it&amp;#039;s important to read the [http://docs.scipy.org/doc/ NumPy documentation], which includes [http://wiki.scipy.org/Tentative_NumPy_Tutorial tutorials] and a [http://wiki.scipy.org/NumPy_for_Matlab_Users comparison of NumPy with MATLAB]. One important difference is the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;copy&amp;lt;/source&amp;gt; function is used to copy values from one array to another, and not a simple assignment with &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;=&amp;lt;/source&amp;gt;. For example, given a NumPy array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, the assignment &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A&amp;lt;/source&amp;gt; does not make a copy of the values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt;, instead &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; are simply two names for the same array. However &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B=A.copy()&amp;lt;/source&amp;gt; copies all values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; into a new array &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations. Operations and function calls on a whole array are much faster than the equivalent code using loops, as they allow optimal use of the processor. Such code optimisation is often called vectorisation. In addition code avoiding loops is often shorter and easier to read.&lt;br /&gt;
&lt;br /&gt;
For example we can test which values in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;A&amp;lt;/source&amp;gt; are greater than 0.5, and then copy those values to a new array called &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;B&amp;lt;/source&amp;gt; as follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True, False, False, False,  True, False, False,  True,  True,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind].copy()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.64799452,  0.98658862,  0.5539366 ,  0.58682753,  0.53097958])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Another method of code optimisation is to preallocate arrays, as this is much quicker than growing them on-the-fly. For example, preallocate two arrays of size 10,000, the first array containing integers and the second containing double precision floating point numbers, as follows at the Python prompt.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; n=10000&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A=numpy.zeros(n,int)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B=A=numpy.zeros(n)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Running code in Python ==&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3183</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3183"/>
				<updated>2013-10-14T15:36:44Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* Simple example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value (note: in version 3 of Python the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object, however in version 2 of Python this function creates a list object). For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
As Python 3 is expected to be the future of Python, we recommend using this version unless you have a good reason not to.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm (remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance).&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org/ NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package to must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;, and namespaces allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a rand function, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespace within the function call, e.g. to use at the Python command prompt&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example of NumPy arrays ===&lt;br /&gt;
In the above example the NumPy rand function returned random values in Numpy &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;arrays&amp;lt;/source&amp;gt;, as can be demonstrated at the Python command line&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(A)&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.61375094,  0.73416961,  0.2053699 ,  0.92205103])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
NumPy &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;arrays&amp;lt;/source&amp;gt; have significant differences to MATLAB arrays (and NumPy also contains a &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;matrix&amp;lt;/source&amp;gt; class) so it&amp;#039;s important to read the [[http://docs.scipy.org/doc/|NumPy documentation]], which includes [[http://wiki.scipy.org/Tentative_NumPy_Tutorial|tutorials]] and a [[http://wiki.scipy.org/NumPy_for_Matlab_Users|comparison with MATLAB]]. One important difference is that to copy values from one array to another &lt;br /&gt;
&lt;br /&gt;
NumPy arrays are important because they can be used in whole array operations, for example we can test which values of A are greater than 0.5, assign the result of the test to a variable, and extract those values in A and assign to a new array called B.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; ind = A &amp;gt; 0.5&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; ind&lt;br /&gt;
array([ True,  True, False,  True], dtype=bool)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B = A[ind]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; B&lt;br /&gt;
array([ 0.61375094,  0.73416961,  0.92205103])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Running code in Python ==&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3182</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3182"/>
				<updated>2013-10-14T15:35:14Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* Using Python Packages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value (note: in version 3 of Python the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object, however in version 2 of Python this function creates a list object). For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
As Python 3 is expected to be the future of Python, we recommend using this version unless you have a good reason not to.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm (remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance).&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [http://www.numpy.org/ NumPy], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [https://pypi.python.org/pypi the Python Package Index].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package to must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;, and namespaces allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a rand function, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespace within the function call, e.g. to use at the Python command prompt&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
&lt;br /&gt;
== Running code in Python ==&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3181</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3181"/>
				<updated>2013-10-14T12:17:36Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* Preliminaries */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;range&amp;#039;&amp;#039;&amp;#039; of values, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOrRangeOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOrRangeOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list or range object in Python. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value (note: in version 3 of Python the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function creates a range object, however in version 2 of Python this function creates a list object). For example to create a range containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
As Python 3 is expected to be the future of Python, we recommend using this version unless you have a good reason not to.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm (remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance).&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [[http://www.numpy.org/|NumPy]], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [[https://pypi.python.org/pypi|the Python Package Index]].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package to must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;, and namespaces allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a rand function, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespace within the function call, e.g. to use at the Python command prompt&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
&lt;br /&gt;
== Running code in Python ==&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3180</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3180"/>
				<updated>2013-10-14T11:50:41Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* Improvements on the above */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; of values&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list in Python. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. For example to create a list containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included in the list, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can also be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
As Python 3 is expected to be the future of Python, we recommend using this version unless you have a good reason not to.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm (remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance).&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above (avoiding loops) ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both &amp;#039;&amp;#039;&amp;#039;simplifies code&amp;#039;&amp;#039;&amp;#039;, and also &amp;#039;&amp;#039;&amp;#039;allows programs to run faster&amp;#039;&amp;#039;&amp;#039;, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
The functionality that allows us to operate on whole vectors and matrices isn&amp;#039;t part of core Python, and requires us to use a Python package called [[http://www.numpy.org/|NumPy]], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [[https://pypi.python.org/pypi|the Python Package Index]].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package to must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;, and namespaces allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a rand function, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespace within the function call, e.g. to use at the Python command prompt&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
=== Simple example ===&lt;br /&gt;
&lt;br /&gt;
== Running code in Python ==&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3179</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3179"/>
				<updated>2013-10-14T11:42:38Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* Improvements on the above */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; of values&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list in Python. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. For example to create a list containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included in the list, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can also be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
As Python 3 is expected to be the future of Python, we recommend using this version unless you have a good reason not to.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm (remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance).&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above ==&lt;br /&gt;
&lt;br /&gt;
=== Using Python Packages ===&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both simplifies code, and also allows programs to run faster, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
However, this functionality isn&amp;#039;t part of core Python, and requires us to use a Python package called [[http://www.numpy.org/|NumPy]], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [[https://pypi.python.org/pypi|the Python Package Index]].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package to must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;, and namespaces allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a rand function, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespace within the function call, e.g. to use at the Python command prompt&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Running code in Python ==&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3178</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3178"/>
				<updated>2013-10-14T11:41:35Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* Improvements on the above */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; of values&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list in Python. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. For example to create a list containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included in the list, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can also be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
As Python 3 is expected to be the future of Python, we recommend using this version unless you have a good reason not to.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm (remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance).&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both simplifies code, and also allows programs to run faster, in particular:&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions can work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays&lt;br /&gt;
&lt;br /&gt;
However, this functionality isn&amp;#039;t part of core Python, and requires us to use a Python package called [[http://www.numpy.org/|NumPy]], which adds other useful functionality including pseudo-random number generators. There are many other Python Packages, which are listed at [[https://pypi.python.org/pypi|the Python Package Index]].&lt;br /&gt;
&lt;br /&gt;
Before using a Python package, the package to must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within &amp;#039;&amp;#039;&amp;#039;namespaces&amp;#039;&amp;#039;&amp;#039;, and namespaces allow package writers to choose functions names without worrying about whether that function name has been used elsewhere. For example, NumPy includes a rand function, which exists within a namespace called &amp;#039;&amp;#039;random&amp;#039;&amp;#039;. And the &amp;#039;&amp;#039;random&amp;#039;&amp;#039; namespace is within the NumPy namespace (which is called &amp;#039;&amp;#039;numpy&amp;#039;&amp;#039;). After importing NumPy we can use the rand function, but have to include the namespace within the function call, e.g. to use at the Python command prompt&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random.rand&amp;lt;/source&amp;gt; refers to the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;rand&amp;lt;/source&amp;gt; function in the &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;numpy.random&amp;lt;/source&amp;gt; namespace. While this allows safe reuse of names, it does potentially introduce a lot of extra typing, and so Python includes ways to simplify our code. For example, we can import individual functions from a namespace as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can also rename the function as we import it&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In addition we can rename the namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Running code in Python ==&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3177</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3177"/>
				<updated>2013-10-14T11:29:27Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* while */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; of values&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list in Python. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. For example to create a list containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included in the list, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can also be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
As Python 3 is expected to be the future of Python, we recommend using this version unless you have a good reason not to.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm (remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance).&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code is a follows.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This introduces a shorthand also used in other programming languages (e.g. C) as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both simplifies code, and also allows programs to run faster, in particular&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays.&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays.&lt;br /&gt;
&lt;br /&gt;
However, this functionality isn&amp;#039;t part of core Python, and to use requires us to use a Python package called [[http://www.numpy.org/|NumPy]]. There are various add on Python Packages, which are listed at [[https://pypi.python.org/pypi|the Python Package Index]].&lt;br /&gt;
&lt;br /&gt;
Before using the functionality within a Python package, the package to must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within namespaces, this allows package writers to write functions without worrying about whether the function name has been used elsewhere, for example, NumPy includes a rand function, which exists within a namespace called random, which itself is within the NumPy namespace (which is called numpy). So after importing NumPy we can use the rand function, e.g. at the Python command prompt&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and numpy.random.rand refers to the rand function in a numpy.random namespace.&lt;br /&gt;
For simple programs this could introduce a lot of extra typing, and Python includes ways to simplify things. For example, we can import individual functions from a namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
we can rename a function&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can rename a namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
== Running code in Python ==&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3176</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3176"/>
				<updated>2013-10-14T11:24:46Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* while */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; of values&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list in Python. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. For example to create a list containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included in the list, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can also be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
As Python 3 is expected to be the future of Python, we recommend using this version unless you have a good reason not to.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm (remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance).&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
introduces a shorthand implemented in some other programming languages, e.g. C. The line &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both simplifies code, and also allows programs to run faster, in particular&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays.&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays.&lt;br /&gt;
&lt;br /&gt;
However, this functionality isn&amp;#039;t part of core Python, and to use requires us to use a Python package called [[http://www.numpy.org/|NumPy]]. There are various add on Python Packages, which are listed at [[https://pypi.python.org/pypi|the Python Package Index]].&lt;br /&gt;
&lt;br /&gt;
Before using the functionality within a Python package, the package to must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within namespaces, this allows package writers to write functions without worrying about whether the function name has been used elsewhere, for example, NumPy includes a rand function, which exists within a namespace called random, which itself is within the NumPy namespace (which is called numpy). So after importing NumPy we can use the rand function, e.g. at the Python command prompt&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and numpy.random.rand refers to the rand function in a numpy.random namespace.&lt;br /&gt;
For simple programs this could introduce a lot of extra typing, and Python includes ways to simplify things. For example, we can import individual functions from a namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
we can rename a function&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can rename a namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
== Running code in Python ==&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3175</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3175"/>
				<updated>2013-10-14T11:24:16Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* while */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; of values&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list in Python. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. For example to create a list containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included in the list, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can also be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
As Python 3 is expected to be the future of Python, we recommend using this version unless you have a good reason not to.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm (remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance).&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;=T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
introduces a shorthand implemented in some other programming languages, e.g. C. The line &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both simplifies code, and also allows programs to run faster, in particular&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays.&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays.&lt;br /&gt;
&lt;br /&gt;
However, this functionality isn&amp;#039;t part of core Python, and to use requires us to use a Python package called [[http://www.numpy.org/|NumPy]]. There are various add on Python Packages, which are listed at [[https://pypi.python.org/pypi|the Python Package Index]].&lt;br /&gt;
&lt;br /&gt;
Before using the functionality within a Python package, the package to must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within namespaces, this allows package writers to write functions without worrying about whether the function name has been used elsewhere, for example, NumPy includes a rand function, which exists within a namespace called random, which itself is within the NumPy namespace (which is called numpy). So after importing NumPy we can use the rand function, e.g. at the Python command prompt&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and numpy.random.rand refers to the rand function in a numpy.random namespace.&lt;br /&gt;
For simple programs this could introduce a lot of extra typing, and Python includes ways to simplify things. For example, we can import individual functions from a namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
we can rename a function&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can rename a namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
== Running code in Python ==&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3174</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3174"/>
				<updated>2013-10-14T11:21:36Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, in Python a code block ends when the indenting reverts. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. lines with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python (compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB). Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; of values&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list to &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list in Python. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. For example to create a list containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, we can use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included in the list, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can also be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and elements don&amp;#039;t have to be of the same class. It is for this reason that lists are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python follows, and a description of how to run this code is given towards the end of this page. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets (array indices start at 1 in MATLAB). It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point value. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
As Python 3 is expected to be the future of Python, we recommend using this version unless you have a good reason not to.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm (remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance).&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;=T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
introduces a shorthand implemented in some other programming languages, e.g. C. The line &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements on the above ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both simplifies code, and also allows programs to run faster, in particular&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays.&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays.&lt;br /&gt;
&lt;br /&gt;
However, this functionality isn&amp;#039;t part of core Python, and to use requires us to use a Python package called [[http://www.numpy.org/|NumPy]]. There are various add on Python Packages, which are listed at [[https://pypi.python.org/pypi|the Python Package Index]].&lt;br /&gt;
&lt;br /&gt;
Before using the functionality within a Python package, the package to must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within namespaces, this allows package writers to write functions without worrying about whether the function name has been used elsewhere, for example, NumPy includes a rand function, which exists within a namespace called random, which itself is within the NumPy namespace (which is called numpy). So after importing NumPy we can use the rand function, e.g. at the Python command prompt&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and numpy.random.rand refers to the rand function in a numpy.random namespace.&lt;br /&gt;
For simple programs this could introduce a lot of extra typing, and Python includes ways to simplify things. For example, we can import individual functions from a namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
we can rename a function&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can rename a namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
== Running code in Python ==&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3171</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3171"/>
				<updated>2013-10-11T15:13:44Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, Python uses a change in indent. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in general in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python, compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB. Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; of values&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list to the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list in Python. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. For example to create a list containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included in the list, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can also be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and that elements don&amp;#039;t have to be of the same class. It is for this reason that lists are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python is &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets, whereas  array indices start at 1 in MATLAB. It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
As Python 3 is expected to be the future of Python, we recommend using this version unless you have a good reason not to.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm (remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance).&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;=T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
introduces a shorthand implemented in some other programming languages, e.g. C. The line &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Improvements of the above ==&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python allow us to adopt a programming style that both simplifies code, and also allows programs to run faster, in particular&lt;br /&gt;
&lt;br /&gt;
# Operators, functions and logical expressions work not only on scalars, but also on vectors, matrices and, in general, on n-dimensional arrays.&lt;br /&gt;
# Subvectors/submatrices can be extracted using logical 0-1 arrays.&lt;br /&gt;
&lt;br /&gt;
However, this functionality isn&amp;#039;t part of core Python, and to use requires us to use a Python package called [[http://www.numpy.org/|NumPy]]. There are various add on Python Packages, which are listed at [[https://pypi.python.org/pypi|the Python Package Index]].&lt;br /&gt;
&lt;br /&gt;
Before using the functionality within a Python package, the package to must be imported, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&amp;lt;/source&amp;gt;&lt;br /&gt;
Functions within a package are located within namespaces, this allows package writers to write functions without worrying about whether the function name has been used elsewhere, for example, NumPy includes a rand function, which exists within a namespace called random, which itself is within the NumPy namespace (which is called numpy). So after importing NumPy we can use the rand function, e.g. at the Python command prompt&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = numpy.random.rand(5)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.50639352,  0.44000756,  0.16118149,  0.69615487,  0.3887179 ])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and numpy.random.rand refers to the rand function in a numpy.random namespace.&lt;br /&gt;
For simple programs this could introduce a lot of extra typing, and Python includes ways to simplify things. For example, we can import individual functions from a namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.25254338,  0.95567921,  0.28244092,  0.92564069])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
we can rename a function&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; from numpy.random import rand as nprand&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = nprand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.96127673,  0.57402182,  0.36119553,  0.99832014])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and we can rename a namespace&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import numpy&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy.random as npr&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A = npr.rand(4)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; A&lt;br /&gt;
array([ 0.4282803 ,  0.80106321,  0.7078212 ,  0.13823879])&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3153</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3153"/>
				<updated>2013-10-11T11:42:13Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: /* Preliminaries */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, Python uses a change in indent. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in general in Python there is no need to add a semicolon at the end of a line to suppress output, since Python produces no output for lines involving assignment (i.e. with the  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;=&amp;lt;/source&amp;gt; sign).&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python, compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB. Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; of values&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for LoopVariable in ListOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list to the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;LoopVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list in Python. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. For example to create a list containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included in the list, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 for these if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can also be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and that elements don&amp;#039;t have to be of the same class. It is for this reason that lists are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to &amp;#039;&amp;#039;&amp;#039;break&amp;#039;&amp;#039;&amp;#039; out of for or while loops, or &amp;#039;&amp;#039;&amp;#039;continue&amp;#039;&amp;#039;&amp;#039; with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python is &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and indices are placed inside square brackets, whereas  array indices start at 1 in MATLAB. It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039; instead. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
As Python 3 is expected to be the future of Python, we recommend using this version unless you have a good reason not to.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The Python alternative of the above code using a conditional &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loop implements the following algorithm (remember that this contrived example is purely for demonstration purposes, and usually &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;while&amp;lt;/source&amp;gt; loops are used when the number of iterations is not known in advance).&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms e: T=len(e)&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Increase i by 1, i.e. &amp;lt;math&amp;gt;i=i+1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Repeat lines 5-6 whilst &amp;lt;math&amp;gt;i&amp;lt;=T&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Python code&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
i=1&lt;br /&gt;
while i &amp;lt; T:&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
   i+=1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
introduces a shorthand implemented in some other programming languages, e.g. C. The line &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i+=1&amp;lt;/source&amp;gt; is shorthand for &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i+1&amp;lt;/source&amp;gt;. This shorthand can be used with other operators, e.g. &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i*=10&amp;lt;/source&amp;gt; is equivalent to typing &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;i=i*10&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For comparison, the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3149</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3149"/>
				<updated>2013-10-10T11:15:21Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, Python uses a change in indent. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output.&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python, compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB. Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; of values&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for CounterVariable in ListOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list to the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;CounterVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list in Python. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of integers with a defined start, stop and step value. For example to create a list containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included in the list, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This might seems strange, but makes more sense when we realise the start and step values are optional, and the range function assumes default values of 1 if they are not given, i.e.  &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(N)&amp;lt;/source&amp;gt; returns &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;N&amp;lt;/source&amp;gt; values starting at 1, e.g.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(5)&lt;br /&gt;
[0, 1, 2, 3, 4]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(10)&lt;br /&gt;
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can also be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and that elements don&amp;#039;t have to be of the same class. It is for this reason that lists are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to break out of for or while loops, or continues with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python is &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and uses square brackets, whereas  array indices start at 1 in MATLAB. It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain instead &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039;. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
As Python 3 is expected to be the future of Python, we recommend using this version unless you have a good reason not to.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3148</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3148"/>
				<updated>2013-10-10T10:55:46Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, Python uses a change in indent. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output.&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python, compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB. Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; of values&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for CounterVariable in ListOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list to the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;CounterVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list in Python. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of numbers with a defined start, stop and step value. For example to create a list containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included in the list, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can also be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and that elements don&amp;#039;t have to be of the same class. It is for this reason that lists are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to break out of for or while loops, or continues with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python is &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and uses square brackets, whereas  array indices start at 1 in MATLAB. It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain instead &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039;. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0)&lt;br /&gt;
&amp;lt;type &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
As Python 3 is expected to be the future of Python, we recommend using this version unless you have a good reason not to.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3147</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3147"/>
				<updated>2013-10-10T10:54:34Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, Python uses a change in indent. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output.&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python, compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB. Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; of values&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for CounterVariable in ListOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list to the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;CounterVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list in Python. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of numbers with a defined start, stop and step value. For example to create a list containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included in the list, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can also be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and that elements don&amp;#039;t have to be of the same class. It is for this reason that lists are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to break out of for or while loops, or continues with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python is &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important difference to MATLAB is that Python list and array indexing starts at 0 and uses square brackets, whereas  array indices start at 1 in MATLAB. It is also important to understand that Python generally assumes a number to be integer unless there is something to indicate it is a floating point. Consider the line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; that preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;floating point&amp;#039;&amp;#039;&amp;#039; numbers all set to zero. If this had been written as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0]*T&amp;lt;/source&amp;gt; the list would contain instead &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; &amp;#039;&amp;#039;&amp;#039;integers&amp;#039;&amp;#039;&amp;#039;. We can demonstrate this at the Python prompt using the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;type&amp;lt;/source&amp;gt; function, which tells us the class of an object, e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(0.0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(1e0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; type(0e0)&lt;br /&gt;
&amp;lt;type &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Controversially, the behaviour of integer division changed in Python version 3, compared to version 2, and it is worth mentioning this now. In Python 2 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
whereas in Python 3&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;type(1/2)&lt;br /&gt;
&amp;lt;class &amp;#039;float&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; 1/2&lt;br /&gt;
0.5&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
As Python 3 is expected to be the future of Python, we recommend using this version unless you have a good reason not to.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This can be implemented in Python as &lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=0.0&lt;br /&gt;
if abs(phi1)&amp;lt;1:&lt;br /&gt;
   y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is relatively similar to the MATLAB version&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=0;&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;br /&gt;
the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  if abs(phi1)&amp;lt;1&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  else&lt;br /&gt;
  y0=0;&lt;br /&gt;
  end&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1)&lt;br /&gt;
  i=2;&lt;br /&gt;
  while i&amp;lt;=T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
    i=i+1;&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3146</id>
		<title>Python/Program Flow and Logicals</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Python/Program_Flow_and_Logicals&amp;diff=3146"/>
				<updated>2013-10-10T10:32:13Z</updated>
		
		<summary type="html">&lt;p&gt;Jb: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Preliminaries =&lt;br /&gt;
&lt;br /&gt;
One important thing to understand when programming in Python is that &amp;#039;&amp;#039;&amp;#039;correct indenting of code is essential&amp;#039;&amp;#039;&amp;#039;. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.&lt;br /&gt;
* while and for loops&lt;br /&gt;
* if, elif, else constructs&lt;br /&gt;
* functions&lt;br /&gt;
The indent for each block must be the same, the Python programming language also requires you to mark the start of a block with a colon. So where MATLAB used &amp;lt;source enclose=none&amp;gt;end&amp;lt;/source&amp;gt; to mark the end of a block of code, Python uses a change in indent. Other than this, simple Python programmes aren&amp;#039;t dissimilar to those in MATLAB.&lt;br /&gt;
&lt;br /&gt;
For example, the simplest case of an &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; conditional statement in Python would look something like this&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where the code in lines &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed only if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;. Sharp sighted readers might spot another difference to MATLAB, in Python there is no need to add a semicolon at the end of a line to suppress output.&lt;br /&gt;
&lt;br /&gt;
The boolean &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;==&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;equality&amp;#039;&amp;#039;&amp;#039;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;&amp;gt;=&amp;lt;/source&amp;gt; test for &amp;#039;&amp;#039;&amp;#039;greater than&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;greater than or equal to&amp;#039;&amp;#039;&amp;#039; respectively. The main difference is that&amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;!=&amp;lt;/source&amp;gt; tests for &amp;#039;&amp;#039;&amp;#039;inequality&amp;#039;&amp;#039;&amp;#039; in Python, compared to &amp;lt;source enclose=none&amp;gt;~=&amp;lt;/source&amp;gt; in MATLAB. Relational operators return boolean values of either &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt; or &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
And Python&amp;#039;s logical operators are &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;and&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;or&amp;lt;/source&amp;gt; and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;not&amp;lt;/source&amp;gt;, which are hopefully self explanatory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if&amp;lt;/source&amp;gt; functionality can be expanded using &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; as follows&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;  &lt;br /&gt;
where &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;, and &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement1a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;statement2a&amp;lt;/source&amp;gt;, &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;...&amp;lt;/source&amp;gt; is executed if &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;False&amp;lt;/source&amp;gt;. Note that the code block after the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;else&amp;lt;/source&amp;gt; starts with a colon, and this code block is also indented.&lt;br /&gt;
&lt;br /&gt;
Finally, the most general form of this programming construct introduces the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;elif&amp;lt;/source&amp;gt; keyword (in contrast to &amp;lt;source enclose=none&amp;gt;elseif&amp;lt;/source&amp;gt; in MATLAB) to give&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;if condition1:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
elif condition2:&lt;br /&gt;
   statement1a&lt;br /&gt;
   statement2a&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
   ...&lt;br /&gt;
elif conditionN:&lt;br /&gt;
   statement1b&lt;br /&gt;
   statement2b&lt;br /&gt;
   ...&lt;br /&gt;
else:&lt;br /&gt;
   statement1c&lt;br /&gt;
   statement2c&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; of values&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;for CounterVariable in ListOfValues:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
and repeat for as many times as there are elements in the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;ListOfValues&amp;lt;/source&amp;gt;, each time assigning the next element in the list to the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;CounterVariable&amp;lt;/source&amp;gt;. The code block associated with the loop is identified by a colon and indenting as described above.&lt;br /&gt;
&lt;br /&gt;
There are various ways of creating a list in Python. The &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range&amp;lt;/source&amp;gt; function can be used to create sequences of numbers with a defined start, stop and step value. For example to create a list containing the four values 1, 4, 7 and 10, i.e. a sequence starting at 1 with steps of 3, use &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,11,3)&amp;lt;/source&amp;gt;. Note that the stop value passed to the range function is not included in the list, i.e. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;range(1,10,3)&amp;lt;/source&amp;gt; would produce only the three numbers 1, 4 &amp;amp; 7. We can verify this at the Python command prompt, i.e.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;gt; range(1,11,3)&lt;br /&gt;
[1, 4, 7, 10]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; range(1,10,3)&lt;br /&gt;
[1, 4, 7]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python lists can also be created from a sequence of values separated by commas within square brackets, e.g. &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList = [1.0, &amp;quot;hello&amp;quot;, 1]&amp;lt;/source&amp;gt; creates a list called &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;MyList&amp;lt;/source&amp;gt; containing 3 values, a floating point number &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1.0&amp;lt;/source&amp;gt;, the string &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;hello&amp;lt;/source&amp;gt; and an integer &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;1&amp;lt;/source&amp;gt;. This example demonstrates that Python lists are general purpose containers, and that elements don&amp;#039;t have to be of the same class. It is for this reason that lists are best avoided for numerical calculations unless they are relatively simple, as there are much more efficient containers for numbers, i.e. NumPy arrays, which will be introduced in due course.&lt;br /&gt;
&lt;br /&gt;
Conditional while loops are identified with the &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; keyword, so &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;while condition:&lt;br /&gt;
   statement1&lt;br /&gt;
   statement2&lt;br /&gt;
   ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will repeatedly execute the code block for as long as &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;condition&amp;lt;/source&amp;gt; is &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;True&amp;lt;/source&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As in MATLAB, Python allows us to break out of for or while loops, or continues with the next iteration of a loop, using &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;break&amp;lt;/source&amp;gt; and &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;continue&amp;lt;/source&amp;gt; respectively. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;for &amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
We now look at the Python equivalents of the MATLAB code discussed in the [[Program_Flow_and_Logicals#for_..._end_loop|MATLAB page on Program Flow and Logicals]]. A description of the mathematics is available on the MATLAB page, for brevity it is not repeated here. In the case when the error terms in &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt; are known in advance, the Python version of the algorithm is:&lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as vector &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;. Please remember, we assume that &amp;lt;math&amp;gt;y_0=E(y)=\phi_0/(1-\phi_1)&amp;lt;/math&amp;gt;&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 4 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A simple implementation in Python is &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;T=len(e)&lt;br /&gt;
y=[0.0]*T&lt;br /&gt;
y0=phi0/(1-phi1)&lt;br /&gt;
y[0]=phi0+phi1*y0+e[0]&lt;br /&gt;
for i in range(1,T):&lt;br /&gt;
   y[i]=phi0+phi1*y[i-1]+e[i]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and for comparison the MATLAB code is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;  T=size(e,1);&lt;br /&gt;
  y=zeros(T,1);&lt;br /&gt;
  y0=phi0/(1-phi1);&lt;br /&gt;
  y(1)=phi0+phi1*y0+e(1);&lt;br /&gt;
  for i=2:T&lt;br /&gt;
    y(i)=phi0+phi1*y(i-1)+e(i);&lt;br /&gt;
  end&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One difference to note is Python list and array indexing starts at 0 and uses square brackets, whereas  array indices start at 1 in MATLAB. The line &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt; preallocates a Python list containing &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T&amp;lt;/source&amp;gt; floating point numbers all set to zero.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;if else&amp;lt;/source&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
As above, a description of the mathematics can be found on the [[Program_Flow_and_Logicals#if_else_end_or_if_end|MATLAB page on Program Flow and Logicals]]. The Python algorithm is now &lt;br /&gt;
&lt;br /&gt;
# Find length of the list containing the error terms &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;T=len(e)&amp;lt;/source&amp;gt;&lt;br /&gt;
# Initialize a list &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y&amp;lt;/source&amp;gt; with the same length as &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;e&amp;lt;/source&amp;gt;: &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y=[0.0]*T&amp;lt;/source&amp;gt;&lt;br /&gt;
# Check whether &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;abs(phi1)&amp;lt;1&amp;lt;/source&amp;gt;. If this statement is true, then &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=phi0/(1-phi1)&amp;lt;/source&amp;gt;. Else, &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y0=0&amp;lt;/source&amp;gt;. Please remember, we set &amp;lt;math&amp;gt;y_0=E(y_0)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[0]=phi0+phi1*y0+e[0]&amp;lt;/source&amp;gt;.&lt;br /&gt;
# Compute &amp;lt;source enclose=none lang=&amp;quot;python&amp;quot;&amp;gt;y[i]=phi0+phi1*y[i-1]+e[i]&amp;lt;/source&amp;gt; for &amp;lt;math&amp;gt;i=1&amp;lt;/math&amp;gt;&lt;br /&gt;
# Repeat line 5 for &amp;lt;math&amp;gt;i=2,...,(T-1)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;source lang=&amp;quot;python&amp;quot; enclose=none&amp;gt;while&amp;lt;/source&amp;gt; ==&lt;/div&gt;</summary>
		<author><name>Jb</name></author>	</entry>

	</feed>