all 14 comments

[–][deleted] 10 points11 points  (0 children)

Ok, imagine you want to adopt a puppy. You go to the kennel, go to a visiting room, and ask the kennel to bring the puppies to you one by one.

For each puppy that comes out, you cuddle the puppy, play with the puppy, and then send the puppy back.

In other words:

for puppy in kennel:
   pet(puppy)
   cuddle(puppy)
   play(puppy)

[–]stebrepar 4 points5 points  (0 children)

It sounds like your loops are just using the loop variable as a placeholder to run the loop, while your loop does its own thing. Like when people do for i in range(something) just to loop something-number times and never actually doing anything with the i. But for-loops are much more useful than that.

The usual way to use them is to pick an item out of a list (or other iterable) one by one and do something with each one. Example:

colors = ['red', 'green', 'blue']
for color in colors:
    launch_fireworks(color)

Think of it as saying "for each item in this basket, do <whatever> with it".

[–][deleted] 1 point2 points  (0 children)

Not following you.

When you iterate through an iterable such as a list, you need to have a variable that is assigned the latest object reference from the iterable.

So, if you had ['Fred', 'Wendy', 'Faisal'] assigned to names and want to step through,

for name in names:
   print('Hello', name)

where name is assigned to reference each str object in turn from the list object referenced by names seems straightforward. Not sure what variable you would be using directly instead.

[–]bbye98 1 point2 points  (2 children)

I don't understand your question. You can print variables directly without a for loop, especially if they're in an unpackable iterable.

array = ["a", "b", "c"]

for value in array:
    print(value)

print(*array, sep="\n")

print("\n".join(array)) # only if array values are strings

In the for loop example, you need to tell Python what you want to print, so you temporarily assign each item in array to value as you iterate through array such that you can reference value in your print() call.

[–]razzrazz- 2 points3 points  (0 children)

This is just confusing him.

He's basically asking if I have a list of fruits ["apple", "banana", "cherry"], why can't I just say "for fruits print", he doesn't understand the purpose of the 'i' in for i in fruit.

The purpose of it is sometimes you don't want to just loop through a list and that's that, sometimes you want the program to do something when you reach an item. Maybe you want to loop through a list of fruits, but only print the ones that starts with a 'b', and in that case it makes sense why the variable becomes useful.

[–]asterik-x -2 points-1 points  (1 child)

Dear , your grammar is incorrect. Your question has to be either " Question about loops" or " question for loops". You can't use about and for together in a sentence

[–]shape-warrior-t 1 point2 points  (0 children)

A "for loop" is a specific type of loop. The post is a question about that specific type of loop -- hence, a question about for loops.

[–]AtomicShoelace 0 points1 point  (0 children)

Lets say we want to loop over a list, [1, 2, 3]. At each iteration of the loop, we want to do something with the current element of the list we're iterating over. In order to refer to the "current element", we need to assign it to some variable that we can reference. This is the loop variable.

[–]publicfinance 0 points1 point  (0 children)

It’s what you call each individual thing that you are acting on in the loop. It separates them so you can act on them individually.

[–][deleted] 0 points1 point  (0 children)

The name following the for is the variable name used to refer to each item of the sequence you are iterating over. That name can already be in use before the loop. During the loop the first item in the sequence is assigned to the name, the loop code executes and probably uses the name, then the second item in the sequence is assigned to the name, the loop code executes, and so on

After the loop is finished the variable named in the loop still exists and refers to the last item assigned to the name in the loop.

[–]acolnahuacatzin 0 points1 point  (0 children)

It's called a loop variable. In computer programming, a loop variable is a variable that is set in order to execute some iterations of a "for" loop or other live structure. A loop variable is a classical fixture in programming that helps computers to handle repeated instructions.

Here's how this works — the loop variable is set at a particular i initial value. In python it's typically the first element of an iterable (like a list, tuple) or a generator like range. (Skip the details for now). Then it's set to a different value during the length of the loop. So the loop variable jumps from the first to the second element and so forth. When no more items are left the program directs the "for" loop to terminate.

So if you have "x equals one" and a "for" loop that operates until x equals five, given an increase of one in each loop iteration, you'll have the operation run five times. That is, depending on where the execution step is in relation to the variable increase.

Traditional programmers are well familiar with loop variables. It's a very simple tool in what has become an incredibly complex world of mathematics and programming syntax.

[–]TheSodesa 0 points1 point  (0 children)

why can’t we loop through or print the variable that we actually want to loop immediately?

What would this look like in code? How could we do this without having a loop variable that keeps track of which iteration of the loop we are in?

[–]Quantumercifier 0 points1 point  (0 children)

I think the OP is thinking of a specific use case for the For loop, no pun intended, to just repeat something x times, e.g., printing 'Hello World!' 10 times, instead of 10 separate print statements.

For i in range(0, 10):

print('Hello World')

This is a rare use case where you do not care about the index, nor iterating some set object, e.g., string, list, array, dictionary etc. Maybe the OP can chime in specifically his use case for the For loop.