Friday , September 20 2024

What is the expected result of the following code? import calendar c = calendar.Calendar() for weekday in c.iterweekdays(): print(weekday, end=” “)

Questions BankCategory: Python Essentials 2What is the expected result of the following code? import calendar c = calendar.Calendar() for weekday in c.iterweekdays(): print(weekday, end=” “)
What is the expected result of the following code?

import calendar
 
c = calendar.Calendar()
 
for weekday in c.iterweekdays():
    print(weekday, end=" ")
  • Mo Tu We Th Fr Sa Su
  • 1 2 3 4 5 6 7
  • Su Mo Tu We Th Fr Sa
  • 0 1 2 3 4 5 6

Explanation: Let’s analyze the code:

  • Line 1: the calendar module is imported.
  • Line 3: a c object is created from the Calendar() class provided by the calendar module. The Calendar() class constructor takes no optional firstweekday parameter, which means its value is equal to 0 by default (0 is equal to Monday).
  • Lines 5 and 6: the for loop and the Calendar() class method named iterweekdays are used to iterate through the days of the week, and return an iterator for each week day number, starting from 0 (Monday), to 6 (Sunday). The values are printed to the console.

More Questions: Python Essentials 2 – Module 4 Test