Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #102032

Re: import locale and print range on same line

From Vlastimil Brom <vlastimil.brom@gmail.com>
Newsgroups comp.lang.python
Subject Re: import locale and print range on same line
Date 2016-01-23 12:12 +0100
Message-ID <mailman.175.1453547570.15297.python-list@python.org> (permalink)
References <2bda88dd-82e3-4e43-b49c-3945a0befdc2@googlegroups.com> <87zivw37ml.fsf@elektro.pacujo.net>

Show all headers | View raw


2016-01-23 11:36 GMT+01:00 Marko Rauhamaa <marko@pacujo.net>:
> raiwil@gmail.com:
>
>> Can someone tell me why next code doesn't work?
>>
>> import locale; locale.setlocale(locale.LC_ALL, ""); for i in
>> range(1,20,4): print(locale.format("%2f", i, 1))
>>
>> It gives an error: SyntaxError: invalid syntax --> indicating 'for'
>>
>> However I need to put the code on one single line.
>> When I separate them like below it works fine.
>>
>> import locale
>> locale.setlocale(locale.LC_ALL, "")
>> for i in range(1,20,4):
>>    print(locale.format("%2f", i, 1))
>
> The answer is in Python's syntax definition. Not everything is allowed
> on a single line.
>
> See <URL: https://docs.python.org/3/reference/grammar.html>
>
> Only small_stmt's can be separated by semicolons.
>
> A for statement is a compound_stmt, which is not a small_stmt.
>
>
> Marko
> --
> https://mail.python.org/mailman/listinfo/python-list

Hi,
if you realy can't use multiple lines (which is rather essencial for
python), you can circumvent this in some cases, e.g. using list
comprehension and ignoring the created list itself:
import locale; locale.setlocale(locale.LC_ALL, "");
[print(locale.format("%2f", i, 1)) for i in range(1,20,4)]

vbr

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

import locale and print range on same line raiwil@gmail.com - 2016-01-23 02:02 -0800
  Re: import locale and print range on same line Marko Rauhamaa <marko@pacujo.net> - 2016-01-23 12:36 +0200
    Re: import locale and print range on same line Vlastimil Brom <vlastimil.brom@gmail.com> - 2016-01-23 12:12 +0100
      Re: import locale and print range on same line Ramo <raiwil@gmail.com> - 2016-01-23 03:57 -0800
  Re: import locale and print range on same line Steven D'Aprano <steve@pearwood.info> - 2016-01-24 00:07 +1100
    Re: import locale and print range on same line Chris Angelico <rosuav@gmail.com> - 2016-01-24 00:19 +1100
      Re: import locale and print range on same line Steven D'Aprano <steve@pearwood.info> - 2016-01-24 00:45 +1100
        Re: import locale and print range on same line Chris Angelico <rosuav@gmail.com> - 2016-01-24 00:58 +1100
          Re: import locale and print range on same line Ramo <raiwil@gmail.com> - 2016-01-23 06:03 -0800
            Re: import locale and print range on same line Chris Angelico <rosuav@gmail.com> - 2016-01-24 01:53 +1100
        Re: import locale and print range on same line Terry Reedy <tjreedy@udel.edu> - 2016-01-23 21:45 -0500
        Re: import locale and print range on same line Chris Angelico <rosuav@gmail.com> - 2016-01-24 13:51 +1100
        Re: import locale and print range on same line eryk sun <eryksun@gmail.com> - 2016-01-24 02:00 -0600

csiph-web