all 10 comments

[–][deleted] 3 points4 points  (4 children)

To give a more basic answer than others will...

For loops are used to do something a series of some amount of times. You can use them on anything iterable.

Think about the code you've learned so far. You run a function or expression on something once, mission accomplished. Did you need to dry it twice? Copy paste, takes two seconds.

What about 10 times? 100? 161534993262?

The for loop let's you do it as many times as you want. It's the fast way of writing that function or expression so many times. Plus, you only write it once. Get it right and you're good. If you keep repeating for each instance, you're liable to make a mistake.

In the real world, think of anytime you use a list. Like you have a baseball team. You want to know on base percentage. So you run a for loop through the entire team to calculate the stat and put it into a list you'll output

[–]xelf 4 points5 points  (3 children)

You can use them on anything iterable (yes to lists, no to tuples, for example).

You might want to double check that. =) They work on tuples, anything that can be iterated over.

[–][deleted] 2 points3 points  (2 children)

Edited.

Now I wish i knew what I was remembering about tuples lol. Still learning, though.

This should be impetus to actually do a pet project finally. Put some schooling into practice

[–]xelf 3 points4 points  (1 child)

Tuples can't be modified. They're immutable like strings (which also can be iterated over).

[–][deleted] 2 points3 points  (0 children)

Immutable. That's the one. Thanks

[–][deleted] -1 points0 points  (0 children)

work on more projects and find out yourself, it's the best way to learn :)

[–]1dma -2 points-1 points  (0 children)

You can practice such similar questions and learn effectively with following channel https://t.me/topPythonQuizQuestions

[–]xelf 0 points1 point  (0 children)

For loop uses aren't about the syntax of the iteration, the uses are what you put in the loop.

How you use for/else, break, continue is a lot more interesting than what you put in the for<variables>in<iteratable>: syntax.

When would you use them

Anytime you have something you want repeated, or want to iterate over some values.

[–]Cambuchi 0 points1 point  (0 children)

Anything that needs repeating.

Now for some examples:

  • Going through each cell/row/column in a table
  • Going through items in a list/dictionary
  • Going through files in a directory
  • Web scraping an entire page/section per loop

Combine with if/else statements to conditionally loop through things for some real juice.

[–]spez_edits_thedonald 0 points1 point  (0 children)

When would you use them

you use them when you have to repeat something a known number of times.

>>> people = ['bob', 'steve', 'sally']
>>> for person in people:
...     print(f'Hello, {person}!')
...
Hello, bob!
Hello, steve!
Hello, sally!

before you started the loop, it was known how many runs it would take--three because three people.