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) :

#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) :

#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

#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

#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

Home -> Control -> Loops ->

CountersBasic syntax:in range(n)orin [s..t]wherenis a positive integer andsandtare numberss≤t.suntil it is >tincrementing by 1. Options includestep sizeandcondition. (Maybe others.)listname- "counts" through the list namedlistname## EXAMPLES ON MY SAGE PAGE: Loops in Sage

in range(n) counts from 0 to n-1 (inclusive) incrementing by 1Example 1- counting in a loop forrepeating a set of commands#The argument of range() must be a positive integer. Here n=4

for j in range(4) :

Result:1

2

3

Done (see bottom of My Sage Page)

Example 2 - counting in a loop inside a commandlist1=[j for j in range(4)]

print 'list1= ', list1

Result:Example 3: range(start, end+1, stepsize)startandend+1. The third is thestepsize.#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) :

Result:3

Done (see bottom of My Sage Page)

in [s..t] counts starting fromsuntil it is >tincrementing by 1Example 4- counting in a loop forrepeating a set of commandss≤t. Here s=1, t=3.for j in [1..3] :

Result:4

9

Done (see bottom of My Sage Page)

Example 5 - loop inside a commandlist1=[ j for j in [(2.5)..(7.5)] ]

print 'list1= ', list1

Result:Example 6: [s..t, step=w]step=and then a number w.list1=[ cos(j) for j in [0..2*pi, step=pi/4] ]

print 'list1= ', list1

Result:Example 7list1=[ float(cos(j)) for j in [0..2*pi, step=pi/4] ]

print 'list1= ', list1

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

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