From the course: Python Essential Training

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

While

While

- While loops look innocuous, but they're one of the most dangerous things you can write in Python. One small mistake and your program will run forever. Granted, some programs we want to run forever, like a web service, but your program might run forever and not in a good way. But don't be afraid, while you're here, you'll learn advanced while loop control statements to tame these wild beasts. Let's go to the code. One thing I like to use to test while loops is the Python datetime class, from datetime import datetime. All right and sure, we could increment some numbers in our while loop to check if we've reached a particular number yet, but why wouldn't we just use a for loop for that? Checking the actual time is much more exciting and we can get the current date and time by calling datetime.now() and if we just want the seconds in the current minute, we can use .second so we're 34 seconds into the current minute. Now, if we want to get two seconds into the future, just add 2, but you…

Contents