Skip to main content

Posts

Showing posts from September, 2022

How To Print Calendar Using Python

In this article, I’ll show you those two lines of Python code, using which you can print calendar of any year. Required Package The pre-requisite to generate calendar is to import the required package named calendar as shown below: from calendar import * Generate Calendar Now to generate a calendar, we need to call constructor and pass in four parameters as shown below: print(calendar(2018,2,1,4)) Here, 2018 is the year for which calendar will be printed 2 signifies width of each character 1 signifies number of lines per week 4 signifies column separation value You can adjust the last three parameters for better spacing in between the text. Output This is how the calendar looks like: I hope you enjoyed generating a calendar of your choice. Make sure to check out my recording explaining each and every parameter discussed in this article:

Sort A Python Dictionary By Value

It is quite common to sort a Python dictionary based on key. But what if you want to perform sorting on the value? In this article, I’m going to show you multiple ways to sort dictionary value and then return the resultant dictionary. Method 1: Using operator The very first method to sort dictionary value is by using sorted(…) function along with operator. So, let’s go ahead and do it as shown below: import operator students = {‘Shweta’:25,’Andy’:30,’Maddy’:3} students = sorted(students.items(), key=operator.itemgetter(1)) print (students) Method 2: Using lambda The second method to sort dictionary value is by using sorted(…) function along with lambda and the code looks as shown below: : import operator students = {‘Shweta’:25,’Andy’:30,’Maddy’:3} students = sorted(students.items(), key=lambda stud:stud[1]) print (students) Output On execution of above two methods, you will get exactly same output. I hope you enjoyed sorting your dictionary values in Python. If you have reached t