Home -> Control -> Loops -> Counters


Basic syntax: in range(n)   or   in [s..t]   where n is a positive integer and s and t are numbers st.

  • used in for loops
  • [s..t] counts starting from s until it is >t incrementing by 1. Options include step size and condition. (Maybe others.)
  • range(n) counts from 0 to n-1 (inclusive) incrementing by 1.
  • listname - "counts" through the list named listname

EXAMPLES ON MY SAGE PAGE: Loops in Sage


  • in range(n) counts from 0 to n-1 (inclusive) incrementing by 1
    • Example 1 - counting in a loop for repeating a set of commands
      #Remember j is a "dummy" variable for doing the counting. We can call it anything we want, but don't use variable names from your program.
      #The argument of range() must be a positive integer. Here n=4
      for j in range(4) :
      print j
      print 'Done'
      Result:
      0  
      1  
      2  
      3  
      Done   (see bottom of My Sage Page)

    • Example 2 - counting in a loop inside a command
      #This creates a list (notice the brackets) called list1.
      list1=[j for j in range(4)]
      print 'list1= ', list1
      Result:
      list1= [0, 1, 2, 3]

    • Example 3: range(start, end+1, stepsize)
      #You must add a "start" integer if you want a "stepsize". That is, the first two integers are start and end+1. The third is the stepsize.
      #All three arguments must be positive integers.
      # (I don't like this because I am always confused about the end+1 thing so for counts with stepsize I use the "in" command (below).
      for j in range(1,5,2) :
      print j
      print 'Done'
      Result:
      1  
      3  
      Done   (see bottom of My Sage Page)

  • in [s..t] counts starting from s until it is >t incrementing by 1
    • Example 4 - counting in a loop for repeating a set of commands
      #st. Here s=1, t=3.
      for j in [1..3] :
      print j*j
      print 'Done'
      Result:
      1  
      4  
      9   
      Done   (see bottom of My Sage Page)

    • Example 5 - loop inside a command
      #This creates a list called list1. Outside brackets are for list, inside brackets are for counter. Parenthesis are because sage gets confused with all of the periods :)
      list1=[ j for j in [(2.5)..(7.5)] ]
      print 'list1= ', list1
      Result:
      list1= [2.50000000000000, 3.50000000000000, 4.50000000000000, 5.50000000000000, 6.50000000000000, 7.50000000000000]

    • Example 6: [s..t, step=w]
      #This creates a list using a stepsize. There is a comma and then the expression step= and then a number w.
      list1=[ cos(j) for j in [0..2*pi, step=pi/4] ]
      print 'list1= ', list1
      Result:
      list1= [1, 1/2*sqrt(2), 0, -1/2*sqrt(2), -1, -1/2*sqrt(2), 0, 1/2*sqrt(2), 1]

    • Example 7
      #With the command float() around cos() we get decimals (this is nothing to do with counters).
      list1=[ float(cos(j)) for j in [0..2*pi, step=pi/4] ]
      print 'list1= ', list1
      Result:
      list1= [1.0, 0.70710678118654757, 0.0, -0.70710678118654757, -1.0, -0.70710678118654757, 0.0, 0.70710678118654757, 1.0]

Reference: Sage_for_Newbies, pp.35-37 (Ted Kosan)



Keywords: sage, loops, while, for, control, repeating, steps, incrementing