This question already has answers here:
What is the purpose of the single underscore "_" variable in Python?
(5 answers)
Closed 3 years ago.
At 44:05 in his Fun of Reinvention talk, Dave Beasley writes
>>> d = _
There is a lot before that, which is necessary for the result he gets. But ignoring the output, what does that input line mean? Whenever I try it, either in a file in the PyCharm editor, in the PyCharm Python console, using straight IDLE (all v3.7) I get an error.
Any idea what this may mean and how to get something like that to run?
Thanks
_ is a special variable in the python language.
In some REPLs, like IDLE, it holds the result of the last expression executed.
d = _ assigns the result of the last expression executed to d.
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:
How do I execute a string containing Python code in Python?
(14 answers)
Closed 2 years ago.
I have a little problem, I have written a for loop as a string.
In PHP, with the help of function exec(), we can run the string which will eventually run the for loop defined inside the string.
Can we do such a thing in Python as well?
By example, I would like run follow it:
string="for i in range(1,(5+1)): print(str(i))"
How to run this in Python?
You can use exec if you want to execute some statements:
code = 'for i in range(1,(5+1)): print(str(i))'
exec(code)
If you want to evaluate an expression and get the value then you can use eval:
value = eval('2+3')
print(value) # 5
This question already has answers here:
Replace console output in Python
(12 answers)
Closed 4 years ago.
I am trying to build a game.
The game will have an item called a "pulsating crystal" (I am using \033[1;31;40m] to change the items colour), I want to it to be rainbow, so it keeps changing colours, without deleting everything else in the terminal. I used print(\033c) to clear the terminal but I just want to print the last line. I am sorry if the question is unclear or repetitive, or has another answer but I couldn't find another clear answer for my problem. PS I use Linux.
I just want to print the last line.
To print a line repeatedly, just override the line ending \n by giving the keyword argument end='\r' to print().
This question already has answers here:
Is it possible to implement a Python for range loop without an iterator variable?
(15 answers)
Closed 7 months ago.
Is it possible in python to have a for-loop without index and item?
I have something like the following:
list_1 = []
for i in range(5):
list_1.append(3)
The code above works fine, but is not nice according to the pep8 coding guidelines.
It says: "Unused variable 'i'".
Is there a way to make a for-loop (no while-loop) without having neither the index nor the item? Or should I ignore the coding guidelines?
You can replace i with _ to make it an 'invisible' variable.
See related: What is the purpose of the single underscore "_" variable in Python?.
While #toine is completly right about using _, you could also refine this by means of a list comprehension:
list_1 = [3 for _ in range(5)]
This avoids the ITM ("initialize, than modify") anti-pattern.