Infinite WHILE Loops in Python

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

🖥️ Infinite WHILE Loops in Python

Suppose you write a while loop that, theoretically, never ends. Sounds weird, right?!

Well, that kind of loop, also known as infinite loop in Python, might be very useful in client / server programming, where the server needs to run continuously so that the client can communicate with it – as and when required.

As you may have understood, an infinite while loop in Python executes infinite times, which means: theoretically, the execution never stops. This may come as a surprise, but it has its own advantages – and its disadvantages, as well. Therefore, you must use it with caution, because of the possibility that this condition never resolves to a False value – and may never exit this infinite loop in Python.

Let’s have a look at a more practical example. This program will run infinite iterations, unless you press Ctrl+C, or put a break control statement in the loop. The condition within the while statement must eventually become False. Otherwise, the loop will execute forever – creating an infinite loop!

while True:
         print('Press Ctrl+C to stop me!')

Fortunately, you can safely implement an infinite loop in your program, using the break control statement that you have seen little bit earlier. This way, as soon as the condition is met, this break keyword will immediately interrupt the current iteration, exit the infinite loop, and proceed with the next statements – outside of the loop.

while True:
    name = input('Enter name:')
    if name == 'stop': break
    print('Hello', name)

OUTPUT:
Enter name:Alicia
Hello Alicia
Enter name:Bob
Hello Bob
Enter name:stop


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