Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can I increase the value of every item in the list by 1 in every 1 minute in Python?

I would like to do it by constantly checking local time in my computer. Is there also a way to do it by NOT checking with time of local machine?
I want to also program not execute certain statements during specific times on my local machine. Let’s say it does not execute the if statement between 5 and 7 pm.
I would appreciate if someone helped in this topic, please.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Suppose I have a list lst in which I want to append numbers from 0 to 10, once per minute I can do this.

import time

lst = []

for i in range(10):
  lst.append(i)

  time.sleep(60)

Now suppose in the example above, you don’t want to append the numbers to the list if the current time is between 5 pm and 7 pm use this.

import datetime
lst = []

for i in range(10):
  current_hour = datetime.datetime.now().hour
  
  if current_hour >= 17 and current_hour < 19:
    pass

  else:
    lst.append(i)

datetime.datetime.now() gets you the current datetime. From this, you can use hour attribute to get the current hour but the hour is in 24hr format.


These two are just arbitrary examples but would help you understand how you can code time into Python.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading