r/learnpython • • Dec 23 '19

What are some creative small programs that you have coded using Python?

I am a beginner at coding with Python, so I am curious about learning what small programs or projects you all have created. I am hoping to develop my skills using Python, so I figured looking at actual code for various programs would greatly help.

Please include a short description of what the program does and the code itself in the comments below. Thank you!

348 Upvotes

224 comments sorted by

View all comments

Show parent comments

0

u/[deleted] Dec 23 '19

[deleted]

3

u/kwollms Dec 23 '19 edited Dec 23 '19

Not 100% sure I’m correct in thinking this but: I would imagine that it wouldn’t be much different than your computer endlessly counting time in other words something like this wouldn’t be very resource heavy...

0

u/[deleted] Dec 23 '19

[deleted]

6

u/KJ7GES Dec 23 '19
import time
while True:
    if get_time_since_last_input() > some_number:
        shut_down()
    else:
        time.sleep(some_number_of_seconds)

adding time.sleep lets you delay the program's execution some number of seconds. So instead of running the while True: loop as fast as possible, you could put even a 0.5 second delay in the program which would reduce the load. In reality, a much larger delay (1 minute? 5 minutes?) would probably be acceptable for this use.

Other alternative is to remove the loop altogether and run the script on an interval using something like Windows Task Scheduler (or linux cron, etc).

2

u/kwollms Dec 23 '19

Possibly, I haven’t even looked into documentation for this yet so I don’t think I can speak to that yet...

0

u/Nixellion Dec 23 '19

use time.sleep and it wont