Python Question Bank
Python Question Bank
1. String:
2. Create a program that counts the number of vowels and consonants in a string.
9. Implement a program to replace all occurrences of a specific word in a string with another
word.
2. List:
10. Write a program to find the common elements between two lists.
3. Tuple:
6. Implement a program to find the index of the first occurrence of an element in a tuple.
4. Set:
7. Write a Python program to find the symmetric difference between two sets.
5. Dictionary:
1. Create a program to find the value associated with a specific key in a dictionary.
9. Implement a function that returns the keys with the maximum value in a dictionary.
6. For Loop:
1. Write a program to print all even numbers from 1 to 50 using a for loop.
2. Create a program to calculate the sum of all elements in a list using a for loop.
3. Implement a program to find the factorial of a number using a for loop.
4. Write a program to print the multiplication table of a given number using a for loop.
5. Create a program to find the largest element in a list using a for loop.
7. While Loop:
2. Write a program to find the sum of all even numbers from 1 to 100 using a while loop.
4. Implement a program that generates random numbers until a specific condition is met using
a while loop.
8. If-Elif-Else:
3. Implement a program to find the largest of three numbers using if-elif-else statements.
9. Functions:
2. Create a function that accepts two numbers and returns their sum.
4. Write a function that calculates the power of a number using a recursive function.
7. Write a function that takes a list of numbers and returns the maximum and minimum values.
8. Create a program that defines a function to check if a given year is a leap year.
10. Write a Python function to generate a Fibonacci sequence up to a specified number of terms.
1. Write a function that accepts and prints an arbitrary number of positional arguments.
2. Create a program that defines a function with default arguments to calculate the area of a
rectangle.
3. Implement a Python function that accepts keyword arguments to display user information
(name, age, etc.).
4. Write a function that accepts a variable-length argument list and calculates their sum.
5. Create a program that uses a function with keyword-only arguments to print a message.
7. Write a Python function that accepts a list and unpacks it into individual arguments.
8. Create a program that defines a function with a variable number of keyword arguments.
9. Implement a function that accepts a dictionary and extracts specific key-value pairs.
10. Write a Python function that demonstrates the use of *args and **kwargs in function
definitions.
12. Decorator:
2. Write a decorator that logs the function name and its arguments before execution.
3. Implement a decorator that limits the execution of a function to a specified number of times.
4. Create a program that defines a decorator to check if a user is logged in before accessing a
function.
5. Write a decorator that caches the results of a function and returns cached values for
repeated input arguments.
7. Create a Python decorator that encrypts the result of a function before returning it.
8. Write a decorator that ensures a function is only executed during specific hours of the day.
9. Implement a decorator that logs exceptions raised during the execution of a function.
10. Create a program that defines a decorator to print the output of a function in a specific
format.
13. Generator:
10. Write a generator that generates a sequence of dates within a specific range.
2. Create a program that uses a lambda function to sort a list of tuples based on the second
element.
4. Write a lambda function to find the maximum value from a list of dictionaries.
5. Create a program that defines a lambda function to calculate the average of a list of
numbers.
6. Implement a lambda function to extract the last word from a list of sentences.
7. Write a lambda function to find the sum of the squares of numbers in a list.
8. Create a lambda function to capitalize the first letter of each word in a sentence.
9. Implement a program that uses a lambda function to find the smallest element in a list.
10. Write a lambda function to filter out words from a list of strings based on their length.
1. Create a program that uses list comprehension to generate a list of squares of numbers from
1 to 10.
4. Create a program that uses list comprehension to extract the vowels from a string.
6. Implement a dictionary comprehension to create a dictionary from two lists of keys and
values.
7. Create a program that uses list comprehension to generate a list of words with more than 5
characters from a sentence.
8. Write a list comprehension to calculate the sum of all prime numbers up to 50.
9. Implement a dictionary comprehension to filter out key-value pairs with values greater than
a specified number.
10. Create a program that uses list comprehension to flatten a list of lists into a single list.
1. Write a Python program to read a text file and display its content.
2. Create a program that reads a CSV file and displays its data in a tabular format.
4. Write a Python program to copy the contents of one text file to another.
8. Create a program that reads a JSON file and displays its data.
9. Implement a program to read a binary file and display its content in hexadecimal format.
10. Write a Python program to create a new directory and move a file into it.
2. Create a program that catches a FileNotFoundError and displays a custom error message.
5. Create a program that raises a custom exception and handles it using try-except.
6. Implement a program that uses a finally block to ensure a resource is properly closed.
10. Write a Python program to handle exceptions for user input errors (e.g., ValueError).
1. Create a Python class to represent a "Car" with attributes like make, model, and year.
Implement a method to display the car's information.
2. Write a program that defines a "Student" class with attributes like name, age, and roll
number. Implement a method to display the student's details.
3. Implement a "Rectangle" class with attributes for length and width. Write a method to
calculate and return the area of the rectangle.
4. Create a Python class for a "BankAccount" with methods to deposit and withdraw money.
Implement a method to display the account balance.
5. Write a program that defines a "Person" class with attributes like name, age, and address.
Implement a method to change the person's address.
6. Implement a "Circle" class with attributes for radius and a method to calculate and return
the area of the circle.
7. Create a Python class for a "Book" with attributes for title, author, and publication year.
Implement a method to display the book's details.
8. Write a program that defines a "Employee" class with attributes like name, salary, and
employee ID. Implement a method to give a salary raise.
9. Implement a "Dog" class with attributes for name and breed. Write a method to display the
dog's name and breed.
10. Create a Python class for a "Bank" that manages multiple bank accounts. Implement
methods to add accounts, deposit money, withdraw money, and display account balances.
19. Inheritance:
1. Define a base class "Vehicle" with attributes like make, model, and year. Create a derived
class "Car" that inherits from "Vehicle" and adds a method to display car details.
2. Implement a base class "Shape" with attributes for color and area. Create derived classes
"Rectangle" and "Circle" that inherit from "Shape" and calculate their respective areas.
3. Define a base class "Person" with attributes for name, age, and address. Create a derived
class "Employee" that inherits from "Person" and adds attributes like employee ID and salary.
4. Create a base class "Animal" with attributes for name and species. Define a derived class
"Dog" that inherits from "Animal" and adds attributes for breed and age.
5. Implement a base class "BankAccount" with methods for deposit and withdraw. Create a
derived class "SavingsAccount" that inherits from "BankAccount" and adds an interest calculation
method.
6. Define a base class "Fruit" with attributes for name and color. Create derived classes "Apple"
and "Banana" that inherit from "Fruit" and add attributes like taste and vitamin content.
7. Create a base class "GeometricShape" with attributes for color and dimensions. Define a
derived class "Rectangle" that inherits from "GeometricShape" and calculates its area.
8. Implement a base class "Animal" with attributes for name and species. Create a derived class
"Bird" that inherits from "Animal" and adds attributes for wingspan and habitat.
9. Define a base class "Employee" with attributes for name, age, and salary. Create a derived
class "Manager" that inherits from "Employee" and adds attributes for team size and project.
10. Implement a base class "Vehicle" with attributes for make, model, and year. Create a derived
class "Truck" that inherits from "Vehicle" and adds attributes like payload capacity and towing
capacity.
20. Polymorphism:
1. Create a base class "Shape" with a method to calculate area. Define derived classes
"Rectangle" and "Circle" that implement the area calculation differently.
2. Implement a base class "Animal" with a method to make a sound. Create derived classes
"Dog" and "Cat" that override the sound method.
3. Define a base class "Employee" with a method to calculate the bonus. Create derived classes
"Manager" and "Worker" that calculate the bonus differently.
4. Create a base class "Vehicle" with a method to display information. Define derived classes
"Car" and "Truck" that override the display method.
5. Implement a base class "Fruit" with a method to describe taste. Create derived classes
"Apple" and "Banana" that provide their own taste descriptions.
6. Define a base class "BankAccount" with methods for deposit and withdraw. Create derived
classes "SavingsAccount" and "CheckingAccount" that implement the methods differently.
7. Create a base class "Shape" with a method to calculate perimeter. Define derived classes
"Rectangle" and "Circle" that calculate perimeter differently.
8. Implement a base class "Animal" with a method to describe habitat. Create derived classes
"Bird" and "Mammal" that provide their own habitat descriptions.
9. Define a base class "Employee" with a method to calculate the annual salary. Create derived
classes "Manager" and "Worker" that calculate the annual salary differently.
10. Create a base class "Vehicle" with a method to start the engine. Define derived classes "Car"
and "Bicycle" that start the engine differently.
21. Encapsulation:
1. Create a class "Student" with private attributes for name, age, and roll number. Implement
methods to set and get these attributes.
2. Define a class "Employee" with private attributes for name, salary, and employee ID.
Implement methods to encapsulate these attributes.
3. Implement a class "BankAccount" with a private attribute for account balance. Provide
methods to deposit and withdraw money while encapsulating the balance.
4. Create a class "Rectangle" with private attributes for length and width. Implement methods
to calculate and return the area while encapsulating these attributes.
5. Define a class "Person" with private attributes for name and age. Implement methods to
change the name and age while encapsulating these attributes.
6. Implement a class "Circle" with a private attribute for radius. Provide methods to calculate
and return the area while encapsulating the radius.
7. Create a class "Book" with private attributes for title, author, and publication year.
Implement methods to encapsulate these attributes.
8. Define a class "Dog" with private attributes for name and breed. Implement methods to
encapsulate these attributes and display the dog's details.
9. Implement a class "Bank" that manages multiple bank accounts with private account
balances. Provide methods to encapsulate account balances and perform transactions.
10. Create a class "Student" with private attributes for name, age, and roll number. Implement a
method to display the student's details while encapsulating the attributes.