1

In my python course we are learning about for loops and I still do not understand why we need (in this example) "x" in the code. Below is an example taken from W3schools.

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  if x == "banana":
    continue
  print(x)

My question is why do we need to use 'x' (or any other variable) for this, when we just want to cycle through the list that is 'fruits'?

Mark
  • 90,562
  • 7
  • 108
  • 148
VC_96
  • 23
  • 3
  • 1
    `for in fruits:` Does that sound like valid English? – no ai please May 29 '21 at 00:31
  • 8
    What would you put where `x` is in `if x == "banana"` if you didn't have this variable? You need something to call the particular item from your collection so you can refer to it in the code. That's all a variable is: a name for something. – Mark May 29 '21 at 00:32
  • If you don't plan on using the variable, use `_`. – Have a nice day May 29 '21 at 01:52

5 Answers5

2

You need the variable in order to utilize each element during the loop:

fruits = ["apple", "banana", "cherry"]
for x in fruits:
    if x == "banana": # Without the variable, you won't be able to do this line
        continue
    print(x)

In cases where you don't need the variable, you can use _ as the variable name:

for _ in range(5):
    print("hello")

Output:

hello
hello
hello
hello
hello

Of course, the _ will still be a variable, but it's used as a general purpose "throwaway" variable name. See What is the purpose of the single underscore “_” variable in Python?

Red
  • 26,798
  • 7
  • 36
  • 58
1

My question is why do we need to use 'x' (or any other variable) for this, when we just want to cycle through the list that is 'fruits'?

x is the iterator variable, it stores the value of the current item in your list.

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  if x == "banana":
    continue
  print(x)

In this code, on the first iteration x would store the value of "apple", on the second iteration it stores the value of "banana", third iteration it stores "cherry". A variable is simply a named location in memory. As you iterate through a list using a for loop, the iterator variable stores the current item in that list. If you didn’t store the value of the current item in the list, your IF statement would not be able to check the value of x if x == "banana" wouldn’t be able to run.

0

Because you dont want to just cycle through the list, but you also want to do something with the current element. In this case you have 3 cycles and want to check if the element in the current cycle is "banana". To do that you somehow have to reference the current element, which is referenced as x in this case. You dont have to declare the reference as x, you could just as well use a, b , c or y or whatever you want (within certain naming rules).

goldarm5
  • 49
  • 8
0

If you were just cycling through the list then x wouldn't strictly be necessary as far as storing a value goes. Just print them all right. You could just use print(fruits) though the output would be slightly different.

The important thing to notice is the comparison or the conditional statement if x == "banana":

In English this translates to, for each fruit in the list, if the fruit is a banana then ignore it, but how do you compare something you don't know anything about? So the variable x is used to store the information relevant to the item so you can do something with it. In this case it's checking if each fruit is a banana.

matdon
  • 146
  • 6
0
desserts = ["cake", "ice cream", "sorbet", "pie"]
fruits = ["apple", "banana", "cherry"]
for y in desserts
   for x in fruits:
     if x == "banana" && y == "sorbet":
       continue
     print(x + " " + y)

Maybe it is more clear when using more than one variable. Though I'm not sure what exactly is your misunderstanding. Have you learned about functions yet? A for loop is like a function in that they are both used to execute the SAME code multiple times on DIFFERENT things. You need variables to stand in for the different things. Like a simple math function. How would you write a math function without variables?

Darren
  • 1
  • 1
  • You miss a `:` at the end of `for y in desserts` and `&&` is not supported in Python, so your code is not able to run. – adamkwm May 29 '21 at 08:32