Find the weekday index for a calendar date.
How It Works
calendar.weekday(year, month, day) returns Monday=0 through Sunday=6 for valid Gregorian dates. To get Monday=1 through Sunday=7 use date.isoweekday().
Step-by-Step Example
import calendar
print(calendar.weekday(2026, 10, 17))
print(calendar.weekday(2026, 10, 18))
Understanding the Example
- 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:
5
6
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
Use date.isoweekday() when ISO weekday numbering is required.
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
- Comparing weekday() results to ISO weekday numbering without converting.
- Label weekdays for a week of calendar dates.
Key Takeaways
- Find the weekday index for a calendar date.
- calendar.weekday(year, month, day) returns Monday=0 through Sunday=6 for valid Gregorian dates.
- Apply the demonstrated operation to the practice input and verify its result before depending on it in a larger program.