🎥 VLOG – PART 2 (Continue, Break, For Loop, Range + Bank Program)
“Hey everyone! Today I’m explaining some important Python loop concepts.
First let's dive into what we learned yesterday, continue and break keywords.
continue is used to skip the current iteration and move to the next loop cycle.
break is used to exit the loop completely.
Both of these cannot be used outside a loop and are usually written with an if condition.
For loop
A for loop is used to iterate through elements of an iterable one by one, so it’s often called a for-each loop.
The syntax is simple:
for variable in iterable:
execute code for each item.
You can also use for with else, where the else block runs after the loop finishes all iterations.
Now let’s compare while vs for loop:
_while loop is best when we don’t know the exact number of iterations and it’s condition-based.
for loop is best when we know the number of iterations and it’s iterable-based._
Next comes the range() function, which generates a sequence of integers.
Syntax: range(start, stop, step)
-start is inclusive
-stop is exclusive
-step controls the gap
Positive step gives a forward sequence, negative step gives a reverse sequence, and step 0 causes an error.
Now let’s see a banking example using a loop.
The program asks the user to choose options like withdraw, deposit, check balance, or stop.
Using a while True loop keeps the program running continuously until the user enters the stop code.
Inside the loop, if and elif conditions handle each banking operation, and when the user chooses the stop option, the break statement exits the loop and the program prints Thank you.
That’s how loops and conditions work together in a real Python program!”
#python #Day1 #DataScience #bhartiinsan
Top comments (0)