Noob questions about Python

Bruno Desthuilliers bdesth.quelquechose at
Wed Oct 17 22:05:36 CEST 2007

Ixiaus a écrit :
> I have recently (today) just started learning/playing with Python. So
> far I am excited and impressed

Welcome onboard then !-)

> (coming from PHP background).
> I have a few questions regarding Python behavior...
> val = 'string'
> li = list(val)
> print li.reverse()
> returns nothing, but,
> val = 'string'
> li = list(val)
> li.reverse()
> print li
> returns what I want. Why does Python do that?

list.reverse (like list.sort) is a destructive in-place operation. Not 
returning the object is reminder of the destructive nature of the 
operation. That's a design choice, whether you agree with it or not 
(FWIW, I don't, but I live with it !-)

Note that there's also the reverse() function that returns a reverse 
iterator over any sequence, so you could also do:

li = list('allo')
print ''.join(reverse(li))

> Also I have been playing around with Binary math and noticed that
> Python treats:
> val = 00110
> as the integer 72 instead of returning 00110, why does Python do that?

Literal integers starting with '0' (zero) are treated as octal. It's a 
pretty common convention (like 0x for hexa). FWIW, PHP does just the 
same thing.

> (and how can I get around it?)

You can't. Python has no literal notation for binary integers so far. 
Literal notation for binary ints is not a common feature anyway.


More information about the Python-list mailing list