Day: March 15, 2021

Python Example – Write a Python program calculates the date six months from the current date using the datetime module

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program calculates the date six months from the current date using the datetime module.   Sample Solution: Python Code: import datetime print((datetime.date.today() + datetime.timedelta(6*365/12)).isoformat()) Sample Output: 2017-11-04   Write a Python program calculates the date six months from the current date using …

Python Example – Write a Python program to add a month with a specified date

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to add a month with a specified date.   Sample Solution: Python Code: from datetime import date, timedelta import calendar start_date = date(2014, 12, 25) days_in_month = calendar.monthrange(start_date.year, start_date.month)[1] print(start_date + timedelta(days=days_in_month)) Sample Output: 2015-01-25   Write a Python program to …

Python Example – Write a Python program to get the number of days of a given month and year.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to get the number of days of a given month and year.   Sample Solution: Python Code: from calendar import monthrange year = 2016 month = 2 print(monthrange(year, month)) Sample Output: (0, 29)   Write a Python program to get the …

Python Example – Write a Python program to get the last day of a specified year and month.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to get the last day of a specified year and month.   Sample Solution: Python Code: import calendar year = 2015 month = 2 print(calendar.monthrange(year, month)[1]) Sample Output: 28   Write a Python program to get the last day of a …

Python Example – Write a Python program to get the date of the last Tuesday.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to get the date of the last Tuesday.   Sample Solution: Python Code: from datetime import date from datetime import timedelta today = date.today() offset = (today.weekday() – 1) % 7 last_tuesday = today – timedelta(days=offset) print(last_tuesday) Sample Output: 2017-05-02   …

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

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to get days between two dates.   Sample Solution: Python Code: from datetime import date a = date(2000,2,28) b = date(2001,2,28) print(b-a) Sample Output: 366 days, 0:00:00   Write a Python program to get days between two dates Free Machine Learning …

Python Example – Write a Python program to drop microseconds from datetime.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to drop microseconds from datetime.   Sample Solution: Python Code: import datetime dt = datetime.datetime.today().replace(microsecond=0) print() print(dt) print() Sample Output: 2021-03-14 14:17:47   Write a Python program to drop microseconds from datetime Free Machine Learning & Data Science Coding Tutorials in …

Python Example – Write a program to add year(s) with a given date and display the new date.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a program to add year(s) with a given date and display the new date.   Sample Solution: Python Code: import datetime from datetime import date def addYears(d, years): try: return d.replace(year = d.year + years) except ValueError: return d + (date(d.year + years, 1, …

Python Example – Write a Python program to select all the Sundays of a specified year.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to select all the Sundays of a specified year.   Sample Solution: Python Code: from datetime import date, timedelta def all_sundays(year): dt = date(year, 1, 1) dt += timedelta(days = 6 – dt.weekday()) while dt.year == year: yield dt dt += …

Python Example – Write a Python program to find the date of the first Monday of a given week

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to find the date of the first Monday of a given week. Sample Solution: Python Code: import time print(time.asctime(time.strptime(‘2015 50 1’, ‘%Y %W %w’))) Sample Output: Mon Dec 14 00:00:00 2015   Write a Python program to find the date of …