BREAK in Python WHILE Loops

🎓 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 WHILE Loops

Break in Python is a control flow statement that is used to exit the execution of a Loop, 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.

     while expression is True:
         statement_1
         statement_2
         if condition is True:
             break
         ...
         statement_3
         ...
     following_statement



Let’s understand how you can use a break statement in while 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']
     
     while youtube_page:
         letter = youtube_page.pop()
         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?

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 color, but break the loop when it encounters ‘blue’. How would you proceed? What output would you get?

     colours = ['red', 'green', 'blue', 'yellow']
     
     while colours:
         colour = colours.pop()
         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 while statement in Python: while colours.

It will create an iterator, and the variable colour will get the first value inside the list, on each iteration, until the execution has reached the last item in the sequence and the list becomes empty.

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 in Python, you want to print out the current colour.

As you may have guessed the loop will print ‘red’, ‘green’ and stop as soon as it has encountered item blue. Congrats – You just wrote your first while 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

***

Copy link
Powered by Social Snap