Python Day 6: Modules

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.

1. Importing Modules

To use a module, you first need to import it.

Command for IDLE:

import math

Command for Google Colab:

import math

2. Using Specific Functions from a Module

You can import specific functions from a module to save memory and improve performance.

Command for IDLE:

from math import sqrt

Command for Google Colab:

from math import sqrt

3. Installing External Modules

Some modules aren’t built into Python and need to be installed separately. The method varies depending on the environment:

Command for IDLE:

You can Install Python Idle Module from the command line. Here’s how:

- Open Command Prompt:
  - Press Windows + R, type cmd, and hit Enter.

4. Install/Uninstall Libraries

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

Command for Google Colab:

!pip install requests

4. Checking Installed Modules

List all the installed modules to confirm installation or check for available modules.

Command for IDLE:

pip list

Command for Google Colab:

!pip list

5. Exploring Module Functions

Use the dir() function to view all functions and attributes available in a module.

Command for IDLE:

dir(math)

Command for Google Colab:

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.



Play with Libraries: cv2, Pandas, Matplotlib

1. OpenCV (cv2)

1. Video Capture using OpenCV

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.

Command for IDLE:

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()

Command for Google Colab:

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.

2. Pandas

Pandas is a powerful library for data manipulation and analysis.

Creating a DataFrame and Displaying It

Create a simple DataFrame and display it.

Command for IDLE:

import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35]}
df = pd.DataFrame(data)
print(df)

Command for Google Colab:

import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35]}
df = pd.DataFrame(data)
df

3. Matplotlib

Matplotlib is a plotting library for creating static, animated, and interactive visualizations in Python.

Basic Plotting

Plot a simple line graph using Matplotlib.

Command for IDLE:

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()

Command for Google Colab:

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!