Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
question on using for
#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.


Messages In This Thread
question on using for - by tryingtolearnpython - Jun-17-2018, 11:28 AM
RE: question on using for - by Nwb - Jun-17-2018, 11:49 AM
RE: question on using for - by buran - Jun-17-2018, 11:57 AM
RE: question on using for - by Nwb - Jun-17-2018, 03:42 PM
RE: question on using for - by volcano63 - Jun-17-2018, 03:54 PM
RE: question on using for - by ichabod801 - Jun-17-2018, 06:35 PM
RE: question on using for - by volcano63 - Jun-17-2018, 07:55 PM
RE: question on using for - by ichabod801 - Jun-17-2018, 08:47 PM
RE: question on using for - by tryingtolearnpython - Jun-18-2018, 07:17 AM
RE: question on using for - by Nwb - Jun-18-2018, 10:12 AM
RE: question on using for - by volcano63 - Jun-18-2018, 01:35 PM
RE: question on using for - by DeaD_EyE - Jun-18-2018, 10:51 AM
RE: question on using for - by volcano63 - Jun-18-2018, 06:46 PM
RE: question on using for - by buran - Jun-18-2018, 03:52 PM
RE: question on using for - by Nwb - Jun-22-2018, 02:46 AM
RE: question on using for - by buran - Jun-22-2018, 05:00 AM

Forum Jump:

User Panel Messages

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