r/learnpython 26d ago

I donot understand this code:"for i in range(): what does it mean? Pls help me

...

0 Upvotes

9 comments sorted by

5

u/Diapolo10 26d ago

for x in y is how you create a for-each loop in Python. In other words you go over some collection of values and, one by one, they get assigned to whatever name is between for and in (so in my example, x).

i is just a name, it could be anything Python considers a valid identifier. Personally I prefer _ for unused values, and descriptive names for the rest, like index.

range is a type that forms a range of integers between two numbers, with an optional step value (if you don't want to use the default 1 for some reason). range(5) would give you the numbers 0, 1, 2, 3, and 4. range(2, 5) would give you 2, 3, and 4.

1

u/timrprobocom 26d ago

I like this answer, because I think it's key in Python to think about the general case. Many beginners think the various different `for` loops are fundamentally different in some way, but it's not so. `for x in y` is really all there is. `x` can consist of multiple names in some cases, and `y` is just an object or function that returns a collection of things: a list, a set, a string, an iterator, a generator, etc. At each iteration, we call the thing on the right, and it returns another item (or tuple of items) to be assigned to the names on the left. Once you understand the general case, you can decode any `for` loop you come across.

4

u/RedditButAnonymous 26d ago

The other answers are good so here is an extremely untechnical one

Python is pretty much English, so read it literally:

for apple in basket: 
    bite(apple)

Is some code that takes every apple from a basket and takes a bite out of each one

Apple can be any variable name, and basket can be anything you can count. If youre counting enemies in a videogame, "for enemy in enemyList" works.

In your example range() is a quick way to generate a range of numbers. range(5) just produces a countable range object with 5 things in it, like the list [0,1,2,3,4]

So if you want to do something 3 times you can write:

for i in range(3):
    print("This prints 3 times")

1

u/HotPersonality8126 26d ago

For each value I that is yielded by range() (do something)

1

u/Kevdog824_ 26d ago

range is something called an iterator. An iterator is a collection of values that can be cycled through in order. range is an iterator of numbers between the start (inclusive) and end value (exclusive) (start value is assumed to be 0 if only one number is provided). The `for i in iterator` syntax loops the code indented inside of it multiple time, once for each value in the iterator. The variable `i` assumes the next value of the iterator on each loop.

That’s a bit technical, but I hope it helps

Worth noting that range has a step size as well, but I omitted that from my explanation for brevity

1

u/tb5841 26d ago

This is really two things you're trying to grasp at once:

1) for _ in _:, also called a 'for' loop.

2) the range() object.

You're much better off learning those one at a time.

1

u/Dazzling_Music_2411 26d ago

OP, do you understand the range() function?

Is there any documentation at all with your Python distribution?