473,624 Members | 2,543 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1275
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******** *************** ***********@w7g 2000hsa.googleg roups.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.e duwrote:
"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
2000
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 msg that im getting: "Error executing cl.exe" this is the code that I have, but I know whats causing it, ill just show you the whole thing first though //file: Quadratic
105
5292
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 not work. It only works on lists and strings, for no obvious reason. Why not on all sequence types? Or, another example, the index() method has start and end parameters for lists and strings. The count() method also has start and end parameters...
7
2172
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; score=8 mean=9.67 and deviation=-1.67 now I want to square the deviation with sqrt but that doesn't really work anymore since the value is negative, what function could I use
5
2410
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 number in its respective list. Print the lists of even and odd numbers at the end of the program. You must use a while loop." This is my code and algorythm: #create empty lists to hold even and odd numbers even = "" odd = "" #create a message...
0
8179
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8633
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8348
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7176
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4187
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1797
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.