From my understanding of for loops, you are defining the variable in the first line of the argument, you then use this definition in the body of the loop where you create an expression which involves the variable. Here the for loop is iterating over a sequence of 1-10 and creating a list of head/tails. Why is x (the variable) not referred to in the body, and how does the code know to iterate over the 1-10 sequence if x is not referred to in the if-else statement. Sorry I'm a beginner, so very basic
import numpy as np
np.random.seed(123)
outcomes = []
for x in range(10) :
coin = np.random.randint(0, 2)
if coin == 0:
outcomes.append("heads")
else :
outcomes.append("tails")
print(outcomes)
CodePudding user response:
Blockquote From my understanding of for loops, you are defining the variable in the first line of the argument
You are defining a variable whose job is to decide how many times to repeat the block of code below.
Blockquote you then use this definition in the body of the loop where you create an expression which involves the variable. Here the for loop is iterating over a sequence of 1-10 and creating a list of head/tails
You can if you choose to use this variable in the body of the loop in an expression.
For example here, you want to double all the numeric values up to, but not including 10. Since you know that your for loop will generate all the numbers from 0-9 for you, it makes sense to re-use that value in your loop body that prints the numbers.
for x in range(10):
print(x*2)
However, like in your case, you want 10 random choices, represented as TRUE/FALSE, 0/1, or HEADS/TAILS. The x value that counts how many times you repeat your code block has no impact on whether the outcome is a HEADS or TAILS. Therefore there is no need to use x
in the indented code block. You can just let it do it's one job, which is keeping track of how many times you want to repeat the code block.
CodePudding user response:
I think i didn't understand your question, but if you mean how can the code know the variable and close the loop when it's done, then you need to know that the range(1, 5)
object returns the list [1, 2, 3, 4]
and the for loop must be given a list or eny items type, and the code:
for x in range(1, 5):
print('loop')
reads the items of the list one by one and save it as x
and do every thing inside the for loop until the list has no more items, and when the list is over, the the code will stope repeating the loop and will go to the next line after the loop, the varible doesn't ncessarly have to be used, but it can be used to know were the loop arrived or to make the loop meke more effects and change the loop according to it's variable.
you can tell me if this is not what you ment and give me more explanation about the question.
CodePudding user response:
That is neat feature of Python. Consider
for x in something:
If something
is iterable, the for loop will interate over its elements.
That also means you do not need to access list elements by index:
for x in ['first', 'second', 'third']:
print(x)
#Output
first
second
third
Range in addition is a something like a generator function which generates equally spaced integers (it actually is a sequence object, see link below). In your case, where you did insert only ony argument, the spacing is 1, so it will generate numbers starting from 0 until 9 (the given argument is not part of the generated sequence). For more details on loops in python see for example here For details on range, pretty interesting stuff about what range is doing under the hood.