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.
Related
This question already has answers here:
How do I do a case-insensitive string comparison?
(15 answers)
Closed last year.
import time
while True:
npc=input("Josuke\n\nJotaro Kujo (Part 4)\n\nChoose NPC to talk to.")
if npc=="Josuke" or npc=="josuke":
confirm=input("[Press E to interact.]")
elif npc=="jp4" or npc=="JP4" or npc=="Jp4
Within this code you can see that there are 2 NPCs to interact to. Because there are many ways of addressing the name Jotaro Kujo (Part 4), the if statement has many "or"s, but I want to be able to condense it. Could I use an array to be able to put the possibilities in and then have the if statement identify if the value is within the array? (I haven't completed the code yet, but the problem doesn't require the full code to be completed.)
Yes, you can do it easily using the in operator to check if a particular string is in the list.
as an example:
lst = ["bemwa", "mike", "charles"]
if "bemwa" in lst:
print("found")
And if all the possibilities you want to cover are related to case-insensitivity you can simply convert the input to lower-case or upper-case and compare it with only one possibility.
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.
This question already has answers here:
What is the purpose of the single underscore "_" variable in Python?
(5 answers)
Closed 4 years ago.
I can not understand why does it need "_" before another execution within
a loop.
Here is the code:
for i in range(len(X_train)):
feed = {X: [X_train[i]],y: [y_train[i]]}
_, loss = sess.run([train_op, cost],feed_dict=feed)
I had no problem in running the code, but I have no idea about why it had to place a "_" before the next statement. Anyone knows?
Because you have supplied two inputs, sess.run returns two outputs.
A single underscore is often used in Python as a variable name that we don't care about. _, loss just means "I don't care about the first output, give me the second one."
This question already has answers here:
Dynamic variable in Python [duplicate]
(2 answers)
Closed 8 years ago.
I would like to know how to create lots of variables by looping it. I know other people have asked this before but everyone who knows says you need a good reason for it and to just set it in a dictionary. My reason is that I need to assign up to 6156119580207157310796674288400203776 variables and there is no way I can do that by typing them out.
I need something like:
while counter < 1000:
try[counter] = counter
So that I could do this:
>>> try837
837
>>>try453
453
etc.
(this is an example not the exact code but any answer for this will solve my problem)
I would also like to know why people are opposed to answering this particular question. I don't want to tax my computer more than I already am by assigning this many variables so if it is an issue that could harm my computer or my code I would like to know.
You don't want to do this. Create a dictionary with a key for each suffix that you would use. Then use try[557] in place of the variable try557.
>>> try_ = dict((counter, counter) for counter in range(1000))
>>> print try_[557]
557
I'm using the standard technique of affixing an underscore to the otherwise reserved word "try".
(I'm ignoring the ludicrously large number of variables you claim to need.)
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.