Difference between revisions of "Python/Program Flow and Logicals"

From ECLR
Jump to: navigation, search
Line 14: Line 14:
 
</source>
 
</source>
 
where the code in lines <source lang="python" enclose=none>statement1</source>, <source lang="python" enclose=none>statement2</source>, <source lang="python" enclose=none>...</source> is executed only if <source lang="python" enclose=none>condition</source> is true. 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.
 
where the code in lines <source lang="python" enclose=none>statement1</source>, <source lang="python" enclose=none>statement2</source>, <source lang="python" enclose=none>...</source> is executed only if <source lang="python" enclose=none>condition</source> is true. 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.
 +
 +
The <source lang="python" enclose=none>condition</source> can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. <source lang="python" enclose=none>==</source> tests for equality, <source lang="python" enclose=none>></source> and <source lang="python" enclose=none>>=</source> test for '''greater than''' and '''greater than or equal to''' respectively. The main difference is that<source lang="python" enclose=none>!=</source> tests for inequality in Python, compared to <source lang="python" enclose=none>~=</source> in MATLAB. Relational operators return boolean values of either <source lang="python" enclose=none>True</source> or <source lang="python" enclose=none>False</source>.
 +
 +
And Python's logical operators are <source lang="python" enclose=none>and</source>, <source lang="python" enclose=none>or</source> and <source lang="python" enclose=none>not</source>, which are hopefully self explanatory.
  
 
The <source lang="python" enclose=none>if</source> functionality can be expanded using <source lang="python" enclose=none>else</source> as follows
 
The <source lang="python" enclose=none>if</source> functionality can be expanded using <source lang="python" enclose=none>else</source> as follows
Line 48: Line 52:
 
   ...
 
   ...
 
</source>
 
</source>
 +
 +
Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a list of values
 +
 +
<source lang="python">for CounterVariable in ListOfValues:
 +
  statement1
 +
  statement2
 +
  ...
 +
</source>
 +
and repeat for as many times as there are elements in the <source lang="python" enclose=none>ListOfValues</source>, each time assigning the next element in the list to the <source lang="python" enclose=none>CounterVariable</source>. The code block associated with the loop is identified by a colon and indenting as described above.
 +
 +
There are various ways of creating a Python list. The <source lang="python" enclose=none>range</source> 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 4 values [1  4  7 10] use <source lang="python" enclose=none>range(1,3,11)</source>, noting that the stop value is not included in the list, i.e. <source lang="python" enclose=none>range(1,3,10)</source> would produce only the numbers [1  4  7].
 +
 +
Python lists can also be created using a sequence of values separated by commas within square brackets, e.g. <source lang="python" enclose=none>MyList = [1.0, "hello", 1]</source> creates a list called <source lang="python" enclose=none>MyList</source> containing 3 values, a floating point number <source lang="python" enclose=none>1.0</source>, the string <source lang="python" enclose=none>hello</source> and an integer <source lang="python" enclose=none>1</source>. This example demonstrates that Python lists are general purpose containers, and the elements don't have to be the same class.
 +
 +
To add: Python equivalent of break and continue.
 +
 +
Conditional while loops are identified with the <source lang="python" enclose=none>while</source> keyword, so
 +
 +
<source lang="python">while condition:
 +
  statement1
 +
  statement2
 +
  ...
 +
</source>
 +
will repeatedly execute the code block for as long as <source lang="python" enclose=none>condition</source> is true.

Revision as of 16:14, 9 October 2013

Preliminaries

One essential thing to understand when programming in Python is correct indenting of code is essential. The Python programming language was designed with readability in mind, and as a result forces you to indent code blocks, e.g.

  • while and for loops
  • if, elif, else constructs
  • functions

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 end to mark the end of a block of code, Python uses a change in indent. Other than this, simple Python programmes aren't dissimilar to those in MATLAB.

For example, the simplest case of an if conditional statement in Python would look something like this

if condition:
   statement1
   statement2
   ...

where the code in lines statement1, statement2, ... is executed only if condition is true. 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.

The condition can be built up using relational and logical operators. Relational operators in Python are similar to those in MATLAB, e.g. == tests for equality, > and >= test for greater than and greater than or equal to respectively. The main difference is that!= tests for inequality in Python, compared to ~= in MATLAB. Relational operators return boolean values of either True or False.

And Python's logical operators are and, or and not, which are hopefully self explanatory.

The if functionality can be expanded using else as follows

if condition:
   statement1
   statement2
   ...
else:
   statement1a
   statement2a
   ...

where statement1, statement2, ... is executed if condition is true, and statement1a, statement2a, ... is executed if condition is false. Note that the code block after the else starts with a colon, and this code block is also indented.

Finally, the most general form of this programming construct introduces the elif keyword (in contrast to elseif in MATLAB) to give

if condition1:
   statement1
   statement2
   ...
elif condition2:
   statement1a
   statement2a
   ...
   ...
   ...
elif conditionN:
   statement1b
   statement2b
   ...
else:
   statement1c
   statement2c
   ...

Like MATLAB, Python has while and for loops. Unconditional for loops iterate over a list of values

for CounterVariable in ListOfValues:
   statement1
   statement2
   ...

and repeat for as many times as there are elements in the ListOfValues, each time assigning the next element in the list to the CounterVariable. The code block associated with the loop is identified by a colon and indenting as described above.

There are various ways of creating a Python list. The range 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 4 values [1 4 7 10] use range(1,3,11), noting that the stop value is not included in the list, i.e. range(1,3,10) would produce only the numbers [1 4 7].

Python lists can also be created using a sequence of values separated by commas within square brackets, e.g. MyList = [1.0, "hello", 1] creates a list called MyList containing 3 values, a floating point number 1.0, the string hello and an integer 1. This example demonstrates that Python lists are general purpose containers, and the elements don't have to be the same class.

To add: Python equivalent of break and continue.

Conditional while loops are identified with the while keyword, so

while condition:
   statement1
   statement2
   ...

will repeatedly execute the code block for as long as condition is true.