This question already has answers here:
What does placing \ at the end of a line do in python?
(3 answers)
Closed 8 years ago.
x is an array, y is a dict which is a member of the method z. What does the '\' mean? What does this code do?
for x, y in \
self.z({'yup': [10]}):
for typex in x:
//if statement
EDIT: Thank you for the help, I'm still unsure of what is being iterated over here, the part that is confusing me is the for x,y in as opposed to me usually seeing a statement for only a single variable
The \ is a line continuation, allowing the statement to continue to the next line without raising an indentation error. Aside from that this is just a vanilla for loop.
The \ is simply a line breaker.
The code is not complete, it's just the first part of the for loop. What x in y are iterating over in the loop will depend on the method/function z.
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 the single underscore "_" variable in Python?
(5 answers)
How can I get around declaring an unused variable in a for loop?
(10 answers)
Closed 1 year ago.
I'm looking at some tensorflow stuff and I understand for loops or atleast I think I do, however I came across for _ in range(20) and was wondering what is the meaning of the _ in this case. I am used to for x in range or for i in range stuff and understand those but haven't been able to understand what i've read on the underscore
When you are not interested in some values returned by a function we use underscore in place of variable name . Basically it means you are not interested in how many times the loop is run till now just that it should run some specific number of times overall.
This question already has answers here:
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Comparing a string to multiple items in Python [duplicate]
(3 answers)
Closed 2 years ago.
Note: I'm not a fluent english speaker, so you may see some grammar mistakes here. I'm kinda new to programming and i know i can fix it by putting another else/elif statement, i just trying to get the short possible lines to avoid spamming elif like yandev.
so the thing is, at the #functions i already put if below the gae variable's input, which says that if the input = y or Y then it runs the line below it, which is print("good. go to www.minecraft.net"). Now my problem is, when i try to input other letter than Y, it still runs the same line whilst there's already an else line underneath that i think should exclude other words than Y or y. For example, i already input the word "n" at the output console(?) but the output says "good. go to www.minecraft.net" instead the lines at the else statement. is there any possible solution without adding much more elif/else statement?
Click this link for the image to understand what am i talking about
The line if gae == "Y" or "y" should be if gae == "Y" or gae == "y" otherwise it will always be true and the code will always print "good. go to www.minecraft.net".
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:
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]