473,549 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python + strange == cool

All fine and good <below>
x = ["list1", "list2", "list3", 4, 5, 6, 7]
x[1:-1] ['list2', 'list3', 4, 5, 6] # All good here

this struck me as weird I had an idea to mess around with lists <below>
x[1:+1] []

now when I do this x[1:+2]

['list2']

Does this puzzle anyone else

of course I am a total novice so I have no idea what I am doing here if
anyone has any ideas please reply
Jul 18 '05 #1
4 976
The second number in the slice is the index of the last element to be
included, exclusive. It is not an offset from the first index. Adding
the "+" does nothing; that just means "positive". As the laws of
mathematics dictate, "+n = n"; the unary + is just for symmetry with the
unary '-'.

On Tue, Sep 28, 2004 at 04:21:12PM -0700, Maboroshi wrote:
All fine and good <below>
x = ["list1", "list2", "list3", 4, 5, 6, 7]
x[1:-1] ['list2', 'list3', 4, 5, 6] # All good here

this struck me as weird I had an idea to mess around with lists <below>
x[1:+1] []

now when I do this x[1:+2]

['list2']

Does this puzzle anyone else

of course I am a total novice so I have no idea what I am doing here if
anyone has any ideas please reply

Jul 18 '05 #2
Maboroshi wrote:
All fine and good <below>
>>> x = ["list1", "list2", "list3", 4, 5, 6, 7]
>>> x[1:-1] ['list2', 'list3', 4, 5, 6] # All good here

this struck me as weird I had an idea to mess around with lists <below>
>>> x[1:+1] []

now when I do this >>> x[1:+2]

['list2']

Does this puzzle anyone else

of course I am a total novice so I have no idea what I am doing here if
anyone has any ideas please reply


Look at section 3.1.2 of the Python tutorial (look at the whole tutorial if you
haven't already ;-) ). The part explaining the above starts like this:

"The best way to remember how slices work is to think of the indices as pointing
between characters..."

(note that 3.1.2 is talking about strings, but lists, tuples, and strings are
all sequences and share many properties. Also, working with strings might make
it easier to grasp what's going on, and once you've got those down then applying
the principles to lists and tuples will be a breeze)

-Dave

Jul 18 '05 #3
Ok that makes sense

Cheers


Phil Frost wrote:
The second number in the slice is the index of the last element to be
included, exclusive. It is not an offset from the first index. Adding
the "+" does nothing; that just means "positive". As the laws of
mathematics dictate, "+n = n"; the unary + is just for symmetry with the
unary '-'.

On Tue, Sep 28, 2004 at 04:21:12PM -0700, Maboroshi wrote:
All fine and good <below>
>x = ["list1", "list2", "list3", 4, 5, 6, 7]
>x[1:-1]


['list2', 'list3', 4, 5, 6] # All good here

this struck me as weird I had an idea to mess around with lists <below>
>x[1:+1]


[]

now when I do this
>x[1:+2]


['list2']

Does this puzzle anyone else

of course I am a total novice so I have no idea what I am doing here if
anyone has any ideas please reply

Jul 18 '05 #4
On Tue, 28 Sep 2004 19:30:35 -0400, Phil Frost <in****@bitglue .com>
wrote:
the unary + is just for symmetry with the
unary '-'.


I was bitten in my first python program by writing

++x

:-)

Andrea
Jul 18 '05 #5

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

Similar topics

220
18847
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it...
54
6517
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO FRICKIN' COOL!!! ***MAN*** that would save me a buttload of work and make my life sooooo much easier!" As opposed to minor differences of this feature...
105
5087
by: Peter Hickman | last post by:
Well after all this discussion it would appear that a 'Python like' language has appeared => Prothon. http://www.prothon.org/index.html Very alpha, sort of like Python (if you consider the indenting is what makes Python unique) and sort of Ruby in its use of prefixes to define scoping etc (although there is no reference to this trait being...
4
1844
by: Vent d'Est - East Wind | last post by:
Can somebody tell me what s exactly the difference beetween scheme and python languages i m thiking to learn one of them but i m not sure wich one somebody told me python is better and have much library somebody told me scheme is much smarter and have nice structure i also see a project about a scheme/python implementation here is the...
40
1296
by: Shufen | last post by:
Hi all, Can someone who has use PHP before and know quite well about the language, tell me what are the stuffs that Python offers and PHP doesn't. A few examples will be nice. I know about the date format problem which PHP is having but I need more examples. Thank you for any help. Shufen
114
9716
by: Maurice LING | last post by:
This may be a dumb thing to ask, but besides the penalty for dynamic typing, is there any other real reasons that Python is slower than Java? maurice
4
1322
by: friedmud | last post by:
In trying to construct a good object model in a recent project of mine, I ran across the following peculiarity in python 2.3.4 (haven't tried any newer versions): Say you have a base class that has an attribute and an accessor function for that attribute (just a simple get). Then you inherit that base class to make a sub class and the...
6
3738
by: cournape | last post by:
Hi there, I have some scientific application written in python. There is a good deal of list processing, but also some "simple" computation such as basic linear algebra involved. I would like to speed things up implementing some of the functions in C. So I need profiling. I first tried to use the default python profiler, but profiling my...
29
2194
by: Mike Meyer | last post by:
After spending time I should have been sleeping working on it, the try python site is much more functional. It now allows statements, including multi-line statements and expressions. You can't create code objects yet, so it's still more a programmable calculator than anything real. I've got some of the tutorial text (literally) up as well....
13
3671
by: iu2 | last post by:
Hi, This is a little bit strange post, but I'm curious... I learned Python from its tutorial step by step, and practicing writing small scripts. I haven't seen a Python program before knowing Python. I'm curious, what did Python code look like to those of you who have seen a bunch of Python code for the first time before knowing Python?
0
7550
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7746
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7836
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...
0
5116
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...
0
3521
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3502
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1970
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1084
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
789
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.