[Tutor] Getting range to produce float lists

William Griffin WILLIAM.GRIFFIN@asu.edu
Wed, 17 Apr 2002 15:23:09 -0700


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

--Boundary_(ID_2sQTZWMWgVFWyHn5T2DnkA)
Content-type: text/plain;	charset="iso-8859-1"

It is possible to use Numeric's arange; for example:
(after importing Numeric *)
>>> for i in arange(0.1,0.8,0.1):
... 	print i
... 	
0.1
0.2
0.3
0.4
0.5
0.6
0.7
>>> 
 or in your case ---

>>> for i in arange(0, 0.5, 0.1):
... 	print i
... 	
0.0
0.1
0.2
0.3
0.4
>>> 

Hope this helps.
Bill

-----Original Message-----
From: Michael Williams [mailto:michael.williams@st-annes.oxford.ac.uk]
Sent: Wednesday, April 17, 2002 9:53 AM
To: tutor@python.org
Subject: [Tutor] Getting range to produce float lists


I'm writing a tutorial introducing scientific programming to first year
Physics undergraduates and have got to the bit where I explain for loops
and, by implication range(). It is a common requirement to want to step
the loop in fractional increments, e.g.

[0.0, 0.1, 0.2, 0.3, 0.4]

I would like to do this by doing range(0, 0.5, 0.1) but of course range
only produces integer lists and will round all the arguments (making the
step = 0 if required). Is there a way to do this easily with core
Python or should I hack up a quick function for my students to use?
-- 
Michael


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

--Boundary_(ID_2sQTZWMWgVFWyHn5T2DnkA)
Content-type: text/html;	charset="iso-8859-1"
Content-transfer-encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Diso-8859-1">
<META NAME=3D"Generator" CONTENT=3D"MS Exchange Server version =
5.5.2655.35">
<TITLE>RE: [Tutor] Getting range to produce float lists</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=3D2>It is possible to use Numeric's arange; for =
example:</FONT>
<BR><FONT SIZE=3D2>(after importing Numeric *)</FONT>
<BR><FONT SIZE=3D2>&gt;&gt;&gt; for i in arange(0.1,0.8,0.1):</FONT>
<BR><FONT SIZE=3D2>... &nbsp;&nbsp;&nbsp; print i</FONT>
<BR><FONT SIZE=3D2>... &nbsp;&nbsp;&nbsp; </FONT>
<BR><FONT SIZE=3D2>0.1</FONT>
<BR><FONT SIZE=3D2>0.2</FONT>
<BR><FONT SIZE=3D2>0.3</FONT>
<BR><FONT SIZE=3D2>0.4</FONT>
<BR><FONT SIZE=3D2>0.5</FONT>
<BR><FONT SIZE=3D2>0.6</FONT>
<BR><FONT SIZE=3D2>0.7</FONT>
<BR><FONT SIZE=3D2>&gt;&gt;&gt; </FONT>
<BR><FONT SIZE=3D2>&nbsp;or in your case ---</FONT>
</P>

<P><FONT SIZE=3D2>&gt;&gt;&gt; for i in arange(0, 0.5, 0.1):</FONT>
<BR><FONT SIZE=3D2>... &nbsp;&nbsp;&nbsp; print i</FONT>
<BR><FONT SIZE=3D2>... &nbsp;&nbsp;&nbsp; </FONT>
<BR><FONT SIZE=3D2>0.0</FONT>
<BR><FONT SIZE=3D2>0.1</FONT>
<BR><FONT SIZE=3D2>0.2</FONT>
<BR><FONT SIZE=3D2>0.3</FONT>
<BR><FONT SIZE=3D2>0.4</FONT>
<BR><FONT SIZE=3D2>&gt;&gt;&gt; </FONT>
</P>

<P><FONT SIZE=3D2>Hope this helps.</FONT>
<BR><FONT SIZE=3D2>Bill</FONT>
</P>

<P><FONT SIZE=3D2>-----Original Message-----</FONT>
<BR><FONT SIZE=3D2>From: Michael Williams [<A =
HREF=3D"mailto:michael.williams@st-annes.oxford.ac.uk">mailto:michael.wi=
lliams@st-annes.oxford.ac.uk</A>]</FONT>
<BR><FONT SIZE=3D2>Sent: Wednesday, April 17, 2002 9:53 AM</FONT>
<BR><FONT SIZE=3D2>To: tutor@python.org</FONT>
<BR><FONT SIZE=3D2>Subject: [Tutor] Getting range to produce float =
lists</FONT>
</P>
<BR>

<P><FONT SIZE=3D2>I'm writing a tutorial introducing scientific =
programming to first year</FONT>
<BR><FONT SIZE=3D2>Physics undergraduates and have got to the bit where =
I explain for loops</FONT>
<BR><FONT SIZE=3D2>and, by implication range(). It is a common =
requirement to want to step</FONT>
<BR><FONT SIZE=3D2>the loop in fractional increments, e.g.</FONT>
</P>

<P><FONT SIZE=3D2>[0.0, 0.1, 0.2, 0.3, 0.4]</FONT>
</P>

<P><FONT SIZE=3D2>I would like to do this by doing range(0, 0.5, 0.1) =
but of course range</FONT>
<BR><FONT SIZE=3D2>only produces integer lists and will round all the =
arguments (making the</FONT>
<BR><FONT SIZE=3D2>step =3D 0 if required). Is there a way to do this =
easily with core</FONT>
<BR><FONT SIZE=3D2>Python or should I hack up a quick function for my =
students to use?</FONT>
<BR><FONT SIZE=3D2>-- </FONT>
<BR><FONT SIZE=3D2>Michael</FONT>
</P>
<BR>

<P><FONT =
SIZE=3D2>_______________________________________________</FONT>
<BR><FONT SIZE=3D2>Tutor maillist&nbsp; -&nbsp; Tutor@python.org</FONT>
<BR><FONT SIZE=3D2><A =
HREF=3D"http://mail.python.org/mailman/listinfo/tutor" =
TARGET=3D"_blank">http://mail.python.org/mailman/listinfo/tutor</A></FON=
T>
</P>

</BODY>
</HTML>=

--Boundary_(ID_2sQTZWMWgVFWyHn5T2DnkA)--