105800 Views
82496 Views
47243 Views
47066 Views
45458 Views
38527 Views
Pi-Apps
Intermediate MicroPython
Arduino Alvik
Raspberry Pi Time machine
Now Ad-Free
Guiding Light
Intermediate level MicroPython
Introduction to FreeCAD for Beginners
Building a Robot Arm with Raspberry Pi and PCA9685
Building User Authentication for Static Sites with FastAPI
Mastering Pydantic for Robust Data Validation
Mastering Markdown for Documentation with Jekyll
KevsRobots Learning Platform
35% Percent Complete
By Kevin McAleer, 3 Minutes
Python is known for its rich set of libraries and modules which simplifies the coding experience. In this lesson, we will understand what Python libraries and modules are, how to import them, and get a brief look at a few popular libraries.
libraries
modules
math
random
datetime
In Python, a module is a file containing Python code. A library is a collection of modules. They define functions, classes, and variables that you can reuse in your programs.
module
file
library
functions
classes
variables
You can use the import keyword to import a module or a library. Once imported, you can use the dot notation to access the functions, classes, and variables defined in that module.
import
# Importing the math module import math # Using a function from the math module print(math.sqrt(16)) # Prints '4.0'
The math module provides mathematical functions and constants.
import math # Constants print(math.pi) # Prints '3.141592653589793' # Trigonometric functions print(math.sin(math.pi/2)) # Prints '1.0' # Logarithmic functions print(math.log(100, 10)) # Prints '2.0'
The random module provides functions for generating random numbers.
import random # Generate a random float between 0 and 1 print(random.random()) # Generate a random integer between 1 and 10 print(random.randint(1, 10)) # Randomly choose an item from a list print(random.choice(['apple', 'banana', 'cherry']))
The datetime module provides classes for manipulating dates and times.
import datetime # Get the current date and time now = datetime.datetime.now() print(now) # Create a specific date independence_day = datetime.datetime(1776, 7, 4) print(independence_day) # Calculate the difference between two dates delta = now - independence_day print(delta.days)
In this lesson, you’ve learned about Python libraries and modules. You’ve learned how to import them and explored a few popular libraries: math, random, and datetime. Libraries and modules are powerful tools that allow you to leverage existing code and build complex applications more easily.
< Previous Next >