Difference between revisions of "Python/Program Flow and Logicals"
Line 7: | Line 7: | ||
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 <source enclose=none>end</source> 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. | 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 <source enclose=none>end</source> 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 <source enclose=none>if</source> conditional statement in Python would look something like this | + | For example, the simplest case of an <source lang="python" enclose=none>if</source> conditional statement in Python would look something like this |
<source lang="python">if condition: | <source lang="python">if condition: | ||
statement1 | statement1 | ||
Line 13: | Line 13: | ||
... | ... | ||
</source> | </source> | ||
− | where the code in lines <source enclose=none>statement1</source>, <source enclose=none>statement2</source>, <source enclose=none>...</source> is executed only if <source 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 enclose=none>if</source> functionality can be expanded using <source 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 |
<source lang="python">if condition: | <source lang="python">if condition: | ||
statement1 | statement1 | ||
Line 25: | Line 25: | ||
... | ... | ||
</source> | </source> | ||
− | where <source enclose=none>statement1</source>, <source enclose=none>statement2</source>, <source enclose=none>...</source> is executed if <source enclose=none>condition</source> is true, and <source enclose=none>statement1a</source>, <source enclose=none>statement2a</source>, <source enclose=none>...</source> is executed if <source enclose=none>condition</source> is false. Note that the code block after the <source enclose=none>else</source> starts with a colon, and this code block is also indented. | + | where <source lang="python" enclose=none>statement1</source>, <source lang="python" enclose=none>statement2</source>, <source lang="python" enclose=none>...</source> is executed if <source lang="python" enclose=none>condition</source> is true, and <source lang="python" enclose=none>statement1a</source>, <source lang="python" enclose=none>statement2a</source>, <source lang="python" enclose=none>...</source> is executed if <source lang="python" enclose=none>condition</source> is false. Note that the code block after the <source lang="python" enclose=none>else</source> starts with a colon, and this code block is also indented. |
− | Finally, the most general form of this programming construct introduces the <source enclose=none>elif</source> keyword (in contrast to <source enclose=none>elseif</source> in MATLAB to give | + | Finally, the most general form of this programming construct introduces the <source lang="python" enclose=none>elif</source> keyword (in contrast to <source enclose=none>elseif</source> in MATLAB) to give |
<source lang="python">if condition1: | <source lang="python">if condition1: |
Revision as of 13:34, 8 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 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
...