#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Location
    Canada
    Posts
    242
    Rep Power
    0

    Multiple Line Expressions


    I've forgotten how to write multiple lined expressions in Python, can anyone help me out?
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Originally Posted by NetBSD
    I've forgotten how to write multiple lined expressions in Python, can anyone help me out?
    Hey net, use a \ before the line=break and Python will read the expression as one long line.

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Code:
    >>> a \
    ... = \
    ... 5
    >>> b \
    ... = 6
    >>> a,b
    (5, 6)
    >>>
    That's a pretty clever trick . Never knew about it.
  6. #4
  7. No Profile Picture
    Brony & F/OSS Advocate
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2003
    Location
    Anaheim, CA (USA)
    Posts
    6,653
    Rep Power
    2476
    Also, don't forget that Python's normally-strict rules about indentation and code flow are simply ignored for the second and further lines in such a multi-line statement. You don't necessarily have to keep the lines at the same indentation level.
    ~~ Peter ~~ :: ( Who am I? ) :: ( Peter's Musings: Uploading myself, bit by bit... ) :: ( Electronic Frontier Foundation ) :: ( I'm a GNU/Linux addict and Free Software Advocate. ) :: ( How to Ask Questions the Smart Way ) :: ( The Fedora Project, sponsored by Red Hat ) :: ( GNOME: The Free Software Desktop Project ) :: ( GnuPG Public Key ) :: ( About me on the WIki )
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Location
    Canada
    Posts
    242
    Rep Power
    0
    Ahh, thanks!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    to clarify what codergeek said, python considers anything between brackets (round, curly or square) as a single line. So all of these are valid:

    python Code:
    >>> (1 + 
    ... 2 
    ... +3)
    6
    >>> [ 1,
    ... 2, 
    ... 3,
    ... ]
    [1, 2, 3]
    >>> {1:
    ... 	2,
    ... 	3
    ... 	:
    ... 		4}
    {1: 2, 3: 4}
    >>>


    Dave
  12. #7
  13. No Profile Picture
    Brony & F/OSS Advocate
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2003
    Location
    Anaheim, CA (USA)
    Posts
    6,653
    Rep Power
    2476
    Originally Posted by DevCoach
    to clarify what codergeek said, python considers anything between brackets (round, curly or square) as a single line. So all of these are valid:
    Actually, I meant separated by backslashes:
    python Code:
    >>>if(1 == 1):
    >>>      echo "%s" \
    >>> % 4
    Output: 4
    ~~ Peter ~~ :: ( Who am I? ) :: ( Peter's Musings: Uploading myself, bit by bit... ) :: ( Electronic Frontier Foundation ) :: ( I'm a GNU/Linux addict and Free Software Advocate. ) :: ( How to Ask Questions the Smart Way ) :: ( The Fedora Project, sponsored by Red Hat ) :: ( GNOME: The Free Software Desktop Project ) :: ( GnuPG Public Key ) :: ( About me on the WIki )
  14. #8
  15. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    611
    Rep Power
    65
    Something like this:
    python Code:
    if 'h' in 'hello':
        print \
    """
    strict
    indentation
    rules are ignored
    this way
    """

    The multiline string is still part of print, even though it is not indented. Also, be careful not to add a space after the line continuation \.
    Last edited by Dietrich; July 15th, 2006 at 07:15 AM.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Location
    Finland
    Posts
    260
    Rep Power
    21
    \ is handy and works also in C++
    One more example

    python Code:
     
    >>> print "I \
    ... am \
    ... one \
    ... row."
    I am one row.
  18. #10
  19. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    I'm kinda surprised there's so much to say about this , just goes to show the level of detail programmers like I guess.
    programming language development: www.netytan.com Hula

  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Location
    Canada
    Posts
    242
    Rep Power
    0
    Originally Posted by netytan
    I'm kinda surprised there's so much to say about this , just goes to show the level of detail programmers like I guess.
    Hehe, I was thinking the same thing

IMN logo majestic logo threadwatch logo seochat tools logo