Computer >> Máy Tính >  >> Lập trình >> Python

Làm cách nào để tìm ngày đầu tiên của một năm nhất định bằng Python?

Trong chương trình này, chúng tôi phải in ngày đầu tiên của năm. Chúng tôi phải mất một năm làm đầu vào của người dùng.

Thuật toán

Step 1: Import the datetime library.
Step 2: Take year as input from the user.
Step 3: Get the first day of the year by passing month, day and year as parameters to the datetime.datetime() function
Step 4: Display the first day using strftime() function.

Mã mẫu

import datetime
year = int(input("Enter year: "))
firstday = datetime.datetime(year, 1,1)
print("First Day of ", year, " = ", firstday.strftime("%A"))

Đầu ra

First Day of  2021  =  Friday