1

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)
Jan Christoph Terasa
  • 5,781
  • 24
  • 34
Kay
  • 11
  • 1
  • If you do not need `x`, you do not need to use the loop variable. It's customary in Python to use `_` as name for the loop variable in that case, to make it more clear that the loop variable is not used. – Jan Christoph Terasa Sep 15 '22 at 07:15
  • Usually when using `numpy` you try to use vectorized functions and methods instead of Python loops. – Jan Christoph Terasa Sep 15 '22 at 07:26

3 Answers3

1

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.

el_oso
  • 1,021
  • 6
  • 10
0

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.

Flow
  • 551
  • 1
  • 3
  • 9
0

I think I didn't understand your question, but if you meant 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 any other iterator 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 everything inside the for loop until the list has no more items, and when the list is over, the the code will stop repeating the loop and will go to the next line after the loop, the variable doesn't necessarily have to be used (i.e. save it in a throwaway variable like _), but it can be used to know if end of the loop arrived and change the loop according to it's variable. you can tell me if this is not what you meant and give me more explanation about the question.

Coddy
  • 549
  • 4
  • 18
LORD_M.D
  • 11
  • 1