Python Question Bank
1. String:
1. Write a program to reverse a string.
2. Create a program that counts the number of vowels and consonants in a string.
3. Implement a function to check if a string is a palindrome.
4. Write a program to find the most common character in a string.
5. Create a program to remove all the spaces from a string.
6. Implement a function to capitalize the first letter of each word in a sentence.
7. Write a Python program to count the occurrences of a specific word in a string.
8. Create a function to check if a string is an anagram of another string.
9. Implement a program to replace all occurrences of a specific word in a string with another
word.
10. Write a function to find the longest word in a sentence.
2. List:
1. Write a program to find the sum of all elements in a list.
2. Create a program to remove duplicate elements from a list.
3. Implement a program to find the second largest element in a list.
4. Write a function that checks if a list is sorted in ascending order.
5. Create a program to find the intersection of two lists.
6. Implement a program that checks if a list is a sublist of another list.
7. Write a Python program to merge two lists.
8. Create a function that reverses a list in place.
9. Implement a program to shuffle the elements of a list randomly.
10. Write a program to find the common elements between two lists.
3. Tuple:
1. Write a program to find the maximum element in a tuple.
2. Create a program to find the sum of elements in a tuple.
3. Implement a function to check if an element exists in a tuple.
4. Write a program to concatenate two tuples.
5. Create a program that counts the occurrences of a specific element in a tuple.
6. Implement a program to find the index of the first occurrence of an element in a tuple.
7. Write a Python program to find the product of all elements in a tuple.
8. Create a function that returns the reverse of a tuple.
9. Implement a program to find the frequency of all elements in a tuple.
10. Write a program to check if a tuple is sorted in ascending order.
4. Set:
1. Write a program to find the intersection of two sets.
2. Create a program to find the union of two sets.
3. Implement a function to check if one set is a subset of another set.
4. Write a program to remove duplicate elements from a list using a set.
5. Create a program that checks if two sets are disjoint.
6. Implement a program to find the difference between two sets.
7. Write a Python program to find the symmetric difference between two sets.
8. Create a function to add an element to a set.
9. Implement a program to remove an element from a set.
10. Write a program to clear all elements from a set.
5. Dictionary:
1. Create a program to find the value associated with a specific key in a dictionary.
2. Write a function to check if a key exists in a dictionary.
3. Implement a program to add a key-value pair to a dictionary.
4. Write a program to remove a key from a dictionary.
5. Create a function that returns the keys of a dictionary in alphabetical order.
6. Implement a program to find the maximum value in a dictionary's values.
7. Write a Python program to find the minimum value in a dictionary's values.
8. Create a program to merge two dictionaries.
9. Implement a function that returns the keys with the maximum value in a dictionary.
10. Write a program to calculate the sum of all values 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:
1. Implement a program that counts down from 10 to 1 using a while loop.
2. Write a program to find the sum of all even numbers from 1 to 100 using a while loop.
3. Create a program to calculate the factorial of a number using a while loop.
4. Implement a program that generates random numbers until a specific condition is met using
a while loop.
5. Write a program to reverse a number using a while loop.
8. If-Elif-Else:
1. Create a program that checks if a number is positive, negative, or zero.
2. Write a program to determine if a year is a leap year or not.
3. Implement a program to find the largest of three numbers using if-elif-else statements.
4. Write a program that checks if a user-provided password is strong or weak.
5. Create a program to categorize a given angle as acute, right, or obtuse.
9. Functions:
1. Write a Python function to calculate the factorial of a number.
2. Create a function that accepts two numbers and returns their sum.
3. Implement a program that uses a function to check if a number is prime.
4. Write a function that calculates the power of a number using a recursive function.
5. Create a Python function that calculates the average of a list of numbers.
6. Implement a program that defines a function to find the square of a number.
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.
9. Implement a function that checks if a string is a palindrome.
10. Write a Python function to generate a Fibonacci sequence up to a specified number of terms.
11. Types of Arguments:
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.
6. Implement a function that accepts a combination of positional and keyword arguments.
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:
1. Create a Python decorator that measures the execution time of a function.
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.
6. Implement a decorator that validates user input for a function.
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:
1. Write a Python generator that generates a sequence of even numbers.
2. Create a generator that produces a Fibonacci sequence up to a specified number of terms.
3. Implement a program that uses a generator to generate prime numbers.
4. Write a generator that yields a sequence of squares of numbers.
5. Create a program that defines a generator to produce a series of random integers.
6. Implement a generator that generates a sequence of strings with random characters.
7. Write a Python generator that yields elements of a list in reverse order.
8. Create a generator that generates the powers of 2 (2^n) up to a specified limit.
9. Implement a generator that yields the elements of a list in random order.
10. Write a generator that generates a sequence of dates within a specific range.
14. Lambda Function:
1. Write a lambda function to calculate the square of a number.
2. Create a program that uses a lambda function to sort a list of tuples based on the second
element.
3. Implement a lambda function to filter out even numbers from a list.
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.
15. List and Dictionary Comprehension:
1. Create a program that uses list comprehension to generate a list of squares of numbers from
1 to 10.
2. Write a list comprehension to filter out even numbers from a list.
3. Implement a dictionary comprehension to create a dictionary of squares of numbers from 1
to 5.
4. Create a program that uses list comprehension to extract the vowels from a string.
5. Write a list comprehension to generate a list of even numbers from 1 to 20.
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.
16. File Handling:
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.
3. Implement a program to write data to a text file.
4. Write a Python program to copy the contents of one text file to another.
5. Create a program that counts the number of words in a text file.
6. Implement a program to find and replace a specific word in a text file.
7. Write a Python program to append data to an existing text file.
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.
17. Exception Handling:
1. Write a Python program that handles a ZeroDivisionError exception.
2. Create a program that catches a FileNotFoundError and displays a custom error message.
3. Implement a program that handles a KeyError exception for dictionary access.
4. Write a Python program to catch multiple exceptions in a try-except block.
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.
7. Write a Python program that handles an IndexError exception.
8. Create a program that catches and re-raises an exception.
9. Implement a program that handles a NameError exception.
10. Write a Python program to handle exceptions for user input errors (e.g., ValueError).
18. OOP (Object-Oriented Programming):
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.