Difference between revisions of "Python/Program Flow and Logicals"
m |
|||
Line 1: | Line 1: | ||
= Preliminaries = | = Preliminaries = | ||
− | One essential thing to understand when programming in Python is | + | 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 | * while and for loops | ||
* if, elif, else constructs | * if, elif, else constructs | ||
* functions | * functions | ||
− | The indent for each | + | 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 | |
<source>if condition: | <source>if condition: | ||
statement1 | statement1 | ||
statement2 | statement2 | ||
... | ... | ||
− | + | </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. | |
The <source enclose=none>if</source> functionality can be expanded using <source enclose=none>else</source> as follows | The <source enclose=none>if</source> functionality can be expanded using <source enclose=none>else</source> as follows | ||
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 | + | 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. |
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 enclose=none>elif</source> keyword (in contrast to <source enclose=none>elseif</source> in MATLAB to give |
Revision as of 13:11, 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
...
...
...
elseif conditionN:
statement1b
statement2b
...
else:
statement1c
statement2c
...