This question already has answers here:
How can I do a line break (line continuation) in Python?
(10 answers)
Closed 4 years ago.
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?
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]
Related
This question already has answers here:
What does a backslash by itself ('\') mean in Python? [duplicate]
(5 answers)
What is the purpose of a backslash at the end of a line?
(2 answers)
Closed 1 year ago.
While I was searching code from the internet about YouTube data analysis, I found code like this:
df_rgb2['total_sign_comment_ratio'] = df_rgb2['total_number_of_sign'] / df_rgb2['comment_count']
total_sign_comment_ratio_max = df_rgb2['total_sign_comment_ratio'].replace([np.inf, -np.inf], 0).max()
df_rgb2['total_sign_comment_ratio'] = \
df_rgb2['total_sign_comment_ratio'].replace([np.inf, -np.inf], total_sign_comment_ratio_max*1.5)
and I was wondering why the analyst used the expression:
df_rgb2['total_sign_comment_ratio'] = \
because whether I apply that code or not, the result is same.
I tried to find the meaning of '\' but all I have got is how to use '\' when printing out the result.
\ is usually used to make a piece of code go on onto multiple lines. If you where to just press enter and continue to write code a line below for example declaring a variable, it would count as an error.
You use this when you need to tidy up code or when your working window is too small for some reason.
See: https://developer.rhino3d.com/guides/rhinopython/python-statements/
This question already has answers here:
What is the purpose of a backslash at the end of a line?
(2 answers)
Closed 2 years ago.
What is the use of \ in python?
Example, I saw the following codes"
data_idxs = [(self.entity_idxs[data[i][0]], self.relation_idxs[data[i][1]], \
self.entity_idxs[data[i][2]]) for i in range(len(data))]
or
self.relations = self.train_relations + [i for i in self.valid_relations \
if i not in self.train_relations] + [i for i in self.test_relations \
if i not in self.train_relations]
I guess you write \ when you want to have your code continue in a new line? But I also saw that when you have a lot of parameter in a method definition, you can have new line without using .
I guess you write \ when you want to have your code continue in a new line?
Yes. That's all it means.
In these cases it's not actually needed though because line continuation is implied by brackets.
This question already has answers here:
Python csv string to array
(10 answers)
In regex, match either the end of the string or a specific character
(2 answers)
Closed 3 years ago.
I need to capture words separated by tabs as illustrated in the image below.
The expression (.*?)[\t|\n] works well, except for the last line where a line feed is missing. Can anyone suggest a modification of the regular expression to also match the last word, i.e. Cheyenne? Link to code example
Replace [\t|\n] with (\t|$).
BTW, [\t|\n] is a character class, so the pipe | is literal here. You probably meant [\t\n].
This question already has answers here:
Is it possible to break a long line to multiple lines in Python? [duplicate]
(7 answers)
Closed 5 years ago.
I have a long line that pylint is complaining is breaking pep8 E501
count_approvers = self.leave_not_required_user.company.leave_approvers.count()
How can I split this over 2 lines?
Configure your pylint for length of line. Or you can use '\' after '='
count_approvers = \
self.leave_not_required_user.company.leave_approvers.count()
You could use a temporary alias, if you may:
company = self.leave_not_required_user.company
count_approvers = company.leave_approvers.count()
This question already has answers here:
Why is semicolon allowed in this Python snippet?
(15 answers)
Closed 6 years ago.
What's the difference with Python statements ending with ; and those does not?
There is really no difference. Python ends a line of code at the end of the logical line, or when it encounters ;
The only advantage to using ; is that you can stack multiple logical lines into one physical line. For example (in python3):
import sys
for i in range(10):
print(i, end=' '); sys.stdout.flush()
That said, this is terrible coding style, so don't ever do it
Semicolons serve the same purpose as the newline character. It is really just bad style to use a semicolon, often from people coming from languages where lines require it.