Looping technique: while and for

For and while are means that we want to ask computer to do task repeatedly. but each with slightly different problems.


In [1]:
#just repeatedly do ten times
for i in range(0,10):
    print "*"


*
*
*
*
*
*
*
*
*
*

In [2]:
#each time print more stars
for i in range(0,10):
    print "*"*i


*
**
***
****
*****
******
*******
********
*********

In [13]:
#each time print less stars
for i in range(0,10):
    print "*"*(10-i)


**********
*********
********
*******
******
*****
****
***
**
*

In [13]:
#it could be funny
for i in range(0,10):
    print "*"*i+" "*3+"*"*(10-i)


   **********
*   *********
**   ********
***   *******
****   ******
*****   *****
******   ****
*******   ***
********   **
*********   *

In [3]:
#loop index i is not use in the loop command, nothing is changed.
a=5
for i in range(0,10):
    print "*"*a


*****
*****
*****
*****
*****
*****
*****
*****
*****
*****

In [4]:
for i in range(0,10):
    print i


0
1
2
3
4
5
6
7
8
9

In [14]:
for i in range(0,10):
    print i, i*i


0 0
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81

In [5]:
#we can iterate through i
for i in range(0,10):
    print i**0.5,i, i**2, i**3


0.0 0 0 0
1.0 1 1 1
1.41421356237 2 4 8
1.73205080757 3 9 27
2.0 4 16 64
2.2360679775 5 25 125
2.44948974278 6 36 216
2.64575131106 7 49 343
2.82842712475 8 64 512
3.0 9 81 729

In [15]:
#format is not good. use rjust() a string function.
#use str and float to convert data type to str and float

print 'square root'.rjust(15),'integer'.rjust(10), 'square'.rjust(10), 'cubic'.rjust(10)
for i in range(0,10):
    print str(float(i)**0.50).rjust(15),str(i).rjust(10), str(i**2).rjust(10), str(i**3).rjust(10)


    square root    integer     square      cubic
            0.0          0          0          0
            1.0          1          1          1
  1.41421356237          2          4          8
  1.73205080757          3          9         27
            2.0          4         16         64
   2.2360679775          5         25        125
  2.44948974278          6         36        216
  2.64575131106          7         49        343
  2.82842712475          8         64        512
            3.0          9         81        729

While loop

While loop is more flexible. But we need to

  1. initialize it before entering the loop
  2. set the continue conditions
  3. change the loop index to tigger the stop condition

In [15]:
#comparator and logic and/or
a=10
print a<20
print a<20 and a>5
print a<20 or a<12
print 12<a<20


True
True
True
False

In [17]:
a=10
while a<20:
    print a
    a=a+1


10
11
12
13
14
15
16
17
18
19

In [19]:
#if we set the wrong conditions, loop command will never be excute.
#nothing will be printed
a=10
while a==20:
    print a
    a=a+1

In [1]:
#if we do no change loop index, it will execute forever.
#following code will never stop
#do not run.
a=10
while a<20:
    print a


10
11
12
13
14
15
16
17
18
19

In [2]:
#while could be used just like a for loop
a=0
while a<10:
    print a
    a=a+1


0
1
2
3
4
5
6
7
8
9

In [3]:
#while could be used just like a for loop
#be careful while you change the loop index
a=0
while a<10:
    print a
    a=a+1
    a=a*2


0
2
6

In [11]:
#check if a is a prime
# we can use a flag to trig the exit immediately, save time
a=131
i=2
found=False
is_prime=True
while (i<a) and found==False:
    print i, " : testing......"
    if a%i==0:
        print a," = ", i, " * ",a/i
        found=True
        is_prime=False
    else:
        #print i,"can not divide ", a
        found=False
    i=i+1
#note that once a%i=0, the while loop just stop/exit

print "Prime  => ",is_prime


2  : testing......
3  : testing......
4  : testing......
5  : testing......
6  : testing......
7  : testing......
8  : testing......
9  : testing......
10  : testing......
11  : testing......
12  : testing......
13  : testing......
14  : testing......
15  : testing......
16  : testing......
17  : testing......
18  : testing......
19  : testing......
20  : testing......
21  : testing......
22  : testing......
23  : testing......
24  : testing......
25  : testing......
26  : testing......
27  : testing......
28  : testing......
29  : testing......
30  : testing......
31  : testing......
32  : testing......
33  : testing......
34  : testing......
35  : testing......
36  : testing......
37  : testing......
38  : testing......
39  : testing......
40  : testing......
41  : testing......
42  : testing......
43  : testing......
44  : testing......
45  : testing......
46  : testing......
47  : testing......
48  : testing......
49  : testing......
50  : testing......
51  : testing......
52  : testing......
53  : testing......
54  : testing......
55  : testing......
56  : testing......
57  : testing......
58  : testing......
59  : testing......
60  : testing......
61  : testing......
62  : testing......
63  : testing......
64  : testing......
65  : testing......
66  : testing......
67  : testing......
68  : testing......
69  : testing......
70  : testing......
71  : testing......
72  : testing......
73  : testing......
74  : testing......
75  : testing......
76  : testing......
77  : testing......
78  : testing......
79  : testing......
80  : testing......
81  : testing......
82  : testing......
83  : testing......
84  : testing......
85  : testing......
86  : testing......
87  : testing......
88  : testing......
89  : testing......
90  : testing......
91  : testing......
92  : testing......
93  : testing......
94  : testing......
95  : testing......
96  : testing......
97  : testing......
98  : testing......
99  : testing......
100  : testing......
101  : testing......
102  : testing......
103  : testing......
104  : testing......
105  : testing......
106  : testing......
107  : testing......
108  : testing......
109  : testing......
110  : testing......
111  : testing......
112  : testing......
113  : testing......
114  : testing......
115  : testing......
116  : testing......
117  : testing......
118  : testing......
119  : testing......
120  : testing......
121  : testing......
122  : testing......
123  : testing......
124  : testing......
125  : testing......
126  : testing......
127  : testing......
128  : testing......
129  : testing......
130  : testing......
Prime  =>  True

In [12]:
#check if a is a prime
# we can use a flag to trig the exit immediately, save time
#actually we do not have to check till a-1, sqrt(a) is enough
a=131
i=2
found=False
is_prime=True
while (i<float(a)**0.5) and found==False:
    print i, " : testing......"
    if a%i==0:
        print a," = ", i, " * ",a/i
        found=True
        is_prime=False
    else:
        #print i,"can not divide ", a
        found=False
    i=i+1
#note that once a%i=0, the while loop just stop/exit

print "Prime  => ",is_prime


2  : testing......
3  : testing......
4  : testing......
5  : testing......
6  : testing......
7  : testing......
8  : testing......
9  : testing......
10  : testing......
11  : testing......
Prime  =>  True

While and For are powerful loop tools, we can use them to do repeated operation. Pay attention to

  1. the loop index: what is actually changed during each execution.
  2. pay attention to the conditions to test.
  3. pay attention how to start and how to stop.

In [ ]:
test