Avatar ·

How to properly create a timed loop?

📁 Advanced Python Topics

I need to create a loop that will execute a function for one minute with a 1-second interval.

What is the most efficient way? I couldn't think of anything better than this:

 from time import sleep seconds_left = 60 while seconds_left > 0:      do_something()      seconds_left -= 1      sleep(1)
Avatar ·

I found a pretty elegant way to rotate a matrix in one line without numpy and loops. Couldn't find anything decent in Russian resources, maybe it will help someone. Original here:

rotated = zip(*original[::-1]) # Python 2rotated = tuple(zip(*original[::-1])) # Python 3

How it works.

original = [[1, 2],            [3, 4]]

First the reverse works

 >>> original[:

Log in to leave an answer

Blogs