472,378 Members | 1,487 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,378 software developers and data experts.

simple beginner question about lists and negative index

This program:

s = 'abcde'
i = -1
for i in range (-1, -len(s), -1):
print s[:i], i

gives

abcd -1
abc -2
ab -3
a -4

Why doesn't the first one have the e if -1 is the end of the list? In
Dive Into Python it said that -1 was the end of the list. Thanks.

it is from Chun's book, slightly modified by me to see the index.
Jun 27 '08 #1
3 1207
On May 1, 10:59*am, jmDesktop <needin4mat...@gmail.comwrote:
This program:

s = 'abcde'
i = -1
for i in range (-1, -len(s), -1):
* * print s[:i], i

gives

abcd -1
abc -2
ab -3
a -4

Why doesn't the first one have the e if -1 is the end of the list? *In
Dive Into Python it said that -1 was the end of the list. *Thanks.

it is from Chun's book, slightly modified by me to see the index.
Sorry. It's because of the : right? up to but not including. Dive
into was just list[-1] not list[:-1]. I looked at the for while
before posting and then as soon as I posted I saw it.
Jun 27 '08 #2

"jmDesktop" <ne***********@gmail.comwrote in message
news:c7**********************************@w7g2000h sa.googlegroups.com...
| This program:
|
| s = 'abcde'
| i = -1
| for i in range (-1, -len(s), -1):
| print s[:i], i
|
| gives
|
| abcd -1
| abc -2
| ab -3
| a -4
|
| Why doesn't the first one have the e if -1 is the end of the list? In
| Dive Into Python it said that -1 was the end of the list. Thanks.

A sequence with n items has n+1 slice positions, numbered 0 to n: the 2 at
beginning and end and n-1 between items. Example
-a-b-c-
0 1 2 3
has 4 slice positions.
Hence the first item is seq[0:1] and last is seq[n-1:n]

In a sense, we 'ought' to index sequences with average of two successive
slice positions, giving seq[1/2],,,seq[n-1/2].
But this is inconvenient, so we either round down (C, Python, etc) or up
(Fortran), giving seq[0],,,,seq[n-1] or seq[1],,,seq[n].
Python allows n-1 and n-k to be abbreviated as -1 and -k.

-1 as an abbreviation of n-1 is only the end of the list for indexing.
n is the end for slicing. It is abbreviated by omission. Perhaps

for i in range(n+1): print i, 'abcde'[:5-i]

will make this all even clearer.

Terry Jan Reedy


Jun 27 '08 #3
On May 1, 10:04 am, "Terry Reedy" <tjre...@udel.eduwrote:
"jmDesktop" <needin4mat...@gmail.comwrote in message
|
| s = 'abcde'
| i = -1
| for i in range (-1, -len(s), -1):
| print s[:i], i
| Why doesn't the first one have the e if -1 is the end of the list?
That should be obvious. 'print s[-1]' will yield the "e", but your
code is using slicing, s[:i], not printing elements from a list. Your
code starts with the value s[:-1], which is exactly the same as
s[0:-1], so you print the string from 0 to -1 which omits the last
letter. The next time it omits the last 2 letters s[:-2]. As already
stated, that should be very obvious. Also, if you want a response in
the future, make your code readable. That means not using, "i", "l",
or "o" as variable names. They look too much like numbers and some
people won't waste their time trying to distinguish between them.
There are 23 other letters that work just a well.
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Bshealey786 | last post by:
Okay im doing my final project for my first computer science class(its my major, so it will be my first of many), but anyway im a beginner so im not to great with C++ yet. Anyway this is the error...
105
by: Christoph Zwerschke | last post by:
Sometimes I find myself stumbling over Python issues which have to do with what I perceive as a lack of orthogonality. For instance, I just wanted to use the index() method on a tuple which does...
7
by: Jeremy | last post by:
hello, I am trying to calculate the standard deviation with my program, but I ran into a small problem with squaring negative value's. Which cannot be done.. Ehm let me explain let's say; ...
5
by: imish06 | last post by:
This is the question: "The user must enter a non-negative number to add to the lists or a negative number to stop the program. For every non-negative number entered, place each even and odd...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.