Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
question on using for
#1
trials = 10000
successes = 0

for _ in np.arange(trials):
    rolls = np.random.choice(dice, k)
    if sum(rolls == 6) > 0:
        successes = successes + 1

successes / trials
in the code above, what does it mean to use _ after for?

for _ in np.arange(trials):
#2
(Jun-17-2018, 11:28 AM)tryingtolearnpython Wrote:
trials = 10000 successes = 0 for _ in np.arange(trials): rolls = np.random.choice(dice, k) if sum(rolls == 6) > 0: successes = successes + 1 successes / trials
in the code above, what does it mean to use _ after for? for _ in np.arange(trials):

_ is the variable in which the key values are stored. For iterates over the keys in a list. So for every key, there is one loop.

On every single instance of the loop, suppose it is at the starting key value, _ is set to the key it is iterating. So if you use print(_) then you will get the key.

_ is the name of the variable. You can change it to anything you want it's just a placeholder, like: Giraffe;Comet_Man and etc.
#3
in python _ is valid variable name. As per convention _ variable name is used when the variable value doesn't matter. In your example you just need to loop 10000 times (i.e. 10000 trials), you don't care for the variable value and don't use it in the code elsewhere
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

#4
_ is also assigned the last mathematical call (ex: 2+2, _ is 4) in IDLE. _ should never be used as a variable.
#5
(Jun-17-2018, 03:42 PM)Nwb Wrote: _ is also assigned the last mathematical call (ex: 2+2, _ is 4) in IDLE. _ should never be used as a variable.

It is assigned the value of the last executed expression that produces result which is not None - e.g. print and assignment will not set _. It is often used as a placeholder variable by some people with some experience (me among them) - e.g., in IDEs it will prevent linters from screaming at you that you create an unused variable.

Please, try to grow beyond the newbie level before making sweeping statements
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
#6
(Jun-17-2018, 03:54 PM)volcano63 Wrote: It is often used as a placeholder variable by people with some experience (me among them)

And not by others with some experience (me among them).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
#7
(Jun-17-2018, 06:35 PM)ichabod801 Wrote:
(Jun-17-2018, 03:54 PM)volcano63 Wrote: It is often used as a placeholder variable by people with some experience (me among them)

And not by others with some experience (me among them).

Well, you don't have to, dear. My point was that using it is absolutely legit.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
#8
(Jun-17-2018, 07:55 PM)volcano63 Wrote: Well, you don't have to, dear. My point was that using it is absolutely legit.

My apologies. That was not how your point came across to me.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
#9
Good conversation going here, thanks everybody. I think I understand now thank you all very much. I'm new to Python and I'm trying to learn it from videos on edx so your input really helps thanks.
#10
(Jun-17-2018, 03:54 PM)volcano63 Wrote:
(Jun-17-2018, 03:42 PM)Nwb Wrote: _ is also assigned the last mathematical call (ex: 2+2, _ is 4) in IDLE. _ should never be used as a variable.
It is assigned the value of the last executed expression that produces result which is not None - e.g. print and assignment will not set _. It is often used as a placeholder variable by some people with some experience (me among them) - e.g., in IDEs it will prevent linters from screaming at you that you create an unused variable. Please, try to grow beyond the newbie level before making sweeping statements

You can correct me but keep your comments to yourself. I don't need advice from people, especially people like you. :)


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020