# Untitled

## 1020 days ago by pub

Wikipage: http://sagemath.wikispaces.com/Loops

There are 2 main uses of loops (repeated recycling).

• A loop for repeating a set of commands (top)
• A loop inside a command (below)

Loops for repeating a set of commands - don't forget the colon and the indent

#for loop using [m..n] counter where m and n are (usually) integers, m<=n (if m>n, the loop will be skipped). for j in [1..4]: print j print 'This line is not indented so NOT in the loop and will only print once.'
 1 2 3 4 This line is not indented so NOT in the loop and will only print once. 1 2 3 4 This line is not indented so NOT in the loop and will only print once.
#for loop using range() counter. Notice that with range(), the value of j starts at 0 and we pass through the loop 4 times. #There are 2 command lines in the loop and then a command line outside of the loop. #Each time we pass through the loop, the value of j increase by 1 via the counter; the value of x increases by 1 via the command x=x+1; the value of y does NOT change. x=-3; y=5; for j in range(4): x=x+1 print 'j=',j, 'x=',x, 'y=', y, ' still in loop' print 'This line is not indented so NOT in the loop and will only print once.'
 j= 0 x= -2 y= 5 still in loop j= 1 x= -1 y= 5 still in loop j= 2 x= 0 y= 5 still in loop j= 3 x= 1 y= 5 still in loop This line is not indented so NOT in the loop and will only print once. j= 0 x= -2 y= 5 still in loop j= 1 x= -1 y= 5 still in loop j= 2 x= 0 y= 5 still in loop j= 3 x= 1 y= 5 still in loop This line is not indented so NOT in the loop and will only print once.
#while loop is something like the "front part" of an if command #Each time we pass through the loop, the value of x increases by 2 and we print this value. #Notice that x=3 at the time of the "final" test of whether x<4. Then we increase by 2 and print its value twice, that is, once in the loop, then we exit the loop and print it again outside the loop. x=-3 while x<4 : x=x+2 print x print x
 -1 1 3 5 5 -1 1 3 5 5

Loops inside a command - usually for creating or processing through a list

#for loop to create a list of integers a=[j for j in range(15)] print a
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
#for loop with condition to create a list of even natural numbers <=20 a=[j for j in [1..20] if j/2==int(j/2)] print a
 [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
#for loop with step size in list or array a=[cos(j) for j in [0..pi,step=pi/4] ] print 'a= ', a #to make the values of this list floating decimals b=[float(cos(j)) for j in [0..pi,step=pi/4]] print 'b= ', b
 a= [1, 1/2*sqrt(2), 0, -1/2*sqrt(2), -1] b= [1.0, 0.70710678118654757, 0.0, -0.70710678118654757, -1.0] a= [1, 1/2*sqrt(2), 0, -1/2*sqrt(2), -1] b= [1.0, 0.70710678118654757, 0.0, -0.70710678118654757, -1.0]
#for loop to create (and then plot) a list of points. Notice the double parentheses after point; the outside parenthesis are for the command point() and the inside for the coordinates of the point. #if you try to print this list it will make a plot for each point. listpoints=[point((j,2*j),size=30) for j in [-2..2]] #In the above, notice the size of the point is "inside" the list. #Now to plot all the points in one plot we add the list using sum() and then use show() . s=sum(listpoints) show(s, figsize=4, aspect_ratio=1)  #for loop to create a list of values of a function starting with cos(pi/2) and increasing x by h=pi/4 (all floating decimals) #see also wikipages: Numbers, Lists, Lists+Creating var ('x') f(x)=cos(x) x0=float(pi/2); h=float(pi/4) list1=[f(x0+j*h) for j in range(5)] print list1
 [6.12323399573677e-17, -0.707106781186547, -1.00000000000000, -0.707106781186548, -1.83697019872103e-16] [6.12323399573677e-17, -0.707106781186547, -1.00000000000000, -0.707106781186548, -1.83697019872103e-16]

From the for page: http://sagemath.wikispaces.com/for

#use of for loop for cycling through a set of commands x=-3 for j in range(4) : x=x+1 print j, x, 'looping' print 'This line is not indented so NOT in the loop and will only print once.'
 0 -2 looping 1 -1 looping 2 0 looping 3 1 looping This line is not indented so NOT in the loop and will only print once. 0 -2 looping 1 -1 looping 2 0 looping 3 1 looping This line is not indented so NOT in the loop and will only print once.
#exactly the same result as above x=-3 for j in [0..3] : x=x+1 print j, x, 'looping' print 'This line is not indented so NOT in the loop and will only print once.'
 0 -2 looping 1 -1 looping 2 0 looping 3 1 looping This line is not indented so NOT in the loop and will only print once. 0 -2 looping 1 -1 looping 2 0 looping 3 1 looping This line is not indented so NOT in the loop and will only print once.
#use of for loop to create a list. The name of the list is a. a=[ j for j in range(4) ] print a
 [0, 1, 2, 3] [0, 1, 2, 3]
#we can call our counter any name not in use; here we use pop in place of j a=[ pop for pop in [0..3] ] print a
 [0, 1, 2, 3] [0, 1, 2, 3]
a=[ 2*j for j in [-2..3] if int(j/2)==j/2 ] print a
 [-4, 0, 4] [-4, 0, 4]
a=sum([ point( (2*j,j), size=20) for j in [-2..2] ] ) show(a, figsize=4, aspect_ratio=1)  for j in [1..3] : print j*j print 'Done'
 1 4 9 Done 1 4 9 Done
#Be careful of your indentations! This program will not work because the last statement: print 'Done' has not been correctly matched. for j in [1..3] : print j*j print 'Done'
 Traceback (click to the left of this block for traceback) ... IndentationError: unindent does not match any outer indentation level Traceback (most recent call last): File "", line 1, in File "_sage_input_14.py", line 10, in exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("I0JlIGNhcmVmdWwgb2YgeW91ciBpbmRlbnRhdGlvbnMhIFRoaXMgcHJvZ3JhbSB3aWxsIG5vdCB3b3JrIGJlY2F1c2UgdGhlIGxhc3Qgc3RhdGVtZQpmb3IgaiBpbiBbMS4uM10gOiAKICAgIHByaW50IGoqagogcHJpbnQgJ0RvbmUn"),globals())+"\\n"); execfile(os.path.abspath("___code___.py")) File "", line 1, in File "/tmp/tmpzwcXnb/___code___.py", line 5 print 'Done' ^ IndentationError: unindent does not match any outer indentation level
#Here we have range(start,end+1,stepsize) a=[j for j in range(1,5,2)] print a
 [1, 3] [1, 3]
#Here we have nothing since the start=5, end+1=5 so we are done before we start. a=[j for j in range(5,5)] print a
 [] []
list1=[ j for j in [(2.5)..(7.5)] ] print 'list1= ', list1
 list1= [2.50000000000000, 3.50000000000000, 4.50000000000000, 5.50000000000000, 6.50000000000000, 7.50000000000000] list1= [2.50000000000000, 3.50000000000000, 4.50000000000000, 5.50000000000000, 6.50000000000000, 7.50000000000000]
list1=[ cos(j) for j in [0..2*pi,step=pi/4] ] print 'list1= ', list1
 list1= [1, 1/2*sqrt(2), 0, -1/2*sqrt(2), -1, -1/2*sqrt(2), 0, 1/2*sqrt(2), 1] list1= [1, 1/2*sqrt(2), 0, -1/2*sqrt(2), -1, -1/2*sqrt(2), 0, 1/2*sqrt(2), 1]
list1=[ float(cos(j)) for j in [0..2*pi,step=pi/4] ] print 'list1= ', list1
 list1= [1.0, 0.70710678118654757, 0.0, -0.70710678118654757, -1.0, -0.70710678118654757, 0.0, 0.70710678118654757, 1.0] list1= [1.0, 0.70710678118654757, 0.0, -0.70710678118654757, -1.0, -0.70710678118654757, 0.0, 0.70710678118654757, 1.0]
list1=[ j for j in [1..5,step=2] ] print 'list1= ', list1
 list1= [1, 3, 5] list1= [1, 3, 5]

From the while page: http://sagemath.wikispaces.com/while

x=1; y=3 while x<3 : x=x+1 print x, y, 'looping' print 'This line is not indented so NOT in the loop and will only print once.'
 2 3 looping 3 3 looping This line is not indented so NOT in the loop and will only print once. 2 3 looping 3 3 looping This line is not indented so NOT in the loop and will only print once.