Difference between revisions of "Software tutorial/Loops"
m (Created page with "Looping with a <tt>for</tt> loop is easy when you know how many iterations will be required. {| class="wikitable" |- ! MATLAB ! Python |- | width="50%" valign="top" | The simp...") |
m |
||
(8 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
{{Navigation|Book=Software tutorial|previous=My first program|current=Tutorial index|next=Scripts and functions}} | |||
__TOC__ | |||
== <tt>for</tt> loops == | |||
Looping with a <tt>for</tt> loop is used when you know '''ahead of time''' how many iterations will be required. | |||
{| class="wikitable" | {| class="wikitable" | ||
|- | |- | ||
! | ! MATLAB | ||
! Python | ! Python | ||
|- | |- | ||
| width="50%" valign="top" | | | width="50%" valign="top" | | ||
A simple for-loop can be written as: | |||
<syntaxhighlight lang="matlab"> | <syntaxhighlight lang="matlab"> | ||
for k = 0:4 | for k = 0:4 | ||
Line 23: | Line 26: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
The reason why this works is that instruction <tt>k = | The reason why this works is that instruction <tt>k = 0:4</tt> creates a vector, starting at 0 and ending at 4, in steps of 1.0. You can verify this in MATLAB: | ||
<syntaxhighlight lang="matlab"> | <syntaxhighlight lang="matlab"> | ||
>> k = 0:4 | >> k = 0:4 | ||
Line 41: | Line 44: | ||
| width="50%" valign="top" | | | width="50%" valign="top" | | ||
A simple for-loop can be written as: | |||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
for k in range(5): | for k in range(5): | ||
Line 71: | Line 74: | ||
* <syntaxhighlight lang="python">>>> range(1,5)</syntaxhighlight> | * <syntaxhighlight lang="python">>>> range(1,5)</syntaxhighlight> | ||
* <syntaxhighlight lang="python">>>> range(10, -10, -2)</syntaxhighlight> | * <syntaxhighlight lang="python">>>> range(10, -10, -2)</syntaxhighlight> | ||
|} | |||
'''Key differences''' | |||
<ol> | |||
<li> | |||
* In MATLAB: you must close the loop with an '''<tt>end</tt>''' statement | |||
* In Python: you do not close the loop | |||
<li> | |||
* In MATLAB: you must end every line with a semicolon <tt>";"</tt> to prevent it showing the value of the variable. | |||
* In Python: you do not need to add semicolons; output will not be printed unless you explicitly use the '''<tt>print</tt>''' function. | |||
</ol> | |||
== <tt>while</tt> loops == | |||
Looping with a <tt>while</tt> loop is used when you '''do not know ahead of time''' how many iterations will be required. | |||
{| class="wikitable" | |||
|- | |||
! MATLAB | |||
! Python | |||
|- | |||
| width="50%" valign="top" | | |||
A simple while-loop can be written as: | |||
<syntaxhighlight lang="matlab"> | |||
x = 100; | |||
while x > 5 | |||
x = x / 2.5; | |||
disp(x) | |||
end | |||
</syntaxhighlight> | |||
and the output you should see is: | |||
<syntaxhighlight lang="text"> | |||
40 | |||
16 | |||
6.4000 | |||
2.5600 | |||
</syntaxhighlight> | |||
| width="50%" valign="top" | | |||
A similar while-loop can be written in Python as: | |||
<syntaxhighlight lang="python"> | |||
x = 100 | |||
while x > 5: | |||
x = x / 2.5 | |||
print(x) | |||
</syntaxhighlight> | |||
and you should see the output as: | |||
<syntaxhighlight lang="text"> | |||
40.0 | |||
16.0 | |||
6.4 | |||
2.56 | |||
</syntaxhighlight> | |||
You could be even more concise in Python, if you prefer: | |||
<syntaxhighlight lang="python"> | |||
x = 100 | |||
while x > 5: | |||
x /= 2.5 # note the difference | |||
print(x) | |||
</syntaxhighlight> | |||
Try the following commands in Python: | |||
<syntaxhighlight lang="python"> | |||
a = 10.0 | |||
a += 3 # what is the value of variable a? | |||
a -= 5 | |||
a *= 2 | |||
a /= 4 # and the final value of a is .... | |||
</syntaxhighlight> | |||
|} | |} |
Latest revision as of 12:43, 20 September 2010
for loops
Looping with a for loop is used when you know ahead of time how many iterations will be required.
MATLAB | Python |
---|---|
A simple for-loop can be written as: for k = 0:4
disp(k)
end
and the output should be: 0
1
2
3
4
The reason why this works is that instruction k = 0:4 creates a vector, starting at 0 and ending at 4, in steps of 1.0. You can verify this in MATLAB: >> k = 0:4
k =
0 1 2 3 4
Try looping and printing with these vectors. What is the output you expect when:
If you need help in MATLAB, you can type, for example: help colon
and it will show you how to use the colon operator. |
A simple for-loop can be written as: for k in range(5):
print(k)
and the output should be: 0
1
2
3
4
Why does the output not include the number 5? In Python, you can always check what a function does by using the help command. For example: >>> help(range)
range([start,] stop[, step]) -> list of integers
Return a list containing an arithmetic progression of integers.
range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
When step is given, it specifies the increment (or decrement).
For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!
These are exactly the valid indices for a list of 4 elements.
This shows you that the only input required to the range() function is the stop input - the other inputs, [start] and [step] have square brackets in the HELP text, indicating that they are optional inputs. Using the above HELP text, what would you expect in Python if you typed:
|
Key differences
-
- In MATLAB: you must close the loop with an end statement
- In Python: you do not close the loop
-
- In MATLAB: you must end every line with a semicolon ";" to prevent it showing the value of the variable.
- In Python: you do not need to add semicolons; output will not be printed unless you explicitly use the print function.
while loops
Looping with a while loop is used when you do not know ahead of time how many iterations will be required.
MATLAB | Python |
---|---|
A simple while-loop can be written as: x = 100;
while x > 5
x = x / 2.5;
disp(x)
end
and the output you should see is: 40
16
6.4000
2.5600
|
A similar while-loop can be written in Python as: x = 100
while x > 5:
x = x / 2.5
print(x)
and you should see the output as: 40.0
16.0
6.4
2.56
You could be even more concise in Python, if you prefer: x = 100
while x > 5:
x /= 2.5 # note the difference
print(x)
Try the following commands in Python: a = 10.0
a += 3 # what is the value of variable a?
a -= 5
a *= 2
a /= 4 # and the final value of a is ....
|