Python Forum
question on using for - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: question on using for (/thread-11002.html)

Pages: 1 2


question on using for - tryingtolearnpython - Jun-17-2018

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):


RE: question on using for - Nwb - Jun-17-2018

(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.


RE: question on using for - buran - Jun-17-2018

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


RE: question on using for - Nwb - Jun-17-2018

_ is also assigned the last mathematical call (ex: 2+2, _ is 4) in IDLE. _ should never be used as a variable.


RE: question on using for - volcano63 - Jun-17-2018

(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


RE: question on using for - ichabod801 - Jun-17-2018

(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).


RE: question on using for - volcano63 - Jun-17-2018

(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.


RE: question on using for - ichabod801 - Jun-17-2018

(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.


RE: question on using for - tryingtolearnpython - Jun-18-2018

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.


RE: question on using for - Nwb - Jun-18-2018

(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. :)