More on range and for loop wihout range
Right, let’s dive deeper into the range()
function and explore a cool alternative way to use for
loops, drawing from the video “More on range and for loop without range” and our previous chats about loops! 😊
As we’ve discussed, for
loops are excellent for repetitive tasks. They’re often used when you know how many times you need to repeat something or when you want to go through items in a sequence. The range()
function is a very common tool to use with for
loops to generate a sequence of numbers.
More on range()
🔢
We’ve seen range(10)
which gives you numbers from 0 up to (but not including) 10, i.e., 0, 1, 2, …, 9.
The “More on range” part of the video explains that range()
can take more than one argument, allowing you to control where the sequence starts and how it increments.
range(start, stop)
: This generates numbers starting fromstart
and going up to, but not including,stop
.- For example,
range(1, 11)
generates numbers from 1 up to (not including) 11. This gives you 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. This is super useful, like when we made the multiplication table starting from 1 times the number. - Another example from the sources is
range(5, 11)
which starts at 5 and goes up to 10.
- For example,
range(start, stop, step)
: This adds a third argument,step
, which controls how much the number increases (or decreases) each time. The default step is 1.- If you want to count down, you can use a negative step!. For instance,
range(9, -1, -1)
would start at 9, go down to -1 (stopping before -1, so it includes 0), decrementing by 1 each time. This would give you 9, 8, 7, 6, 5, 4, 3, 2, 1, 0. - You can also use a step like -2 to get odd numbers in reverse order or a positive step like 2 to skip items.
- If you want to count down, you can use a negative step!. For instance,
Using range()
like this with a for
loop is one way to create loops that count or iterate through indexes.
for
loop Without range()
✨
The video then introduces a way to use a for
loop without using range()
. This might seem surprising at first, as many examples show for i in range(...)
.
However, a for
loop is actually a generic sequence iterator. This means it can step through the items of any sequence object, not just the numbers generated by range()
. Sequences include strings, lists, and tuples.
This alternative usage is sometimes called a “for each” loop. Instead of the loop variable taking numbers from range()
, it directly takes each item from the sequence you provide, one by one, until there are no more items left in the sequence.
Here’s the example from the source:
country = "India" # Here's our sequence (a string) 🇮🇳
# Use a for loop directly on the string!
for letter in country: # 'letter' will take each character's value
print(letter) # Print the current character
# Output:
# I
# n
# d
# i
# a
Let’s trace this:
- The
for
loop starts, looking at thecountry
string “India”. - First iteration: The variable
letter
is assigned the first character of “India”, which is ‘I’. The code inside the loop runs, printing ‘I’. - Second iteration:
letter
is assigned the next character, ’n’. The loop body runs, printing ’n’. - This continues for ’d’, ‘i’, and ‘a’.
- Last iteration:
letter
is assigned the last character, ‘a’. The loop body runs, printing ‘a’. - There are no more characters in the string. The loop finishes.
The video points out that this for letter in country
code is a more efficient and easier-to-write way to achieve the same result as writing multiple print(country[index])
lines manually. It automatically handles going through each item without you needing to manage an index variable or use range(len(country))
.
In summary:
- The
for
loop is Python’s primary tool for iterating over sequences. range()
is often used withfor
to generate sequences of numbers, and it has flexiblestart
,stop
, andstep
arguments.- You can also use a
for
loop directly on sequences like strings, lists, or tuples, withoutrange()
. The loop variable takes the value of each item in the sequence turn by turn. This “for each” style is often preferred when you just need the items themselves and not their numerical index.
Understanding these different ways to use for
loops makes them incredibly powerful for handling repetitive tasks with collections of data! 💪