Python modules allow you to organize your code into separate files and reuse them across different projects. Let’s explore some essential commands for using modules in Python.
To use a module, you first need to import it.
import math
import math
You can import specific functions from a module to save memory and improve performance.
from math import sqrt
from math import sqrt
Some modules aren’t built into Python and need to be installed separately. The method varies depending on the environment:
You can Install Python Idle Module from the command line. Here’s how:
- Open Command Prompt:
- Press Windows + R, type cmd, and hit Enter.
Python uses packages to extend its functionality. You can manage these packages using pip, which is included with Python. Here's how to install and uninstall libraries:
- Install a Library:
- In the command prompt, type the following command to install a library (for example, requests):
pip install requests
- Uninstall a Library:
- To uninstall a library, use the following command:
pip uninstall requests
!pip install requests
List all the installed modules to confirm installation or check for available modules.
pip list
!pip list
Use the dir()
function to view all functions and attributes available in a module.
dir(math)
dir(math)
Remember to use import
to bring any module or function into your workspace, and always check if the
module is available natively or needs to be installed.
The following code snippets show how to capture video from your webcam.
This page demonstrates how to capture video from a webcam using OpenCV's VideoCapture
class.
import cv2
# Create a VideoCapture object
cap = cv2.VideoCapture(0)
while True:
# Capture frame-by-frame
ret, frame = cap.read()
if not ret:
break
# Display the resulting frame
cv2.imshow('Webcam Feed', frame)
# Press 'q' to exit the loop
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the capture and close windows
cap.release()
cv2.destroyAllWindows()
import cv2
from google.colab.patches import cv2_imshow
# Create a VideoCapture object
cap = cv2.VideoCapture(0)
while True:
# Capture frame-by-frame
ret, frame = cap.read()
if not ret:
break
# Display the resulting frame
cv2_imshow(frame)
# Press 'q' to exit the loop
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the capture
cap.release()
Note: When using Google Colab, the webcam will need permission to be accessed.
To stop the video feed in both environments, press the 'q' key.
Pandas is a powerful library for data manipulation and analysis.
Create a simple DataFrame and display it.
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35]}
df = pd.DataFrame(data)
print(df)
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35]}
df = pd.DataFrame(data)
df
Matplotlib is a plotting library for creating static, animated, and interactive visualizations in Python.
Plot a simple line graph using Matplotlib.
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.plot(x, y)
plt.title('Sample Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.plot(x, y)
plt.title('Sample Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
Feel free to experiment with these libraries and explore their extensive functionalities!