Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   print() a list (http://www.velocityreviews.com/forums/t697215-print-a-list.html)

DarkBlue 09-05-2009 05:53 AM

print() a list
 
I am trying to get used to the new print() syntax prior to installing
python 3.1:

test=[["VG", "Virgin Islands, British"],["VI", "Virgin Islands, U.S."],
["WF", "Wallis and Futuna"],["EH", "Western Sahara"],["YE", "Yemen"],
["ZM", "Zambia"],["ZW", "Zimbabwe"],]

#old print

for z in test:
if z[0].startswith('W'):
print z[0] , z[1]

print


# new print()
# now a list would have to be printed like this to be equal to old
print ?

for z in test:
if z[0].startswith('W'):
print('%s %s') % (z[0] , z[1])

print

# this output prints the brackets etc. too, not what we want

for z in test:
if z[0].startswith('W'):
print(z[0] , z[1])

print



on python 2.6 I get following output:

WF Wallis and Futuna

WF Wallis and Futuna

('WF', 'Wallis and Futuna')


Before actually installing python 3.1 my question is if the py2to3
converter also considers this situation ?

Thanks
Db

Mark Tolonen 09-05-2009 06:35 AM

Re: print() a list
 

"DarkBlue" <pict100@gmail.com> wrote in message
news:b9c0c4ac-5f8f-4133-b928-9e55ab4b22a0@x5g2000prf.googlegroups.com...
>I am trying to get used to the new print() syntax prior to installing
> python 3.1:
>
> test=[["VG", "Virgin Islands, British"],["VI", "Virgin Islands, U.S."],
> ["WF", "Wallis and Futuna"],["EH", "Western Sahara"],["YE", "Yemen"],
> ["ZM", "Zambia"],["ZW", "Zimbabwe"],]
>
> #old print
>
> for z in test:
> if z[0].startswith('W'):
> print z[0] , z[1]
>
> print
>
>
> # new print()
> # now a list would have to be printed like this to be equal to old
> print ?
>
> for z in test:
> if z[0].startswith('W'):
> print('%s %s') % (z[0] , z[1])
>
> print
>
> # this output prints the brackets etc. too, not what we want
>
> for z in test:
> if z[0].startswith('W'):
> print(z[0] , z[1])
>
> print
>
>
>
> on python 2.6 I get following output:
>
> WF Wallis and Futuna
>
> WF Wallis and Futuna
>
> ('WF', 'Wallis and Futuna')
>
>
> Before actually installing python 3.1 my question is if the py2to3
> converter also considers this situation ?


Without the following statement, print does not work the "new" way. What
you are printing is a tuple of the two list elements.

from __future__ import print_function

test = [
["VG", "Virgin Islands, British"],
["VI", "Virgin Islands, U.S."],
["WF", "Wallis and Futuna"],
["EH", "Western Sahara"],
["YE", "Yemen"],
["ZM", "Zambia"],
["ZW", "Zimbabwe"]]

for z in test:
if z[0].startswith('Z'):
print(z[0],z[1])
print()

----results----
ZM Zambia
ZW Zimbabwe

Comment out "from __future__ import print_function" and you'll get:



Mark Tolonen 09-05-2009 06:38 AM

Re: print() a list
 

"DarkBlue" <pict100@gmail.com> wrote in message
news:b9c0c4ac-5f8f-4133-b928-9e55ab4b22a0@x5g2000prf.googlegroups.com...
>I am trying to get used to the new print() syntax prior to installing
> python 3.1:
>
> test=[["VG", "Virgin Islands, British"],["VI", "Virgin Islands, U.S."],
> ["WF", "Wallis and Futuna"],["EH", "Western Sahara"],["YE", "Yemen"],
> ["ZM", "Zambia"],["ZW", "Zimbabwe"],]
>
> #old print
>
> for z in test:
> if z[0].startswith('W'):
> print z[0] , z[1]
>
> print
>
>
> # new print()
> # now a list would have to be printed like this to be equal to old
> print ?
>
> for z in test:
> if z[0].startswith('W'):
> print('%s %s') % (z[0] , z[1])
>
> print
>
> # this output prints the brackets etc. too, not what we want
>
> for z in test:
> if z[0].startswith('W'):
> print(z[0] , z[1])
>
> print
>
>
>
> on python 2.6 I get following output:
>
> WF Wallis and Futuna
>
> WF Wallis and Futuna
>
> ('WF', 'Wallis and Futuna')
>
>
> Before actually installing python 3.1 my question is if the py2to3
> converter also considers this situation ?


You need the following statement to use print() in Python 2.6:

from __future__ import print_function

test = [
["VG", "Virgin Islands, British"],
["VI", "Virgin Islands, U.S."],
["WF", "Wallis and Futuna"],
["EH", "Western Sahara"],
["YE", "Yemen"],
["ZM", "Zambia"],
["ZW", "Zimbabwe"]]

for z in test:
if z[0].startswith('Z'):
print(z[0],z[1])
print()

----result----
ZM Zambia
ZW Zimbabwe

-Mark



Dero 09-05-2009 06:51 AM

Re: print() a list
 
On Sep 5, 2:35*pm, "Mark Tolonen" <metolone+gm...@gmail.com> wrote:
> "DarkBlue" <pict...@gmail.com> wrote in message
>
> news:b9c0c4ac-5f8f-4133-b928-9e55ab4b22a0@x5g2000prf.googlegroups.com...
>
>
>
> >I am trying to get used to the new print() syntax prior to installing
> > python 3.1:

>
> > test=[["VG", "Virgin Islands, British"],["VI", "Virgin Islands, U.S."],
> > ["WF", "Wallis and Futuna"],["EH", "Western Sahara"],["YE", "Yemen"],
> > ["ZM", "Zambia"],["ZW", "Zimbabwe"],]

>
> > #old print

>
> > for z in test:
> > *if z[0].startswith('W'):
> > * * print z[0] , *z[1]

>
> > print

>
> > # new print()
> > # now a list would have to be printed like this to be equal to old
> > print ?

>
> > for z in test:
> > *if z[0].startswith('W'):
> > * * print('%s %s') % (z[0] , *z[1])

>
> > print

>
> > # this output prints the brackets etc. too, not what we want

>
> > for z in test:
> > if z[0].startswith('W'):
> > * *print(z[0] , z[1])

>
> > print

>
> > on python 2.6 I get following output:

>
> > WF Wallis and Futuna

>
> > WF Wallis and Futuna

>
> > ('WF', 'Wallis and Futuna')

>
> > Before actually installing python 3.1 my question is if the py2to3
> > converter also considers this situation ?

>
> Without the following statement, print does not work the "new" way. *What
> you are printing is a tuple of the two list elements.
>
> from __future__ import print_function
>
> test = [
> * * ["VG", "Virgin Islands, British"],
> * * ["VI", "Virgin Islands, U.S."],
> * * ["WF", "Wallis and Futuna"],
> * * ["EH", "Western Sahara"],
> * * ["YE", "Yemen"],
> * * ["ZM", "Zambia"],
> * * ["ZW", "Zimbabwe"]]
>
> for z in test:
> * if z[0].startswith('Z'):
> * * *print(z[0],z[1])
> print()
>
> ----results----
> ZM Zambia
> ZW Zimbabwe
>
> Comment out "from __future__ import print_function" and you'll get:


Thank you.

I thought in 2.6 both print and print() were equally implemented
without the future import requirement.

AggieDan04 09-05-2009 09:33 AM

Re: print() a list
 
On Sep 5, 1:51*am, Dero <pict...@gmail.com> wrote:
> On Sep 5, 2:35*pm, "Mark Tolonen" <metolone+gm...@gmail.com> wrote:
>
> > "DarkBlue" <pict...@gmail.com> wrote in message

>
> >news:b9c0c4ac-5f8f-4133-b928-9e55ab4b22a0@x5g2000prf.googlegroups.com...

>
> > >I am trying to get used to the new print() syntax prior to installing
> > > python 3.1:

....
> > Without the following statement, print does not work the "new" way. *What
> > you are printing is a tuple of the two list elements.

>
> > from __future__ import print_function

....
> I thought in 2.6 both print and print() were equally implemented
> without the future import requirement.


That couldn't be done because the print() syntax can't be
distinguished from old-style print with a tuple.

~$ python2.6 -c "print(1, 2)"
(1, 2)
~$ python3.0 -c "print(1, 2)"
1 2


All times are GMT. The time now is 08:28 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.