A library is a collection of functions that share a common theme. This is a loose definition and will become clear when we start working with a library.
calendar
Consider the following problem:
In the year , August will fall on which day of the week?
Python to the rescue:
xxxxxxxxxx
21import calendar
2calendar.prmonth(3000, 8)
When the above code is executed, the output is:
xxxxxxxxxx
71August 3000
2Mo Tu We Th Fr Sa Su
31 2 3
44 5 6 7 8 9 10
511 12 13 14 15 16 17
618 19 20 21 22 23 24
725 26 27 28 29 30 31
15th of August falls on a Friday. Isn't that lovely? It took just two lines of code! calendar
is one among several libraries in Python's standard library. A comprehensive list can be found here. Going back to the code, calendar
is the name of the library and import
is the keyword used to include this library as a part of the code.
calendar
is a collection of functions that are related to calendars. prmonth
is one such function. It accepts <year>
and <month>
, as input and displays the calendar for <month>
in the year <year>
. If we want to use a function in calendar
, we must first import the library. Let us see what happens if skip this step:
xxxxxxxxxx
21# import calendar
2calendar.prmonth(3000, 8)
It gives the following error:
xxxxxxxxxx
11NameError: name 'calendar' is not defined
To access a function defined inside a library, we use the following syntax:
xxxxxxxxxx
11<calendar>.<function>(<arguments>)
Another way to solve the problem is to use the function weekday
:
xxxxxxxxxx
21import calendar
2print(calendar.weekday(3000, 8, 15))
The output of the above code is 4
. Days are mapped to numbers as follows:
Day | Number |
---|---|
Monday | 0 |
Tuesday | 1 |
Wednesday | 2 |
Thursday | 3 |
Friday | 4 |
Saturday | 5 |
Sunday | 6 |
time
Let us now try to answer this hypothetical question:
You are stranded on an island in the middle of the Indian Ocean. The island has a computing device that has just one application installed in it: a Python interpreter. You wish to know the current date and time.
Solution
xxxxxxxxxx
21from time import ctime
2print('The current time is:', ctime())
The output is:
xxxxxxxxxx
11The current time is: Fri Apr 2 12:24:43 2021
The syntax of the import statement in line-1 looks different. from
is a new keyword. The first line of the code is essentially doing the following: from the library called time
import the function called ctime
. This way of importing functions is useful when we need just one or two functions from a given library:
xxxxxxxxxx
51from time import ctime, sleep
2print('Current time is:', ctime())
3print('I am going to sleep for 10 seconds')
4sleep(10)
5print('Current time is:', ctime())
sleep(x)
is a function in time
that suspends the execution of the program for x
seconds. If we would be using several functions in the library, then it is a bad idea to keep importing each of them individually. In such cases, it is good to fall back on importing the entire library.
this
As a fun exercise, consider the following code:
xxxxxxxxxx
11import this
This gives the following output:
xxxxxxxxxx
211The Zen of Python, by Tim Peters
2
3Beautiful is better than ugly.
4Explicit is better than implicit.
5Simple is better than complex.
6Complex is better than complicated.
7Flat is better than nested.
8Sparse is better than dense.
9Readability counts.
10Special cases aren't special enough to break the rules.
11Although practicality beats purity.
12Errors should never pass silently.
13Unless explicitly silenced.
14In the face of ambiguity, refuse the temptation to guess.
15There should be one-- and preferably only one --obvious way to do it.
16Although that way may not be obvious at first unless you're Dutch.
17Now is better than never.
18Although never is often better than *right* now.
19If the implementation is hard to explain, it's a bad idea.
20If the implementation is easy to explain, it may be a good idea.
21Namespaces are one honking great idea -- let's do more of those!
These are some nuggets of wisdom from Tim Peters, a "major contributor to the Python programming language" [refer]. Some of the points make immediate sense, such as "readability counts".