ELSE in Python WHILE Loops - Python Tutorial for Beginners

Veröffentlicht am: 08 März 2021
auf dem Kanal: Digital Academy
365
10

🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!

🖥️ ELSE in Python WHILE Loops

The common construct is to run a Loop, and search for an item. If the item is found, we break out of the loop, using the break statement. But, there are 2 scenarios in which the loop may end:

The first one is when the item is found and break is encountered.
The second scenario is that the loop ends without encountering a break statement.

Now we may want to know which one of these, is the reason for a loop’s completion. One method is to set a flag, and then check it once the loop ends. Another, is to use an optional else clause, at the end of for loop.

The ELSE clause in Python will be executed only if the loop terminates naturally, through the exhaustion of the iterable object. On the other hand, if the loop terminates prematurely, with break, the else clause will not be executed - at all.

--

Here is the basic structure, of a while-else loop. First, let's assume you declared a list with some colours inside. Now, you want to search for a specific item - which in that case, is the colour blue.

If you find it, you want to break this loop, then continue with the next statements, after the loop. But, on the other hand, if you didn't find that item, you want to execute some code first, then and only then, execute the same code that if you found it, and proceed with the rest of your code.

As you may have guessed, the function not_found_in_iterable() will never be called in this example, since the while loop will terminate prematurely - as soon as it encounters the item 'blue'.

colours = ['red', 'green', 'blue', 'yellow']

while colours:

colour = colours.pop()

Found it!
if colour == 'blue':
print("Found", colour)
break

else:
Didn't find anything!
not_found_in_iterable()

always_executed()


Let's play this video, stick around and watch until the end of this video! 👍🏻

Digital Academy™ 🎓

***

☞ WATCH NEXT:
○ Data Types in Python -    • DATA TYPES in Python (Numbers, Strings, Li...  
○ Operators in Python -    • OPERATORS in Python (Arithmetic, Assignmen...  
○ IF Statements in Python -    • CONDITIONAL Statements in Python (IF, ELIF...  
○ FOR Loops in Python -    • FOR Loop in Python (Syntax, Break, Continu...  

📖 Blog: http://digital.academy.free.fr/blog

📖 [FULL Course] HOW TO Learn Python? Python Tutorial for Beginners:    • 🐍 Python 101: Learn Python Basics for Abso...  

📖 [PLAYLIST] Complete Python Development Course for Beginners: http://digital.academy.free.fr/playli...

🧑‍🎓 [COURSE] http://digital.academy.free.fr/courses

📘 [BOOK] Python for Absolute Beginners: https://amzn.to/3NvyOWV

🛒 Shopping and Discounts: http://digital.academy.free.fr/store

💌 Weekly Newsletter for Developers: https://www.getrevue.co/profile/digit...

#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/
✧   / digitalacademyy  
✧   / digitalacademyfr  
✧   / digital_academy_fr  
✧    / digitalacademyonline  

♡ SUPPORT US ♡
✧ http://digital.academy.free.fr/join
✧ http://digital.academy.free.fr/store
✧ http://digital.academy.free.fr/donate
✧ http://digital.academy.free.fr/subscribe
✧   / digital_academy  
✧ https://www.buymeacoffee.com/digital_...

***


Auf dieser Seite können Sie das Online-Video ELSE in Python WHILE Loops - Python Tutorial for Beginners mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Digital Academy 08 März 2021 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 365 Mal angesehen und es wurde von 10 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!