Month: March 2021

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 …

Python Example – Write a Python program to get week number.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to get week number.   Sample Solution: Python Code: import datetime print(datetime.date(2015, 6, 16).isocalendar()[1]) Sample Output: 25   Write a Python program to get week number Free Machine Learning & Data Science Coding Tutorials in Python & R for Beginners. Subscribe …

Python Example – Write a Python program to get current time in milliseconds

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to get current time in milliseconds.   Sample Solution: Python Code: import time milli_sec = int(round(time.time() * 1000)) print(milli_sec) Sample Output: 1494055960573   Write a Python program to get current time in milliseconds Free Machine Learning & Data Science Coding Tutorials …

Python Example – Write a Python program to convert Year/Month/Day to Day of Year

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to convert Year/Month/Day to Day of Year.   Sample Solution: Python Code: import datetime today = datetime.datetime.now() day_of_year = (today – datetime.datetime(today.year, 1, 1)).days + 1 print(day_of_year) Sample Output: 126   Write a Python program to convert Year/Month/Day to Day of …

Python Example – Write a Python program to print the number of prime numbers which are less than or equal to a given integer

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to add 5 seconds with the current time.   Sample Solution: Python Code: import datetime x= datetime.datetime.now() y = x + datetime.timedelta(0,5) print(x.time()) print(y.time()) Sample Output: 12:37:43.350241 12:37:48.350241   Write a Python program to add 5 seconds with the current time …

Python Example – Write a Python program to print next 5 days starting from today.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to print next 5 days starting from today.   Sample Solution: Python Code: import datetime base = datetime.datetime.today() for x in range(0, 5): print(base + datetime.timedelta(days=x)) Sample Output: 2021-03-15 12:27:53.632939 2021-03-15 12:27:53.632939 2021-03-15 12:27:53.632939 2021-03-15 12:27:53.632939 2021-03-15 12:27:53.632939   Write a …

Python Example – Write a Python program to convert the date to datetime (midnight of the date).

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to convert the date to datetime (midnight of the date).   Sample Solution: Python Code: from datetime import date from datetime import datetime dt = date.today() print(datetime.combine(dt, datetime.min.time())) Sample Output: 2021-03-15 00:00:00   Write a Python program to convert the date …

Python Example – Write a Python program to print yesterday, today, tomorrow

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to print yesterday, today, tomorrow.   Sample Solution: Python Code: import datetime today = datetime.date.today() yesterday = today – datetime.timedelta(days = 1) tomorrow = today + datetime.timedelta(days = 1) print(‘Yesterday : ‘,yesterday) print(‘Today : ‘,today) print(‘Tomorrow : ‘,tomorrow) Sample Output: Yesterday …

Python Example – Write a Python program to subtract five days from current date.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to subtract five days from current date.   Sample Solution: Python Code: from datetime import date, timedelta dt = date.today() – timedelta(5) print(‘Current Date :’,date.today()) print(‘5 days before Current Date :’,dt) Sample Output: Current Date : 2017-05-05 5 days before Current …

Python Example – Write a Python program to get the current time

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to get the current time.   Sample Solution: Python Code: import datetime print(datetime.datetime.now().time()) Sample Output: 18:08:21.552122   Write a Python program to get the current time Free Machine Learning & Data Science Coding Tutorials in Python & R for Beginners. Subscribe …