Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

The range function creates an enumerator which yields numbers.
The join function in this case, will create a string from an enumerator.

Code Block
borderStylesolid
titlejoin and range example
borderStylesolid
join(range(5))
join(range(3, 7))
join(range(0, 10, 2))
No Format
bgColor#D8DDE9
titleOutput
borderStylesolid
titleOutput
0 1 2 3 4
3 4 5 6
0 2 4 6 8

...

To be used in a for loop is quite easy.

Code Block
borderStylesolid
titlefor loop
borderStylesolid
for i in range(5):
    print i
No Format
bgColor#D8DDE9
titleOutput
borderStylesolid
titleOutput
0
1
2
3
4
Info
titlePractically as fast as C#'s

The range function does not create an array holding all the values called, instead it is an IEnumerator, that will quickly generate the numbers you need.

...

The while loop is very similar to an if statement, except that it will repeat itself as long as its condition is true.

Code Block
borderStylesolid
titlewhile loop
borderStylesolid
i = 0
while i < 5:
    print i
    i += 1
No Format
bgColor#D8DDE9
titleOutput
borderStylesolid
titleOutput
0
1
2
3
4

In case you didn't guess, i += 1 adds 1 to i.

...

The continue keyword is used when looping. It will cause the position of the code to return to the start of the loop (as long as the condition still holds).

Code Block
borderStylesolid
titlecontinue statement
borderStylesolid
for i in range(10):
    continue if i % 2 == 0
    print i
No Format
bgColor#D8DDE9
titleOutput
borderStylesolid
titleOutput
1
3
5
7
9

This skips the print part of this loop whenever i is even, causing only the odds to be printed out.
The i % 2 actually takes the remainder of i / 2, and checks it against 0.

...

the while-break-unless loop is very similar to other languages do-while statement.

Code Block
borderStylesolid
titlewhile-break-unless loop
borderStylesolid
i = 10
while true:
    print i
    i -= 1
    break unless i < 10 and i > 5
No Format
bgColor#D8DDE9
titleOutput
borderStylesolid
titleOutput
10
9
8
7
6
5

Normally, this would be a simple while loop.
This is a good method of doing things if you want to accomplish something at least once or have the loop set itself up.

...

The pass keyword is useful if you don't want to accomplish anything when defining a code block.

Code Block
borderStylesolid
titlewhile-break-unless loop
borderStylesolid
while true:
    pass //Wait for keyboard interrupt (ctrl-C) to close program.

...