Trong Python, mô-đun lịch của thư viện chuẩn cung cấp các hàm để xác định xem một năm nhất định có phải là năm nhuận hay không và trả về số năm nhuận trong một khoảng thời gian đã chỉ định - lịch — Các chức năng chung liên quan đến lịch — Python 3. 10. 4 tài liệu
Bài viết này mô tả các nội dung sau - Thuật toán cho năm nhuận
- Xác định xem một năm có phải là năm nhuận hay không.
if (year is not divisible by 4) then (it is a common year)
else if (year is not divisible by 100) then (it is a leap year)
else if (year is not divisible by 400) then (it is a common year)
else (it is a leap year)
4 - Đếm số năm nhuận trong một khoảng thời gian xác định.
if (year is not divisible by 4) then (it is a common year)
else if (year is not divisible by 100) then (it is a leap year)
else if (year is not divisible by 400) then (it is a common year)
else (it is a leap year)
5 - Liệt kê các năm nhuận trong một khoảng thời gian xác định
- Xác định xem một đối tượng
import calendar
print(calendar.isleap(2019))
# False
print(calendar.isleap(2020))
# True
print(calendar.isleap(1900))
# False
print(calendar.isleap(2000))
# True
0 hoặc import calendar
print(calendar.isleap(2019))
# False
print(calendar.isleap(2020))
# True
print(calendar.isleap(1900))
# False
print(calendar.isleap(2000))
# True
1 có phải là năm nhuận hay không
Xem bài viết sau về cách lấy lịch văn bản hoặc HTML với mô-đun lịch - Nhận lịch dưới dạng văn bản, HTML, danh sách trong Python
Liên kết được tài trợ Thuật toán cho năm nhuậnThuật toán xác định xem một năm là năm nhuận hay năm thường trong lịch Gregorian được biểu diễn bằng mã giả sau if (year is not divisible by 4) then (it is a common year)
else if (year is not divisible by 100) then (it is a leap year)
else if (year is not divisible by 400) then (it is a common year)
else (it is a leap year)
Xác định xem một năm có phải là năm nhuận hay không. if (year is not divisible by 4) then (it is a common year)
else if (year is not divisible by 100) then (it is a leap year)
else if (year is not divisible by 400) then (it is a common year)
else (it is a leap year)
4Sử dụng if (year is not divisible by 4) then (it is a common year)
else if (year is not divisible by 100) then (it is a leap year)
else if (year is not divisible by 400) then (it is a common year)
else (it is a leap year)
4 để kiểm tra xem một năm có phải là năm nhuận hay khôngimport calendar
print(calendar.isleap(2019))
# False
print(calendar.isleap(2020))
# True
print(calendar.isleap(1900))
# False
print(calendar.isleap(2000))
# True
nguồn. Mã nguồn của if (year is not divisible by 4) then (it is a common year)
else if (year is not divisible by 100) then (it is a leap year)
else if (year is not divisible by 400) then (it is a common year)
else (it is a leap year)
4 như sau. Toán tử import calendar
print(calendar.isleap(2019))
# False
print(calendar.isleap(2020))
# True
print(calendar.isleap(1900))
# False
print(calendar.isleap(2000))
# True
5 (modulo) và import calendar
print(calendar.isleap(2019))
# False
print(calendar.isleap(2020))
# True
print(calendar.isleap(1900))
# False
print(calendar.isleap(2000))
# True
6, import calendar
print(calendar.isleap(2019))
# False
print(calendar.isleap(2020))
# True
print(calendar.isleap(1900))
# False
print(calendar.isleap(2000))
# True
7 được sử dụng- Toán tử Boolean trong Python (and, or, not)
def isleap(year):
"""Return True for leap years, False for non-leap years."""
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
nguồn. Đếm số năm nhuận trong một khoảng thời gian xác định. if (year is not divisible by 4) then (it is a common year)
else if (year is not divisible by 100) then (it is a leap year)
else if (year is not divisible by 400) then (it is a common year)
else (it is a leap year)
5Sử dụng if (year is not divisible by 4) then (it is a common year)
else if (year is not divisible by 100) then (it is a leap year)
else if (year is not divisible by 400) then (it is a common year)
else (it is a leap year)
5 để đếm năm nhuận trong một khoảng thời gian xác địnhChỉ định hai năm như def isleap(year):
"""Return True for leap years, False for non-leap years."""
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
0. Khoảng thời gian là ________ 71, bao gồm ________ 72 nhưng không bao gồm ________ 73import calendar
print(calendar.isleap(2019))
# False
print(calendar.isleap(2020))
# True
print(calendar.isleap(1900))
# False
print(calendar.isleap(2000))
# True
2nguồn. Liên kết được tài trợ Liệt kê các năm nhuận trong một khoảng thời gian xác địnhĐể liệt kê các năm nhuận trong một khoảng thời gian xác định, hãy sử dụng if (year is not divisible by 4) then (it is a common year)
else if (year is not divisible by 100) then (it is a leap year)
else if (year is not divisible by 400) then (it is a common year)
else (it is a leap year)
4 làm điều kiện để hiểu danh sách- Danh sách hiểu trong Python
import calendar
print(calendar.isleap(2019))
# False
print(calendar.isleap(2020))
# True
print(calendar.isleap(1900))
# False
print(calendar.isleap(2000))
# True
3nguồn. Lưu ý rằng trong trường hợp của def isleap(year):
"""Return True for leap years, False for non-leap years."""
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
5, không bao gồm def isleap(year):
"""Return True for leap years, False for non-leap years."""
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
6- Cách sử dụng phạm vi () trong Python
Xác định xem một đối tượng import calendar
print(calendar.isleap(2019))
# False
print(calendar.isleap(2020))
# True
print(calendar.isleap(1900))
# False
print(calendar.isleap(2000))
# True
0 hoặc import calendar
print(calendar.isleap(2019))
# False
print(calendar.isleap(2020))
# True
print(calendar.isleap(1900))
# False
print(calendar.isleap(2000))
# True
1 có phải là năm nhuận hay khôngThư viện chuẩn của Python có mô-đun datetime xử lý ngày và giờ - datetime — Các kiểu ngày và giờ cơ bản — Python 3. 10. 4 tài liệu
Mô-đun datetime cung cấp def isleap(year):
"""Return True for leap years, False for non-leap years."""
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
9 cho ngày và giờ và import calendar
print(calendar.isleap(2019))
# False
print(calendar.isleap(2020))
# True
print(calendar.isleap(1900))
# False
print(calendar.isleap(2000))
# True
20 chỉ cho ngàyCả def isleap(year):
"""Return True for leap years, False for non-leap years."""
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
9 và import calendar
print(calendar.isleap(2019))
# False
print(calendar.isleap(2020))
# True
print(calendar.isleap(1900))
# False
print(calendar.isleap(2000))
# True
20 đều có thuộc tính import calendar
print(calendar.isleap(2019))
# False
print(calendar.isleap(2020))
# True
print(calendar.isleap(1900))
# False
print(calendar.isleap(2000))
# True
23. Nếu bạn muốn kiểm tra xem năm được chỉ định bởi các đối tượng này có phải là năm nhuận hay không, hãy chuyển thuộc tính import calendar
print(calendar.isleap(2019))
# False
print(calendar.isleap(2020))
# True
print(calendar.isleap(1900))
# False
print(calendar.isleap(2000))
# True
23 cho if (year is not divisible by 4) then (it is a common year)
else if (year is not divisible by 100) then (it is a leap year)
else if (year is not divisible by 400) then (it is a common year)
else (it is a leap year)
4 |