[Tutor] Adding line numbers to a Python Script

2010-06-12 Thread Sudip Bhattacharya
I quote this example (and the problem that I am facing trying to understand)
from the book "Beginning Python - From Novice to Professional" by Magnus Lie
Hetland

Page 227

# numberlines.py
import fileinput(1)

for line in fileinput.input(inplace = true)(2)
   line = line.rstrip()(3)
   num=fileinput.lineno()..(4)
   print '%-40s #%2i' %(line,num).(5)


Question: Please help me understand:

Line (3) - Are we stripping the entire line of code first, inserting the
code at the end (line (5)) and then placing it back at the same place ?

Line (5) - What does the individual characters in the expression "Print
'%-40s #%2i' %(line,num)" mean ?

 - '-40s' : is this the space [what does "-40s" mean] that we are
creating post - which the line nos is inserted "#%2i" ?
 - What does '%2" mean ? - Is that 2 cursor spaces of width ?

Much thanks in advance...
-- 
Thanks and regards,
Sudip Bhattacharya

Mobile: +91  100 706
Home Land line: +91 11 22237561
Office Land line: +91 0124 4321078
eMail ID: sud...@sudipb.com; sud...@gmail.com

Please visit my website at: www.sudipb.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Tuple - Immutable ?

2012-03-08 Thread Sudip Bhattacharya
>>> s=(1,2,3)
>>> s=s+(4,5,6)
>>>s
(1,2,3,4,5,6)

The tuple has changed.

I thought I read that tuples are sequences (like lists), but they are
immutable - They can't be changed once created. Could someone explain
please ?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tuple - Immutable ?

2012-03-08 Thread Sudip Bhattacharya
That makes perfect sense. What happened was that the old tuple got replaced
with a new tuple (old + new items) and NOT changed cause tuple is
immutable.

Thanks HTH.

On Thu, Mar 8, 2012 at 4:53 PM, col speed  wrote:

> On 8 March 2012 18:11, Sudip Bhattacharya  wrote:
> >>>> s=(1,2,3)
> >>>> s=s+(4,5,6)
> >>>>s
> > (1,2,3,4,5,6)
> >
> > The tuple has changed.
> >
> > I thought I read that tuples are sequences (like lists), but they are
> > immutable - They can't be changed once created. Could someone explain
> please
> > ?
>
> I'm just a noob, but as nobody else has replied yet..
> As far as I *think* I know:
> s=s+(4,5,6) creates a new tuple, it doesn't change it.
> Tuples are immutable as in you can't add or append and stuff.
> Also:
>
> >>> s[0] = 8
> Traceback (most recent call last):
>  File "", line 1, in 
> TypeError: 'tuple' object does not support item assignment
> Nor does it have attribute "reverse" or anything like that.
>
> HTH
> Col
>



-- 
Thanks and regards,
Sudip Bhattacharya

Mobile: +91  100 706
Home Land line: +91 11 22237561
Office Land line: +91 0124 4321078
eMail ID: sud...@sudipb.com; sud...@gmail.com

Please visit my website at: www.sudipb.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor