I have a long expression, its' not fitting in my screen, I want to write in several lines.
new_matrix[row][element] = old_matrix[top_i][top_j]+old_matrix[index_i][element]+old_matrix[row][index_j]+old_matrix[row][index_j]
Python gives me 'indent' error if I just break line. Is there way to 'fit' long expression in screen?
You cannot split a statement into multiple lines in Python by pressing Enter . Instead, use the backslash ( \ ) to indicate that a statement is continued on the next line.
Using Python's inferred line continuation within parentheses, brackets, and braces is the recommended method of wrapping lengthy lines. You can put an extra pair of parentheses around an expression if required.
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 ( # ).
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.
I hate backslashes, so I prefer to enclose the right hand side in parens, and break/indent on the top-level operators:
new_matrix[row][element] = (old_matrix[top_i][top_j]
+ old_matrix[index_i][element]
+ old_matrix[row][index_j]
+ old_matrix[row][index_j])
You can break expressions into multiple lines by ending each line with \
to indicate the expression will continue on the next line.
Example:
new_matrix[row][element] = old_matrix[top_i][top_j]+ \
old_matrix[index_i][element]+old_matrix[row][index_j]+ \
old_matrix[row][index_j]
Yes, use \
:
new_matrix[row][element] = old_matrix[top_i][top_j]+old_matrix[index_i]\
[element]+old_matrix[row][index_j]+old_matrix[row][index_j]
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