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!
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.
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.
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 ( # ).
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.
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"
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"}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With