Day: March 15, 2021

Python Example – Write a Python program to convert degree to radian.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to convert degree to radian. Note : The radian is the standard unit of angular measure, used in many areas of mathematics. An angle’s measurement in radians is numerically equal to the length of a corresponding arc of a unit circle; …

Python Example – Write a Python program to get a list of dates between two dates.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to get a list of dates between two dates.   Sample Solution: Python Code: from datetime import timedelta, date def daterange(date1, date2): for n in range(int ((date2 – date1).days)+1): yield date1 + timedelta(n) start_dt = date(2015, 12, 20) end_dt = date(2016, …

Python Example – Write a Python program to get the current date time information.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to get the current date time information.   Sample Solution: Python Code: import time import datetime print() print(“Time in seconds since the epoch: %s” %time.time()) print(“Current date and time: ” , datetime.datetime.now()) print(“Alternate date and time: ” ,datetime.datetime.now().strftime(“%y-%m-%d-%H-%M”)) print(“Current year: “, …

Python Example – Write a Python program to calculate an age in year.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to calculate an age in year.   Sample Solution: Python Code: from datetime import date def calculate_age(dtob): today = date.today() return today.year – dtob.year – ((today.month, today.day) < (dtob.month, dtob.day)) print() print(calculate_age(date(2006,10,12))) print(calculate_age(date(1989,1,12))) print() Sample Output: 10 28   Write a …

Python Example – Write a Python program to convert two date difference in days, hours, minutes, seconds.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to convert two date difference in days, hours, minutes, seconds.   Sample Solution: Python Code: from datetime import datetime, time def date_diff_in_seconds(dt2, dt1): timedelta = dt2 – dt1 return timedelta.days * 24 * 3600 + timedelta.seconds def dhms_from_seconds(seconds): minutes, seconds = …

Python Example – Write a Python program to calculate a number of days between two dates.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to calculate a number of days between two dates.   Sample Solution: Python Code: import datetime from datetime import date def differ_days(date1, date2): a = date1 b = date2 return (a-b).days print() print(differ_days((date(2016,10,12)), date(2015,12,10))) print(differ_days((date(2016,3,23)), date(2017,12,10))) print() Sample Output: 307 -627 …

Python Example – Write a Python program convert a string date to the timestamp

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program convert a string date to the timestamp.   Sample Solution: Python Code: import time import datetime s = “01/10/2016” print() print(time.mktime(datetime.datetime.strptime(s, “%d/%m/%Y”).timetuple())) print() Sample Output: 1494225605.0   Write a Python program convert a string date to the timestamp Free Machine Learning …

Python Example – Write a Python program to get the GMT and local current time.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to get the GMT and local current time.   Sample Solution: Python Code: from time import gmtime, strftime import time print(“nGMT: “+time.strftime(“%a, %d %b %Y %I:%M:%S %p %Z”, time.gmtime())) print(“Local: “+strftime(“%a, %d %b %Y %I:%M:%S %p %Zn”)) Sample Output: GMT: Mon, …

Python Example – Write a Python program to get the dates 30 days before and after from the current date

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to get the dates 30 days before and after from the current date.   Sample Solution:- Python Code: from datetime import date, timedelta current_date = date.today().isoformat() days_before = (date.today()-timedelta(days=30)).isoformat() days_after = (date.today()+timedelta(days=30)).isoformat() print(“nCurrent Date: “,current_date) print(“30 days before current date: “,days_before) …

Python Example – Write a Python program to create 12 fixed dates from a specified date over a given period.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to create 12 fixed dates from a specified date over a given period. The difference between two dates will be 20.   Sample Solution: Python Code: import datetime def every_20_days(date): print(‘Starting Date: {d}’.format(d=date)) print(“Next 12 days :”) for _ in range(12): …