
I’ve been refreshing my Python skills lately using Codedex and the best way for me to learn is by breaking down the code into simple, everyday language, or what I like to call techsplaining. So lets dive into what exactly a for loop and range() are, and how they work.
The Challenge

The goal was to write a code that will print "I will not use Snapchat in class" 100 times.
My First Attempt

At first, I thought I could just assign the sentence to i and loop it. Although it did print the sentence 100 times, I received an error when I tried to submit the code. After some research, I realized that my code was not running as intended.
The for loop is designed to loop automatically and runs through the code repeatedly until the sequence ends. By reassigning a string to i, I was overwriting what i is supposed to represent: a counter variable that changes each time through the loop.
My Final Code

This version is much cleaner and actually uses the loop properly.
Code Techsplained
Lets tackled this line by line.
for i in range(100):This is the start of a for loop. Whereas the while loop runs based on a condition, the for loop runs based on a sequence.
i is a variable that keeps track of the current position in the loop and can be renamed to anything. It does not control the loop. Instead, the loop automatically updates i each time and stops when it has reached the end of the range.
This creates a sequence of numbers starting from 0 up to, but not including, 100. So it actually means 0–99, which gives us 100 total numbers. Each time the loop runs, i takes on the next number in that sequence.
print("I will not use Snapchat in class")It’s important to note that this line is indented, which means it’s part of the for loop. Each time the loop runs, this line will execute and print the sentence.
Testing My Code
When I ran my final code, it produced the result I'd expected.

What I Learned
I learned the structure of a for loop.
for <variable> in <range>:
<repeat this action>By default, if only a single value is provided in range then range(n) will start at 0, end at n-1, and count up by 1.
for <variable> in <range start, range stop, range step>:
<repeat this action>The range can also be defined with three arguments: start, stop, and step.
for i in range(3, 24, 6):
print(i)In this example, the loop starts at 3, stops before 24, and increments by 6. This loop will print: 3, 9, 15, and 21.
Final Thoughts
This exercise taught me that range() doesn't include the last number and the for loop doesn't rely on conditions. Instead, it automatically runs through each value in the sequence. The loop variable i acts as a counter that updates each time the loop runs.





