Unit 6 subjective questions Real-world applications
ECE181 — Introduction To Python 20 questions with model answers. Write yours first, then compare.
-
Question 1 of 20
What are real-world applications of Python? Explain with at least five domains where Python is widely used.
Model answer
Python is a general-purpose, high-level programming language whose simplicity and rich ecosystem of libraries make it suitable for a vast range of real-world applications:
- Web Development: Frameworks like Django and Flask are used to build robust web applications and REST APIs.
- Data Science & Analytics: Libraries such as Pandas, NumPy, and Matplotlib enable data cleaning, analysis, and visualization.
- Machine Learning & AI: Scikit-learn, TensorFlow, and PyTorch power predictive models and deep learning systems.
- Automation & Scripting: Python automates repetitive tasks like file handling, report generation, and testing.
- Computer Vision: OpenCV is used for image and video processing in applications like face detection.
- Game Development: PyGame helps build 2D games and interactive multimedia.
- Web Scraping: BeautifulSoup and Scrapy extract data from websites.
Python's cross-platform support, large community, and extensive standard library make it a preferred choice across industries such as finance, healthcare, and scientific research.
-
Question 2 of 20
What is OpenCV? Explain how it is used to perform basic image operations in Python.
Model answer
OpenCV (Open Source Computer Vision Library) is an open-source library aimed at real-time computer vision and image processing. In Python, it is accessed through the
cv2module.Common basic image operations include:
- Reading an image:
img = cv2.imread('image.jpg') - Displaying an image:
cv2.imshow('window', img) - Saving an image:
cv2.imwrite('output.jpg', img) - Converting color spaces:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) - Resizing:
resized = cv2.resize(img, (width, height))
Example:
python
import cv2
img = cv2.imread('photo.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('Gray Image', gray)
cv2.waitKey(0)
cv2.destroyAllWindows()OpenCV represents images as NumPy arrays, allowing pixel-level manipulation. It is widely used for face detection, object tracking, edge detection, and other vision tasks.
- Reading an image:
-
Question 3 of 20
Explain the steps to perform edge detection and image thresholding using OpenCV with example code.
Model answer
Edge detection identifies boundaries within an image, while thresholding converts a grayscale image into a binary image.
1. Edge Detection (Canny Algorithm):
The Canny edge detector finds edges based on intensity gradients.
python
import cv2
img = cv2.imread('image.jpg', 0) # grayscale
edges = cv2.Canny(img, 100, 200) # min and max thresholds
cv2.imshow('Edges', edges)
cv2.waitKey(0)2. Image Thresholding:
Pixels above a threshold value are set to a maximum value; others are set to zero.
python
ret, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
cv2.imshow('Thresholded', thresh)
cv2.waitKey(0)Key parameters:
- Canny: lower threshold and upper threshold control edge sensitivity.
- Threshold types:
THRESH_BINARY,THRESH_BINARY_INV,THRESH_TRUNC, etc.
These operations are foundational in feature extraction, object detection, and image segmentation.
-
Question 4 of 20
What is PyGame? Describe the basic structure of a PyGame program.
Model answer
PyGame is a cross-platform set of Python modules designed for writing 2D video games and multimedia applications. It provides functionality for graphics, sound, and user input handling.
Basic structure of a PyGame program:
- Initialization: Import and initialize PyGame with
pygame.init(). - Create the display window:
screen = pygame.display.set_mode((width, height)). - Game loop: A continuous loop that:
- Handles events (keyboard, mouse, quit).
- Updates game state.
- Draws/renders objects on screen.
- Updates the display with
pygame.display.flip().
- Quit:
pygame.quit()when the loop ends.
Example:
python
import pygame
pygame.init()
screen = pygame.display.set_mode((400, 300))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0, 0, 0))
pygame.display.flip()
pygame.quit()The game loop is the heart of any PyGame application, ensuring continuous rendering and interaction.
- Initialization: Import and initialize PyGame with
-
Question 5 of 20
Explain how event handling works in PyGame with suitable examples.
Model answer
Event handling in PyGame captures and responds to user interactions such as key presses, mouse movements, and window closing.
How it works:
- PyGame maintains an event queue that stores all events.
pygame.event.get()retrieves the list of pending events.- Each event has a type that identifies its category.
Common event types:
pygame.QUIT— when the window's close button is clicked.pygame.KEYDOWN/pygame.KEYUP— key press/release.pygame.MOUSEBUTTONDOWN— mouse click.pygame.MOUSEMOTION— mouse movement.
Example:
python
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
print('Move Left')
elif event.key == pygame.K_RIGHT:
print('Move Right')
elif event.type == pygame.MOUSEBUTTONDOWN:
print('Mouse Clicked at', event.pos)Proper event handling makes games interactive and responsive to the player's input.
-
-
Question 6 of 20
What is web scraping? Explain the process of accessing a URL and extracting data using Python libraries.
Model answer
Web scraping is the automated process of extracting data from websites. It involves fetching web pages and parsing their HTML content to retrieve useful information.
Process:
-
Send a request to the URL using the
requestslibrary:
python
import requests
response = requests.get('https://example.com')
html = response.text -
Parse the HTML using
BeautifulSoup:
python
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'html.parser') -
Extract data using tags, classes, or IDs:
python
titles = soup.find_all('h2')
for t in titles:
print(t.text)
Common libraries:
- requests — for sending HTTP requests.
- BeautifulSoup — for parsing HTML/XML.
- Scrapy — a full scraping framework.
- Selenium — for scraping dynamic, JavaScript-rendered pages.
Note: Web scraping should respect a website's
robots.txtand terms of service to remain ethical and legal. -
-
Question 7 of 20
Distinguish between the requests module and the urllib module for accessing URLs in Python.
Model answer
Both modules are used for making HTTP requests in Python, but they differ in usability and features.
Feature requests urllib Type Third-party library Built-in standard library Ease of use Simple, human-friendly API More verbose and complex Syntax requests.get(url)urllib.request.urlopen(url)JSON handling Built-in .json()methodRequires manual parsing Session support Yes, with SessionobjectsLimited Installation Needs pip install requestsNo installation needed Example with requests:
python
import requests
r = requests.get('https://api.example.com/data')
print(r.json())Example with urllib:
python
import urllib.request
response = urllib.request.urlopen('https://example.com')
html = response.read()Conclusion:
requestsis preferred for most modern applications due to its simplicity, whileurllibis useful when avoiding external dependencies. -
Question 8 of 20
What is data visualization? Explain its importance in data analysis.
Model answer
Data visualization is the graphical representation of data and information using visual elements like charts, graphs, and maps. It helps transform complex datasets into intuitive visuals.
Importance in data analysis:
- Simplifies complex data: Large datasets become easier to understand at a glance.
- Reveals patterns and trends: Highlights correlations, outliers, and distributions.
- Supports decision-making: Enables faster, data-driven decisions.
- Improves communication: Conveys insights effectively to non-technical audiences.
- Identifies relationships: Shows how variables interact.
Common Python libraries:
- Matplotlib — foundational plotting library.
- Seaborn — statistical visualizations built on Matplotlib.
- Plotly — interactive charts.
- Pandas — quick plotting via DataFrames.
Common chart types:
- Line charts (trends over time)
- Bar charts (comparisons)
- Pie charts (proportions)
- Scatter plots (relationships)
- Histograms (distributions)
Effective visualization turns raw numbers into actionable insights.
-
Question 9 of 20
Explain how to create different types of plots using Matplotlib with example code.
Model answer
Matplotlib is the most widely used Python library for creating static, animated, and interactive visualizations. The
pyplotmodule provides a MATLAB-like interface.1. Line Plot:
python
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.plot(x, y)
plt.title('Line Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()2. Bar Chart:
python
plt.bar(['A', 'B', 'C'], [5, 7, 3])
plt.show()3. Histogram:
python
data = [1, 2, 2, 3, 3, 3, 4]
plt.hist(data, bins=4)
plt.show()4. Scatter Plot:
python
plt.scatter([1, 2, 3], [4, 5, 6])
plt.show()5. Pie Chart:
python
plt.pie([30, 40, 30], labels=['X', 'Y', 'Z'])
plt.show()Key customization functions:
title(),xlabel(),ylabel(),legend(), andgrid()enhance readability of plots. -
Question 10 of 20
Compare Matplotlib and Seaborn as data visualization libraries in Python.
Model answer
Both Matplotlib and Seaborn are popular visualization libraries, but they serve slightly different purposes.
Aspect Matplotlib Seaborn Level Low-level, highly customizable High-level, built on Matplotlib Syntax More verbose Concise and simpler Default styling Basic Attractive, modern themes Statistical plots Manual effort Built-in (heatmaps, violin plots) Data integration Works with arrays/lists Integrates directly with Pandas DataFrames Use case Full control over plot elements Quick statistical visualization Seaborn Example:
python
import seaborn as sns
import pandas as pd
df = sns.load_dataset('tips')
sns.boxplot(x='day', y='total_bill', data=df)Matplotlib Example:
python
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.show()Conclusion: Use Seaborn for quick, aesthetically pleasing statistical plots and Matplotlib when fine-grained customization is required.
-
Question 11 of 20
What is data storytelling? Explain its key components and importance.
Model answer
Data storytelling is the practice of communicating insights from data using a combination of narrative, visualizations, and context to influence and engage an audience.
Key components:
- Data: The foundation — accurate and relevant information.
- Narrative: A story or logical flow that connects the data to a message.
- Visuals: Charts and graphs that make data understandable.
- Context: Background information that gives meaning to the data.
Importance:
- Engages the audience: Stories are more memorable than raw numbers.
- Drives action: Persuades stakeholders to make decisions.
- Simplifies complexity: Makes technical data accessible to non-experts.
- Provides context: Explains why the data matters, not just what it shows.
Example: Instead of just showing a sales chart, a data story explains why sales dropped in Q2, what caused it, and how to recover — supported by visuals.
Effective data storytelling bridges the gap between analysis and decision-making.
-
Question 12 of 20
Describe some advanced toolboxes/libraries in Python and their applications.
Model answer
Python offers numerous advanced toolboxes that extend its capabilities across specialized domains.
- NumPy: Numerical computing with powerful N-dimensional arrays and mathematical functions.
- Pandas: Data manipulation and analysis using DataFrames and Series.
- SciPy: Scientific computing with modules for optimization, integration, and statistics.
- Scikit-learn: Machine learning algorithms for classification, regression, and clustering.
- TensorFlow / PyTorch: Deep learning and neural network frameworks.
- OpenCV: Computer vision and image processing.
- NLTK / spaCy: Natural language processing tasks.
- Matplotlib / Seaborn / Plotly: Data visualization.
- Requests / BeautifulSoup / Scrapy: Web scraping and HTTP handling.
- Flask / Django: Web application development.
Applications:
These toolboxes power data science, AI, automation, web development, and scientific research, making Python a versatile language for solving real-world problems. -
Question 13 of 20
Explain how images are represented in OpenCV and describe pixel manipulation with an example.
Model answer
In OpenCV, images are represented as NumPy arrays (multi-dimensional matrices), where each element corresponds to a pixel value.
Image representation:
- Grayscale image: A 2D array of shape
(height, width)where each value ranges from 0 (black) to 255 (white). - Color image: A 3D array of shape
(height, width, 3)representing BGR channels (Blue, Green, Red) — note OpenCV uses BGR, not RGB.
Accessing and modifying pixels:
python
import cv2
img = cv2.imread('image.jpg')Access pixel at (50, 100)
pixel = img[50, 100]
print(pixel) # [B, G, R]Modify a pixel to white
img[50, 100] = [255, 255, 255]
Access a specific channel (Blue)
blue = img[50, 100, 0]
Region of Interest (ROI):
python
roi = img[100:200, 150:250] # crop a regionSince images are NumPy arrays, all array operations (slicing, arithmetic, broadcasting) can be applied for efficient image processing.
- Grayscale image: A 2D array of shape
-
Question 14 of 20
Write a Python program using PyGame to move a rectangle across the screen using arrow keys, and explain the logic.
Model answer
The program uses PyGame's event handling and the game loop to move a rectangle based on keyboard input.
Pythonimport pygame pygame.init() screen = pygame.display.set_mode((500, 400)) pygame.display.set_caption('Move Rectangle') x, y = 200, 150 # initial position speed = 5 running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: x -= speed if keys[pygame.K_RIGHT]: x += speed if keys[pygame.K_UP]: y -= speed if keys[pygame.K_DOWN]: y += speed screen.fill((0, 0, 0)) pygame.draw.rect(screen, (255, 0, 0), (x, y, 50, 50)) pygame.display.flip() pygame.time.delay(30) pygame.quit()Logic explanation:
pygame.key.get_pressed()returns the state of all keys, allowing smooth continuous movement.- The x, y coordinates are updated based on which arrow key is pressed.
screen.fill()clears the screen each frame to avoid trailing.pygame.draw.rect()redraws the rectangle at the new position.pygame.time.delay(30)controls the frame rate.
-
Question 15 of 20
Explain the concept of HTTP requests and describe the common HTTP methods used in web scraping and API access.
Model answer
HTTP (HyperText Transfer Protocol) requests are messages sent by a client (like a browser or Python script) to a server to request or send data.
Common HTTP methods:
-
GET: Retrieves data from a server. Used most in web scraping.
python
requests.get(url) -
POST: Sends data to the server to create a resource.
python
requests.post(url, data={'key': 'value'}) -
PUT: Updates an existing resource.
-
DELETE: Removes a resource.
-
HEAD: Retrieves only response headers, not the body.
HTTP status codes:
- 200 — OK (success)
- 301/302 — Redirection
- 404 — Not Found
- 403 — Forbidden
- 500 — Server Error
Example:
python
import requests
r = requests.get('https://example.com')
print(r.status_code) # 200
print(r.headers) # response headersUnderstanding these methods and status codes is essential for effective and error-free web scraping and API interactions.
-
-
Question 16 of 20
Describe the process of building a complete data visualization dashboard and the best practices for creating effective visualizations.
Model answer
A data visualization dashboard presents multiple related visualizations in a single interface for monitoring and analysis.
Process of building a dashboard:
- Define objectives: Identify the key metrics and questions to answer.
- Collect and clean data: Use Pandas to load and preprocess data.
- Choose the right charts: Match chart types to data (trends, comparisons, proportions).
- Build visualizations: Use Matplotlib, Seaborn, Plotly, or dashboard tools like Dash/Streamlit.
- Arrange layout: Organize charts logically for easy interpretation.
- Add interactivity: Filters, dropdowns, and hover details enhance usability.
Best practices:
- Choose appropriate chart types for the data.
- Avoid clutter — keep visuals clean and focused.
- Use consistent colors and clear labels.
- Highlight key insights with annotations.
- Ensure accessibility with readable fonts and colorblind-friendly palettes.
- Provide context through titles and legends.
A well-designed dashboard turns data into clear, actionable insights for decision-makers.
-
Question 17 of 20
What is BeautifulSoup? Explain its main functions used in web scraping with examples.
Model answer
BeautifulSoup is a Python library used for parsing HTML and XML documents. It creates a parse tree that makes it easy to navigate, search, and extract data from web pages.
Creating a BeautifulSoup object:
python
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_content, 'html.parser')Main functions/methods:
-
find(): Returns the first matching element.
python
soup.find('h1') -
find_all(): Returns a list of all matching elements.
python
soup.find_all('a') -
get_text(): Extracts text content.
python
soup.get_text() -
get(): Retrieves an attribute value.
python
link.get('href') -
select(): Uses CSS selectors.
python
soup.select('div.classname')
Example:
python
links = soup.find_all('a')
for link in links:
print(link.get('href'))BeautifulSoup makes extracting structured data from messy HTML simple and reliable.
-
-
Question 18 of 20
Explain NumPy and Pandas as advanced Python toolboxes, and distinguish between their primary data structures.
Model answer
NumPy and Pandas are two foundational libraries for data science and numerical computing in Python.
NumPy (Numerical Python):
- Provides the ndarray — a fast, memory-efficient N-dimensional array.
- Supports vectorized operations, broadcasting, and linear algebra.
- Ideal for numerical and mathematical computations.
Pandas:
- Built on top of NumPy.
- Provides Series (1D) and DataFrame (2D labeled) structures.
- Excellent for data manipulation, cleaning, and analysis.
Distinguishing data structures:
Feature NumPy ndarrayPandas DataFrameDimensions N-dimensional Primarily 2D Labels No labels (index by position) Labeled rows and columns Data type Homogeneous Heterogeneous columns Use case Numerical computation Tabular data analysis Example:
python
import numpy as np
import pandas as pd
arr = np.array([1, 2, 3])
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})Together, they form the backbone of the Python data ecosystem.
-
Question 19 of 20
Explain the challenges and ethical considerations involved in web scraping.
Model answer
While web scraping is powerful, it comes with several technical challenges and ethical/legal considerations.
Technical challenges:
- Dynamic content: JavaScript-rendered pages require tools like Selenium.
- Anti-scraping measures: CAPTCHAs, IP blocking, and rate limiting.
- Changing website structure: HTML changes can break scrapers.
- Pagination and login walls: Complex navigation is required.
- Data cleaning: Extracted data is often messy and needs processing.
Ethical and legal considerations:
- Respect
robots.txt: This file specifies which pages can be crawled. - Follow Terms of Service: Some sites prohibit scraping.
- Avoid overloading servers: Add delays between requests to prevent denial of service.
- Respect copyright and privacy: Do not misuse personal or copyrighted data.
- Attribution: Give credit when using scraped data.
Best practices:
- Use rate limiting and a proper
User-Agentheader. - Prefer official APIs when available.
Responsible scraping ensures legal compliance and maintains a healthy relationship with data sources.
-
Question 20 of 20
Describe how Python integrates image processing, data analysis, and visualization together in a real-world application. Provide a conceptual example.
Model answer
Python's strength lies in combining multiple libraries to build end-to-end real-world applications. Consider a medical imaging analysis system.
Workflow integration:
-
Image Acquisition & Processing (OpenCV):
- Read medical scan images.
- Apply preprocessing like grayscale conversion, noise removal, and edge detection.
python
import cv2
img = cv2.imread('scan.jpg', 0)
edges = cv2.Canny(img, 100, 200)
-
Data Extraction & Analysis (NumPy/Pandas):
- Extract pixel intensity data and store measurements.
python
import numpy as np
import pandas as pd
mean_intensity = np.mean(img)
df = pd.DataFrame({'mean_intensity': [mean_intensity]})
- Extract pixel intensity data and store measurements.
-
Visualization (Matplotlib/Seaborn):
- Plot intensity distributions and trends across scans.
python
import matplotlib.pyplot as plt
plt.hist(img.ravel(), bins=256)
plt.title('Intensity Distribution')
plt.show()
- Plot intensity distributions and trends across scans.
-
Storytelling & Reporting:
- Present findings with narrative and visuals to support diagnosis.
Conclusion:
This integration demonstrates Python's role as a glue language — OpenCV handles vision, NumPy/Pandas handle computation, and Matplotlib communicates results, creating a complete, practical solution. -
That's unit 6
Long answers stick best when they're tested again soon. Run the MCQs on this unit, then come back to the questions you found hardest.
Nearly through. Did unit 6 of ECE181 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 6 of ECE181 save you a night before the exam?
Then you can help keep it free for the next batch. Ads cover part of the server bill; the rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks.