471,356 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

['a', 'b'][True] results 'b' But how?

Hi,

Can any one please tell me how is the following code is working?
['a','b'] is a list of string, and [True] is list of boolean value.
How is it making effect....?
<code Python24>
>>['a','b] [True]
'b'
>>['a','b'] [False]
'a'
>>['a','b']['some_string' == r'some_string']
'b'
>>['a','b']['some_string' == r'somestring']
'a'

<code>
Thanks in advance,
regards,
kath.

Jul 5 '07 #1
2 864
kath wrote:
Hi,

Can any one please tell me how is the following code is working?
['a','b'] is a list of string
Yes.
and [True] is list of boolean value.
No. It's the subscription operator applied to the list of strings.
a = ['a', 'b']
a[True]
may be clearer.
How is it making effect....?
>>int(True)
1
>>int(False)
0
>>isinstance(True, int)
True
>>bool.__bases__
(<type 'int'>,)
<code Python24>
>>>['a','b] [True]
'b'
>>>['a','b'] [False]
'a'
>>>['a','b']['some_string' == r'some_string']
'b'
>>>['a','b']['some_string' == r'somestring']
'a'

<code>
--
rbh
Jul 5 '07 #2
Hi Kelvie and RBH,

Thanks for your quick reply. That was very much helpful indeed.

regards,
kath.
Jul 5 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by John Hunter | last post: by
46 posts views Thread by Scott Chapman | last post: by
43 posts views Thread by sinister | last post: by
90 posts views Thread by John Salerno | last post: by
40 posts views Thread by nufuhsus | last post: by

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.