Date and Time Functions in Python

Date and Time Functions in Python

In Python, date, time and date time classes provides a number of function to deal with dates, times and time intervals. Date and date time are an object in Python, so when you manipulate them, you are actually manipulating objects and not string or timestamps…

पायथन में, दिनांक, समय और डेटटाइम क्लासेज दिनांक, समय और समय अंतराल से निपटने के लिए कई प्रकार के फंक्शन प्रदान करता है। पायथन में डेट और डेटटाइम एक ऑब्जेक्ट हैं, इसलिए जब आप उन्हें हेरफेर करते हैं, तो आप वास्तव में ऑब्जेक्ट्स में हेरफेर कर रहे हैं न कि स्ट्रिंग या टाइमस्टैम्प्स ।

Whenever you manipulate dates or time, you need to import date time function. To import the date time modules follow below steps:

जब भी आप तारीखों या समय में हेरफेर करते हैं, तो आपको डेटटाइम फंक्शन को इंपोर्ट करने की आवश्यकता होती है। दिनांक समय मॉड्यूल इंपोर्ट करने के लिए चरणों का पालन करें।

Step 1: Below statements tells the Python interpreter that from the date time module import the date class. We are not writing the code for this date functionality alas just importing it for our use.

स्टेप 1– नीचे दिए गए स्टेटमेंट पायथन इंटरप्रेटर को बताते हैं कि डेटटाइम मॉड्यूल से डेट क्लास इंपोर्ट करते हैं। हम इस तिथि की कार्यक्षमता के लिए कोड नहीं लिख रहे हैं, बस हमारे उपयोग के लिए इसे इंपोर्ट कर रहे हैं।

from date time import date
from date time import time
from date time import date time

Step 2: After import corresponding classes, Create an instance of the date object. To get today’s date from today method from the date class.

स्टेप 2– करस्पॉन्डिंग क्लासेज इंपोर्ट करने के बाद, दिनांक ऑब्जेक्ट का एक उदाहरण बनाएँ। आज की तारीख से today() डेट क्लास से मेथड प्राप्त करने के लिए।

today = date. today ()

Step 3: Print the date, using print() function

स्टेप 3-प्रिंट() फंक्शन का उपयोग करके दिनांक प्रिंट करें

print (“Today’s date : “, today)

Step 4: date.today() has several properties:

स्टेप 4-date.today() में कई गुण हैं।

  • We can print individual day/month/year and many other things. Here in below code mentioned date, month and year separately.

हम अलग-अलग दिन/महीना/वर्ष और कई अन्य चीजें प्रिंट कर सकते हैं। यहां नीचे दिए गए कोड में तारीख, महीना और साल अलग-अलग बताया गया है।

today.day   =>  current day – वर्तमान दिन
today.month => current month – वर्तमान माह
today. Year => current year – वर्तमान वर्ष

  • date.today () function also gives you the weekday number. Here is the Weekday start with Monday as 0 and Sunday as 6.

date. today () फंक्शन आपको वीकडे नंबर भी देता है। यहां वीकडे की शुरुआत सोमवार के रूप में 0 और रविवार को 6 के रूप में होती है।

today.weekday ( )

Weekday Number is useful for arrays whose index is dependent on the Day of the week.

सप्ताह का दिन उन सरणियों के लिए उपयोगी है, जिनका सूचकांक सप्ताह के दिन पर निर्भर है।

Step 5: Like Date Objects, we can also use “DATETIME OBJECTS” in Python. It gives date along with time in hours, minutes, seconds and milliseconds.

स्टेप 5-दिनांक ऑब्जेक्ट्स की तरह, हम पायथन में भी “DATETIME OBJECTS” का उपयोग कर सकते हैं। यह समय के साथ-साथ घंटे, मिनट, सेकंड और मिलीसेकंड में समय देता है।

today = datetime.now()

It gives the output with current date and time. Suppose we want to print just the current time without the date.

यह वर्तमान दिनांक और समय के साथ आउटपुट देता है। मान लीजिए कि हम तारीख के बिना सिर्फ वर्तमान समय प्रिंट करना चाहते हैं।

t = datetime.time (datetime.now())

Let us see an Example
एक उदाहरण देखते हैं
from datetime import date
from datetime import time
from datetime import datetime
# Get today’s date from datetime class
today=datetime.now ()
print (today)
# Get the current time
t = datetime.time (datetime.now())
print (“The current time is”, t)
#weekday returns 0 (monday) through 6 (sunday)
wd=date. weekday (today)
#Days start at 0 for monday
days= [“monday”, “tuesday”, “wednesday.”, “thursday”, “friday”,”saturday”, “sunday” ]
print (“Today is day number %d” % wd)
print (“which is a ” + days [wd])

Output:
2019-10-08 16:39:18.197156
The current time is 16:39:18.212157
Today is day number 1
which is a Tuesday

Recursion रिकर्सन

Recursion is a method of programming or coding a problem, in which a function calls itself one or more times in its body.

रिकर्सन प्रोग्रामिंग या किसी समस्या को कोड करने की एक विधि है, जिसमें एक फंक्शन अपने बॉडी में एक या अधिक बार कॉल करता है।

Usually, it is returning the return value of this function call. If a function definition satisfies the condition of recursion, we call this function a recursive function.

आमतौर पर, यह इस फंक्शन कॉल का रिटर्न मान लौटा रहा है। यदि एक फंक्शन परिभाषा पुनरावृत्ति की स्थिति को संतुष्ट करती है, तो हम इस फंक्शन को एक पुनरावर्ती फंक्शन कहते हैं।

A recursive function has to fulfill an important condition to be used in a program: it has to terminate. A recursive function terminates, if with every recursive call the solution of the problem is downsized and moves towards a base case. A base case is a case, where the problem can be solved without further recursion. A recursion can end up in an infinite loop, if the base case is not met in the calls.

एक रिकर्सिव फंक्शन को एक प्रोग्राम में उपयोग करने के लिए एक महत्वपूर्ण शर्त को पूरा करनाः इसे समाप्त करना है। एक रिकर्सिव फंक्शन समाप्त हो जाता है, यदि प्रत्येक रिकर्सिव कॉल के साथ समस्या का समाधान कम हो जाता है और बेस केस की ओर बढ़ जाता है। आधार मामला एक ऐसा मामला है, जहां समस्या को बिना किसी पुनरावृत्ति के हल किया जा सकता है। आधार लूप कॉल में नहीं मिलने पर, एक अनंत लूप में समाप्त हो सकता है।

For example – उदाहरण के लिए: factorial actual formula factorial(n) = n * factorial(n-1)

Factorial(3)  =>3 * factorial(2)
                       =>3* 2 * factorial(1)
                       =>3 * 2 * 1 * factorial(0)
                       =>3*2*1*1
                       =>Factorial(3) => 6

The main advantages of recursive functions are
रिकर्सिव फंक्शन के मुख्य लाभ हैं

  1. we can reduce length of the code and improve readability

हम कोड की लेंथ कम कर सकते हैं और पठनीयता में सुधार कर सकते हैं

  1. we can solve complex problems very easily.

हम बहुत आसानी से कॉम्प्लेक्स समस्याओं को हल कर सकते हैं।

Example 1: Write a program to find factorial of given number with recursion.
रिकर्सन के साथ दिए गए संख्या के भाज्य को फाइंड के लिए एक कार्यक्रम लिखें।

def fact (n): 
    if n == 0:
        result = 1 
    else:
        result = n * fact (n-1) 
    return result
print("Factorial of given number is : ", fact(int (input("Enter a number : ")))) 

Output:
Enter a number : 5
Factorial of given number is : 120

Example 2: Write a program to calculate GCD using recursive functions.
रिकर्सिव फंक्शन का उपयोग करके GCD की गणना करने के लिए एक प्रोग्राम लिखें।

def GCD (a, b) :
    reminder = a % b
    if (reminder == 0):
        return b 
    else:
        return GCD (b, reminder)
x = int (input ("Enter 1st number : ") ) 
y = int (input ("Enter 2nd 'number: ")) 
print ("GCD is : ", GCD (x, y))

Output:
Enter 1st number : 100
Enter 2nd number : 10
GCD is : 10 .

Example 3: Write a program to print the Fibonacci series using recursion.
रिकर्सन का उपयोग करके Fibonacci सीरीज को मुद्रित करने के लिए एक प्रोग्राम लिखें।

def fab (n): 
    if n<2:
        return 1 
    else:
        return fab (n-1) +fab (n-2)
fabseries = int (input('Enter a number :')) 
for i in range (fabseries):
    print(i, " = ", fab(i))

Output:
Enter a number :10
0  =  1
1  =  1
2  =  2
3  =  3
4  =  5
5  =  8
6  =  13
7  =  21
8  =  34
9  =  55
Chapter wise Model Paper Link

About Me