...

/

ما هي مكتبة Python القياسية؟

ما هي مكتبة Python القياسية؟

استكشف بعض المهام مفتاح التي يتم تنفيذها باستخدام مكتبة Python القياسية.

سنغطي ما يلي...

The Python Standard Library (PSL), is a collection of pre-defined modules that contain many useful utilities which spare us a lot of time. These include different types of complex mathematical functions, high-level data types, network programming tools, and this is just the tip of the iceberg!

Let’s dive in.

datetime

Let’s start by importing the datetime module which contains several methods related to the current date and time. As always, these methods can be accessed using the . operator:

Press + to interact
# Importing modules at the beginning of the program is a good practice
import datetime
date_today = datetime.date.today() # Current date
print(date_today)
time_today = datetime.datetime.now()
print(time_today.strftime("%H:%M:%S")) # Current time
print(time_today.strftime("%A, %B %d, %Y"))
print(time_today.strftime("%I:%M:%S %p"))
print(time_today.strftime("%Y-%m-%d %H:%M:%S"))

Explanation

Here is the explanation of the code provided above:

  • Line 2: Imports datetime module.

  • Lines 3–4: today is a method of the datetime.date class that returns the current date as a datetime.date object. The returned object is then stored in the date_today variable and displayed in the format YYYY-MM-DD (e.g., 2024-07-30).

  • Line 6: now is a method of the datetime class within the datetime module. Note that this is different from datetime.date used earlier. It returns the current date and time as a datetime.datetime object which is then stored inside the time_today variable.

  • Lines 7–10: strftime is method of the datetime.datetime object that allows you to format the date and time string according to a specific ...