Snake Has A Limit: Controlling The Flow Continues


Revisit this article. In this article, the father of our stubborn snake offers him some chocolates in return for the phone. The parents of this snake were born after 1990, which means they are modern parents who cannot punish their child. Why? Because their kid would be devastated by just a slap.
But let's reimagine this story with parents born between 1970 and 1980. What would they do to their snake kid? Let's imagine the snake is an early 2000s-born kid and see how the parents handle this situation where the snake is not giving his father's phone back to him.
The stubborn snake (born in the early 2000s) is not giving the phone back to his father. The father has to email something important to the manager of the company. The snake has been using the phone for the past hour non-stop. His father is very angry, and so is his mother. What do you think? Will his current parents, i.e., parents born in the 1970s or 1980s, give him an offer like his previous parents did, or do something else? Let’s see in this article.
Snake Has A TIme Limit (For Loop and Range)
So, how do these parents handle their kid? They are very intelligent parents. They know that if they give offers to their kid, then their kid will become greedy, and it will be considered bad parenting. So, what will the father and mother do?
Mother got an idea. She said to the father, “We should give our son a time limit. If he exceeds that limit, then we will take the phone back and also punish him by excluding him from dinner.”
That’s a brilliant idea. This way, the snake can use the phone and will return it after the time is over. Both parents are happy. Let’s see how the father conveys this message to his son.
Father said, “Son, you can use my phone ‘for’ ten minutes only. After that, you have to return it; otherwise, I will punish you.”
So, our snake will “repeatedly” use the phone for the given ten minutes and then give it back to his father.
Let’s get technical and understand the concept of the For loop.
For Loop
There are two looping statements in Python, one of which is the for statement. Looping statements help our computer program repeat a task. For loops are used to perform a task repeatedly for a certain amount of time or for a particular range.
Syntax → for variable_name in range()/Lists/Tuples/Sets/Dictionaries
Revisit our story, where our snake is repeatedly using the phone for ten minutes, after which his father will snatch it from his hand. So, from the above example, the task the snake should repeat is “Using Phone,” and how much he can use it is the range limit of the for loop, which is ten minutes in our story.
Technically, his father is saying it like this…..
for time in range(11):
print("Son, you can use my phone")
The above code basically means, if time is less than ten minutes, the snake can use the phone repeatedly. Once time reaches above ten, then the program will stop, i.e., the snake is not using the phone.
You might ask why I write 11 inside the range() function; that's how the range() function works. Let's discuss it in detail.
Father Sets The Limit (range())
Okay, the snake’s father sets the limit of ten minutes during which his son can use the phone repeatedly, but how can we set limits in our program so that it can run some commands up to a certain limit and exit the code once that limit is reached?
That’s where the range() function comes in. The range function is basically used to set the limit for the loop. It tells the loop to work until that range is reached. If the range is reached, then the loop stops its execution. If we don’t define the range function, then the for loop will give an error unless we are traversing through the data structures, which we discussed in this article.
The range() function takes three arguments: starting, ending, and steps. By default, starting is 0 and step is 1.
Syntax → range(start, end, step)
The range() function works till end-1
Note → The range() function is not limited to for loops; it can be used in any place where we want to generate a sequence of numbers. But it is commonly used in for loops to set the loop’s limit; otherwise, the loop will go to infinity and crash our system.
Let’s see some code examples…
Write the table of 4. How to do that? Firstly, we have to understand what mathematical operation we have to do to write a table. You are correct, we have to do multiplication at least ten times to write a table of a particular number. Now, the question is how we will multiply ten times. You can do that one by one, meaning first we multiply 4 with 1, then 2, and then 3 for ten times straight. Or we can use a loop, and once the multiplication for ten times is completed, we can exit the loop and print out our table of 4.
See the image below for clarity….
For loop is also used to traverse through different datastructures like lists, tuples, sets, dictionaries. I discussed it in detail in this article.
Father Give His Phone Back But On A Condition (While Loop)
Let's revisit the story and assume that our snake returned the phone to his father after ten minutes. Our snake's father also completed his important work for which he needed that phone. Now, that stubborn snake demands to use that phone again for some more time.
Now, what can a father do? He is a father, so it's obvious he has a solution, and what's the solution?
His father told him that he will give his phone back, but the snake has to complete his homework first.
Note that the father will give back his phone to the snake only if he completes his homework. (Jab tak homework pura nahi karega dobara phone nahi milega)
That ‘jab tak’ in Hindi is ‘while’ in Python. Let me explain more clearly...
While and ‘Jab Tak’
The while loop will work until a condition remains true; once it becomes false, the loop body stops. Let's understand it from our story. Our snake will not touch his father's phone again while his homework is not complete (Jab tak homework pura na ho tab tak phone nhi).
Technically, the snake's dad is saying this...
Homework_Not_Done = True
while Homework_Not_Done: #Jab tak home work pura nhi hua i.e Homework_Not_Done = True
print("Dear son, you can not use my phone")
# Loop will stop working when Homewor_Not_Done becomes False (Snake has done his homework)
Remember → ‘Jab tak’ koi condition false na ho tab tak loop chlega
Let’s take the same example of printing a table of 4, but this time we will do it using a while loop.
Logic → In a for loop, we limit the loop to run ten times using the range() function. In a while loop, we will set a condition that if the variable is greater than ten, then stop the loop. That’s the basic difference between the two loops. See the image for clarity.
Let’s understand the working of while loop in detail in the image given below..
Infinity Saga In While Loop
Remember, if the condition remains true inside a while loop, then the loop will get executed, but for how long? The answer is forever.
Yes, a while loop can run forever if the condition remains True. A while loop can only be stopped by making the condition inside the while loop False.
For example, if our snake doesn’t do his homework, he will never get a phone to use. He must break the loop, and for that, he must do the homework, meaning make Homework_Not_Done False from True.
Take the example of the table printing program. If we remove i = i+1 from our code, then i is not incrementing, meaning the condition i <= 10 will always remain true because i will always be 1, and hence, it will cause our while loop to run till infinity, which simply means forever. See the image for more clarity.
When To Use Which
Using loops in Python is not that confusing. Just remember, when you know the number of times a loop should run, you should use a for loop. And when you don’t know the number of times a loop should run, or you have some condition until which a loop should run, you should always use a while loop.
For example, in list traversal, we know the loop should run until the length of the list, so we use a for loop and set its range to the length of the list. On the other hand, if we don’t know the number of times a loop should run and we have a certain condition that must be satisfied to run a loop, then we use a while loop. For example, if we play a game, the game window should be open until we press the cross on the window. In that situation, we use a while loop.
Mom Is Angry On Father And Son (Jump Statement Overview)
Let me tell you another story about the same happy snake family; it will help you understand the concept of jump statements.
One midnight, the stubborn son had a craving for ice cream. He woke up and told his father. Both father and son agreed to eat some ice cream at midnight, but they decided not to tell the mom. Mom would be angry with both of them if she found out about anything related to eating at midnight because eating at midnight is a very bad habit.
When the father and son took out ice cream, the snake's mom woke up because she needed to pee. As she was heading to the washroom, she noticed the kitchen lights were on and became suspicious about someone being in the kitchen. She first thought there might be a thief. When she started to move into the kitchen, she saw her husband and son standing near the gas stove. She asked, "What happened to both of you?" The father replied, "Nothing, there are mosquitoes, so he and I are just lighting up some mosquito coils." After hearing this, the mom went back to bed, and both the father and son resumed eating their ice cream.
The father then explained the rules of midnight eating to his son. He said, "If you ever suspect that your mom is coming to see what's happening in the kitchen, just stop your loop of ice cream eating. Once she finds that everything is fine and no one is eating at midnight, then continue your loop of ice cream eating until your stomach is full."
Now, the question is how will the father explain these rules to the computer? Or we can ask how the father will tell the computer when to stop the loop and continue the same loop after some time or after some condition is satisfied? That’s where jump statements come into play. I hope you got some idea of how jump statements can work to alter the loop’s flow from my short story. **Let’s explain them technically.**Jump Statements In Detail
Jump Statements
Jump statements are technically used to alter the flow of our loops. Using jump statements, we can tell our loop when to stop even if the range() of a for loop is not completely executed, or if the condition of a while loop is still true, we can alter the flow of our loops. Let's see how, shall we?
Types of Jump Statements
In Python, there are three types of jump statements :
Break
Continue
Pass
Of these three, break and continue are the most important. The pass statement is just like its name; it passes control and does absolutely nothing.
Break(break
)
Let's revisit our story; trust me, it will clear up any doubts. In the story, the father and son eat ice cream at midnight until the son's mother comes into the kitchen to check what's happening. Knowing that the son’s mother is coming, the son’s father immediately puts the brakes on eating ice cream. Let’s program this using the ‘break’ keyword.
Technically, the break statement terminates the loop body.
So, technically, that's how the snake's father is telling his son when to stop.
Now let's move on from my story and try break statements on some numerical programs. See the image for clarity...
Let's play a small game. There is a secret number. If you guess it right, then it's good; if not, you have to try again until you find that number. There is no time limit. How to do that? Just see the image below...
We can also use break to find a specific element inside our list, tuple or dictionary. Let’s see how
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 100, 98, 45]
for number in numbers:
if number == 95:
print("Here is your number at index ", numbers.index(95))
break
else:
print("Number not found")
#Output -> Number not found will be our output why because 95 is not present in the list hence else part will work
Continue (continue
)
Again, let's revisit our story. The father and son duo break the loop of eating ice cream if mom comes; otherwise, they continue their loop and eat. This means they are escaping all distractions and only focusing on eating.
Technically, continue is used to skip the current iteration and move on to the next iteration of the loop. Just like the father and son duo continue their eating, skipping all distractions and only focusing on when the mom comes next. (If mom comes, then they have to stop.)
Continue simply means skip that part (condition) and continue the loop for other parts. Let’s do some programming…
Pass(pass
)
The pass statement literally used inside the loop to do nothing just pass the flow to the next statement. According to chatgpt, it does nothing when executed. It's used as a placeholder where code is syntactically required but you don't want to write anything (yet).
if condition:
pass # Do nothing (for now)
Conclusion
In this article, we explore parenting strategies through a humorous story involving a stubborn snake child who refuses to return his father's phone. The story serves as a backdrop to explain Python programming concepts of loops and jump statements. Parents born in different eras handle the situation differently; those born in the 1970s and 1980s set time limits, teaching the idea of 'for loops.' Further, conditions for returning the phone illustrate 'while loops.' The tale also delves into 'break' and 'continue' statements using another scenario where the snake and his father eat ice cream at midnight, demonstrating how loop flow can be selectively altered in programming.
At last, let me tell you a secret: both our snake and his father got caught in the kitchen while eating ice cream at midnight, and both got beaten up until the morning. Just like Shinchan and his dad always get brutally beaten by Shinchan’s mom. (I feel for them and am also afraid that my future wife might do the same to me.)
That’s it for this article. I hope you understand loops better. Thank you for reading this, and don’t forget to share it with other learners. For more articles, click this. If you have any doubts, please reach out to me on Discord at any time, and I will help. :)
#ChaiCode
Subscribe to my newsletter
Read articles from Nikhil directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Nikhil
Nikhil
Write code and poems