[Tutor] trouble with stringio function in python 3.2

2015-05-04 Thread anupama srinivas murthy
Hello,

My python code needs to run on versions 2.7 to 3.4. To use stringio
function as appropriate, the code i use is;

if sys.version < '3':
dictionary = io.StringIO(u"""\n""".join(english_words))
else:
dictionary = io.StringIO("""\n""".join(english_words))

The code runs fine on all versions mentioned above except for 3.2 where i
get the error:
dictionary = io.StringIO(u"""\n""".join(english_words))
^
SyntaxError: invalid syntax

How can I solve the issue?

Thank you
Anupama
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] trouble with stringio function in python 3.2

2015-05-04 Thread anupama srinivas murthy
> On May 4, 2015 2:17 AM, "anupama srinivas murthy" <
> anupama.2312.bm...@gmail.com> wrote:
>
>> Hello,
>>
>> My python code needs to run on versions 2.7 to 3.4. To use stringio
>> function as appropriate, the code i use is;
>>
>> if sys.version < '3':
>> dictionary = io.StringIO(u"""\n""".join(english_words))
>> else:
>> dictionary = io.StringIO("""\n""".join(english_words))
>>
>> The code runs fine on all versions mentioned above except for 3.2 where i
>> get the error:
>> dictionary = io.StringIO(u"""\n""".join(english_words))
>> ^
>> SyntaxError: invalid syntax
>>
>> How can I solve the issue?
>>
>> Thank you
>> Anupama
>>
>>
>


On 4 May 2015 at 12:48, Reuben  wrote:

Did you import correct library?


Thank you for asking

Yes. I made a new modification and the code now runs fine on python 3.2 as
well. I replaced;

dictionary = io.StringIO(u"""\n""".join(english_words))

with

dictionary = io.StringIO(unicode('\n').join(english_words))

However, i do not understand why it works fine now
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Query regarding output

2017-06-29 Thread anupama srinivas murthy
for a[x] in a:
   print(a)

Except for when x = 0, a[x] never takes on its original value throughout
the iterations

So for x = 0, we get:

[0, 1, 2, 3]
[1, 1, 2, 3]
[2, 1, 2, 3]
[3, 1, 2, 3]

For x = 1:
[0, 0, 2, 3]
[0, 0, 2, 3]
[0, 2, 2, 3]
[0, 3, 2, 3]

For x = 2:
[0, 1, 0, 3]
[0, 1, 1, 3]
[0, 1, 1, 3]
[0, 1, 3, 3]

How is the array being transformed?

On 29 June 2017 at 14:32, Alan Gauld via Tutor  wrote:

> On 29/06/17 03:14, shubham goyal wrote:
>
> > This Question is asked in some exam. i am not able to figure it out.
> >
> > a = [0, 1, 2, 3]
> > for a[-1] in a:
> > print(a[-1])
> >
> > its giving output 0 1 2 2
> >
> > it should be 3 3 3 3 as a[-1] belongs to 3.
> > can anyone help me figuring it out.
>
> This is quite subtle and it took me a few minutes to figure
> it out myself.
>
> It might be clearer if we print all of 'a' instead
> of a[-1]:
>
> >>> for a[-1] in a:
> ...print(a)
> ...
> [0, 1, 2, 0]
> [0, 1, 2, 1]
> [0, 1, 2, 2]
> [0, 1, 2, 2]
>
> What is happening is that a[-1] is being assigned the value
> of each item in a in turn. The final iteration assigns a[-1]
> to itself, thus we repeat the 2.
>
> Another way to see it is to convert the for loop to
> a while loop:
>
> for variable in collection:
> process(variable)
>
> becomes
>
> collection = [some sequence]
> index = 0
> while index < len(collection):
>  variable = collection[index]
>  process(variable)
>  index += 1
>
> Now substitute your values:
>
> collection = [0,1,2,3]
> index = 0
> while index < len(collection):
>  collection[-1] = collection[index]
>  print(collection[-1])
>  index += 1
>
> Does that help?
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 

Thank you
Anupama
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor