Python Practical Questions
—----------------------------------------------------------------------------------------------------------------------------
1. Question
Problem statement
A student wants to learn the fundamentals of Python by printing a personalized greeting and
exploring how variables are stored in memory.
Your Task:
Write a Python program that takes your name as input and prints a greeting using the print()
function. Add a function to display the memory address of the variable storing your name using
the id() function.
2. Question
Problem statement
A developer wants to create a voting eligibility checker while learning about Python's dynamic
typing.
Your Task:
Create a program to accept a user's age and check if they are eligible to vote. Include a function
that dynamically determines the data type of user input using type().
3. Question
Problem statement
A company wants to organize employee data in a matrix and access records efficiently.
Your Task:
Implement a program to create a 2D array (matrix) that stores employee data (e.g., ID, Name,
and Salary). Add functionality to retrieve employee details by their ID.
4. Question
Problem statement
A photographer wants to experiment with creating images programmatically.
Your Task:
Write a program to create an image with random colors using OpenCV and save it locally using
imwrite().
5. Question
Problem statement
A developer working on input sanitization wants to detect and handle escape sequences.
Your Task:
Write a program that reads user input and checks if it contains a specific escape sequence (\n
or \t). Demonstrate the importance of indentation in the program.
6. Question
Problem statement
An analyst needs a quick way to categorize numbers based on their sign.
Your Task:
Create a ternary operator-based function that categorizes a number as "Positive," "Negative," or
"Zero."
7. Question
Problem statement
A teacher wants to demonstrate the power of Python comprehensions to students.
Your Task:
Generate a list of squares for numbers 1–10 using a list comprehension. Demonstrate how to
extract even squares from the list.
8. Question
Problem statement
A student wants to learn efficient generation of large datasets using generators.
Your Task:
Write a generator function to yield the first N Fibonacci numbers. Use it to print Fibonacci
numbers up to 100.
9. Question
Problem statement
A data scientist wants to analyze sorting performance for large datasets.
Your Task:
Measure the time taken to sort a list of 1,000,000 random integers using the timeit module.
10. Question
Problem statement
A programmer wants to understand how Python handles copying in memory.
Your Task:
Illustrate the difference between shallow and deep copy using lists.
11. Question
Problem statement
A developer validates user input in a registration system.
Your Task:
Write a program that creates a custom exception InvalidEmailError. Validate an email
address using regular expressions, and raise the custom exception if the email is invalid.
12. Question
Problem statement
A data engineer processes streaming data for analytics.
Your Task:
Create a coroutine to process a stream of numbers in real time. It should calculate the running
average of the numbers provided.
13. Question
Problem statement
A data scientist optimizes calculations for large datasets.
Your Task:
Write a Python program to use the multiprocessing module to calculate the sum of squares
of numbers in a large dataset by splitting the dataset into chunks.
14. Question
Problem statement
A developer creates a plugin system for a Python application.
Your Task:
Write a program to dynamically load and execute a Python module based on user input.
Validate the module's existence before importing.
15. Question
Problem statement
A system administrator tracks file access and ensures proper closure.
Your Task:
Create a custom context manager to handle file operations safely. Ensure that it logs each file
access in a separate log file.