Page 198 - Data Science Algorithms in a Week
P. 198
Python Reference
Output:
$ python example11_for_loop_list.py
The first 6 primes are:
2
3
5
7
11
13
Break and continue
For loops can be exited earlier with the statement break. The rest of the cycle in the for loop
can be skipped with the statement continue.
Input:
source_code/appendix_c_python/example12_break_continue.py
for i in range(0,10):
if i % 2 == 1: #remainder from the division by 2
continue
print 'The number', i, 'is divisible by 2.'
for j in range(20,100):
print j
if j > 22:
break;
Output:
$ python example12_break_continue.py
The number 0 is divisible by 2.
The number 2 is divisible by 2.
The number 4 is divisible by 2.
The number 6 is divisible by 2.
The number 8 is divisible by 2.
20
21
22
23
[ 186 ]