Read ISO week-year, week number, and weekday details.
How It Works
date.isocalendar() returns a named tuple containing ISO year, ISO week, and ISO weekday. The ISO week-year can differ from the calendar year near New Year, because ISO weeks begin on Monday.
Step-by-Step Example
from datetime import date
week_details = date(2026, 10, 17).isocalendar()
print(week_details.weekday)
print(1 <= week_details.week <= 53)
Understanding the Example
- The expression 'date(2026, 10, 17).isocalendar()' supplies the value stored as 'week_details'. This value is then used or checked by the later statements.
- The print() call exposes the result so you can compare it with the behavior described above. Check the value and its type when experimenting.
A successful run of this example prints the following output:
6
True
Where This Fits in a Real Program
For event timestamps, distinguish the displayed local clock from the instant that it represents. Store or exchange an unambiguous aware value, then convert it when displaying it to a user. For repeated calendar appointments, use a geographic timezone so daylight-saving rules are not lost.
Practice: Try It Yourself
ISO week 1 contains the first Thursday of the ISO year.
For this exercise, predict the result before running the changed code. Then explain the difference between the original and modified cases using the actual values, not just whether the program completed.
Further Reading
Read the official Python documentation for complete parameters, return types and exceptions.
Common Beginner Mistakes
- Assuming the ISO week-year always equals date.year near the end of December.
- Examine ISO week details for December 31 and January 1 of adjacent years.
Key Takeaways
- Read ISO week-year, week number, and weekday details.
- date.isocalendar() returns a named tuple containing ISO year, ISO week, and ISO weekday.
- Apply the demonstrated operation to the practice input and verify its result before depending on it in a larger program.