Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Breaking a line of python to multiple lines?

Tags:

python

syntax

In C++, I like to break up my lines of code if they get too long, or if an if statement if there are a lot of checks in it.

if (x == 10 && y < 20 && name == "hi" && obj1 != null) 
    // Do things 

// vs

if (x == 10
    && y < 20
    && name == "hi"
    && obj1 != null)
{
    // Do things
}

AddAndSpawnParticleSystem(inParticleEffectName, inEffectIDHash, overrideParticleSystems, mAppliedEffects[inEffectIDHash], inTagNameHash);
// vs
AddAndSpawnParticleSystem(inParticleEffectName, inEffectIDHash, overrideParticleSystems, 
    mAppliedEffects[inEffectIDHash], inTagNameHash);

In Python, code blocks are defined by the tabs, not by the ";" at the end of the line

if number > 5 and number < 15:
    print "1"

Is mutliple lines possible in python? like...

if number > 5 
and number < 15:
    print "1"

I don't think this is possible, but it would be cool!

like image 382
MintyAnt Avatar asked Jan 19 '13 18:01

MintyAnt


People also ask

Is it possible to break a long line to multiple lines in Python?

If you have a very long line of code in Python and you'd like to break it up over over multiple lines, if you're inside parentheses, square brackets, or curly braces you can put line breaks wherever you'd like because Python allows for implicit line continuation.

How do you break a line in Python code?

In Python, the new line character “\n” is used to create a new line. When inserted in a string all the characters after the character are added to a new line. Essentially the occurrence of the “\n” indicates that the line ends here and the remaining characters would be displayed in a new line.

How do you multi line in Python?

Unlike other programming languages such as JavaScript, Java, and C++ which use /*... */ for multi-line comments, there's no built-in mechanism for multi-line comments in Python. To comment out multiple lines in Python, you can prepend each line with a hash ( # ).

How do I split a string into multiple lines?

You can have a string split across multiple lines by enclosing it in triple quotes. Alternatively, brackets can also be used to spread a string into different lines. Moreover, backslash works as a line continuation character in Python. You can use it to join text on separate lines and create a multiline string.


3 Answers

Style guide (PEP-8) says:

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation. Make sure to indent the continued line appropriately. The preferred place to break around a binary operator is after the operator, not before it.

Method 1: Using parenthesis

if (number > 5 and
        number < 15):
    print "1"

Method 2: Using backslash

if number > 5 and \
number < 15:
    print "1"

Method 3: Using backslash + indent for better readability

if number > 5 and \
        number < 15:
    print "1"
like image 134
ATOzTOA Avatar answered Oct 14 '22 22:10

ATOzTOA


You can break an expression up into multiple lines if you surround it with parentheses:

if (x == 10
    and y < 20
    and name == "hi"
    and obj1 is not None):
    # do something

The same is true of brackets or curly braces used to create a list or dictionary:

mylist = [1, 2, 3, 4,
          5, 6, 7, 8]

mydict = {1: "a", 2: "b",
          3: "c", 4: "d"}
like image 42
David Robinson Avatar answered Oct 14 '22 21:10

David Robinson


The pep8 standard guide seems to indented new lines for a list of things in parenthesis while for a long line they suggest a backslash at the end of the line.

Indenting new lines

Backslashes at the end of the line

like image 3
JonathanV Avatar answered Oct 14 '22 20:10

JonathanV