A Library is a collection of functions and methods that can be used to reduce repetitive tasks. These functions are stored in different files to keep your code more organized and readable.
A library that comes pre-installed with Python is the time library. Here's an example of how to use a library:
import timestart_time = time.time() #gets current timetime.sleep(1) #pauses for 1 secondend_time = time.time()total_time = end_time - start_timeprint("Seconds taken:")print(total_time)When using a library, you use the library name and the function name with a period in between. To skip this step, you can import only specific functions like this:
from time import sleepsleep(1)from time import *#the * means all functions will be importedtime()You can change the prefix of your library call by importing the library as variable name: import RoboPiLib as RPL
If you need to import a library from another directory, you can use the relative path of your current file.
import directory/file