🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!
🖥️ BREAK in Python FOR Loops
In each example you have seen so far, the entire body of the FOR Loop is executed, on each iteration. It is all very well, but quite limited, Right? Fortunately for you, Python provides two keywords that will terminate a loop iteration, prematurely: BREAK and CONTINUE in Python.
BREAK in Python is a control flow statement that is used to exit the execution, as soon as the break is encountered. The Python break statement, immediately terminates a Loop – entirely. Consequently, the program execution then proceeds to the first statement, following the loop body.
for var in sequence:
statement_1
statement_2
if condition:
break
...
statement_3
following_statement
Let’s understand how you can use a break statement in for loop, using an example. So, let’s say you have a list with strings, as items. You want to exit the loop using the break statement, as soon as the desired string is encountered.
youtube_page = ['D','I','G','I','T','A','L',' ','A','C','A','D','E','M','Y']
for letter in youtube_page:
if letter == ' ':
break
print(letter)
In the above example, as soon as the loop encounters the space character “ ”, it will enter the IF statement block where the break statement will exit the loop, straight away. Thus, it will only print DIGITAL. Did you get it?
○ Exercise
Then let’s practice – it’s your turn now. Suppose you have a list of colours: [‘red’, ‘green’, ‘blue’, ‘yellow’], and you want to print each colour, but break the loop when it encounters ‘blue’. How would you proceed? What output would you get?
colours = ['red', 'green', 'blue', 'yellow']
for colour in colours:
if colour == 'blue':
break
print(colour)
First, Let’s declare a list, in which you added all of the colours – [‘red’, ‘green’, ‘blue’, ‘yellow’]. Then, iterate throughout this list, using for statement: for colour in colours. It will create an iterator, and the variable colour will get the next value, on each iteration, until the execution has reached the last item in the sequence.
When it encounters the item ‘blue’, you do not want to execute the code that comes next, but exit the loop – if colour == ‘blue’: break. In all the other cases, as it would be with an “else” statement, you want to print the current colour.
As you may have guessed the loop will print ‘red’, ‘green’ and stop – because the next item is blue. Congrats – You just wrote your first for loop, on your own!
Let’s play this video, stick around and watch until the end of this video! 👍🏻
– Digital Academy™ 🎓
***
☞ WATCH NEXT:
○ Data Types in Python – https://youtu.be/cweUByxBWiU
○ Operators in Python – https://youtu.be/-wDaVLkKOiU
○ IF Statements in Python – https://youtu.be/CC5seZ6OBJ4
○ FOR Loops in Python – https://youtu.be/JgH-D5DSTho
☞ WATCH MORE:
○ HOW TO Learn Python? Python Tutorial for Beginners [FULL Course] https://youtu.be/9hvnSZPMtuw
📖 Blog: http://digital.academy.free.fr/blog/
📖 Complete Python Development Course for Beginners [PLAYLIST]: http://digital.academy.free.fr/playlist/python-development-for-beginners
🛒 Shopping and Discounts: http://digital.academy.free.fr/store/
#Python #Tutorial #Beginners #Shorts
***
♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.
***
♡ FOLLOW US ♡
✧ http://digital.academy.free.fr/
✧ https://twitter.com/DigitalAcademyy
✧ https://www.facebook.com/digitalacademyfr
✧ https://www.instagram.com/digital_academy_fr/
✧ https://www.youtube.com/c/DigitalAcademyOnline
♡ SUPPORT US ♡
✧ http://digital.academy.free.fr/join
✧ http://digital.academy.free.fr/donate
✧ http://digital.academy.free.fr/subscribe
✧ https://www.patreon.com/digital_academy
✧ https://www.buymeacoffee.com/digital_academy
***