Basic Python Concepts. (Click here for solved problems and explanations)
- Write a Python program to calculate the factorial of a number using recursion.
- How can you check if a given string is a palindrome? Write a program to demonstrate.
- Write a program to find the largest and smallest elements in a list without using built-in functions.
- Demonstrate how to swap two numbers in Python without using a temporary variable.
- Write a Python program to generate the Fibonacci sequence up to
n
terms.
Data Structures in Python
- Write a Python program to sort a list of dictionaries by a specific key.
- Demonstrate the use of a stack using a Python list with push, pop, and peek operations.
- Write a program to find duplicates in a list and print their frequency.
- Implement a queue using Python’s
collections.deque
module. - Write a Python program to flatten a nested list.
String Manipulations
- Write a program to count the number of vowels and consonants in a given string.
- Demonstrate how to reverse a string while preserving the positions of special characters.
- Write a program to find all permutations of a given string.
- How can you check if two strings are anagrams of each other? Write a program to show this.
- Write a Python program to capitalize the first letter of each word in a string.
File Handling
- Write a program to count the number of lines, words, and characters in a text file.
- Demonstrate how to merge the contents of two files into a single file.
- Write a Python script to read a file and remove all the blank lines.
- Create a program to find and replace a word in a text file.
- Write a Python program to read a CSV file and calculate the average of numerical columns.
Error and Exception Handling
- Write a program to handle division by zero using try-except blocks.
- Demonstrate how to raise and handle custom exceptions in Python.
- Write a Python program to read a file and handle the
FileNotFoundError
gracefully. - Create a program that validates user input and raises exceptions for invalid entries.
- Write a Python script that logs errors to a file instead of printing them to the console.
Intermediate Topics
- Write a Python program to implement a binary search algorithm.
- Demonstrate how to implement the bubble sort and quick sort algorithms in Python.
- Write a program to calculate the transpose of a matrix.
- Demonstrate how to create and use lambda functions in Python.
- Write a Python program to merge two dictionaries.
Real-World Applications
- Create a Python program that simulates a simple calculator with basic operations.
- Write a program to scrape the titles and links of blog posts from a webpage.
- Demonstrate how to send an email with attachments using Python.
- Write a program to create a basic to-do list application using Python.
- Create a Python script to automate the renaming of files in a directory.
Advanced Topics
- Write a Python program to implement a simple REST API using Flask.
- Create a Python script to analyze a dataset using pandas and matplotlib.
- Write a program to solve optimization problems using Python’s
scipy
library. - Demonstrate how to create a chatbot using Python and the OpenAI API.
- Write a Python script to perform image recognition using TensorFlow.
Some other Assignment Questions
-
Set Operations:
What is the output of the following code snippet?nums = set([1, 1, 2, 3, 3, 3, 4, 4]) print(len(nums))
Hint: Sets only store unique elements.
-
Dictionary Keys:
What will be the output of this Python code?d = {"john": 40, "peter": 45} print(list(d.keys()))
Hint:
d.keys()
returns the keys of a dictionary. -
Password Validation:
Write a Python program to validate a user's password based on the following criteria:- At least 1 letter between [a-z].
- At least 1 letter between [A-Z].
- At least 1 number between [0-9].
- At least 1 special character from [$#@].
- Minimum password length: 6.
- Maximum password length: 12.
-
List Iteration:
Write a program using a for loop to print all elements of a list along with their positions.
Example input:a = [4, 7, 3, 2, 5, 9]
-
Even Indexed Characters:
Write a Python program to accept a string from the console and print the characters at even indexes.
Example input:H1e2l3l4o5w6o7r8l9d
Example output:Helloworld
-
Reverse String:
Write a Python program that accepts a string from the console and prints it in reverse order.
Example input:rise to vote sir
Example output:ris etov ot esir
-
Character Frequency:
Write a Python program to count and print the frequency of each character in a given string.
Example input:abcdefgabc
Example output:a,2 b,2 c,2 d,1 e,1 f,1 g,1
-
List Intersection:
Write a program to find the intersection of two given lists:list1 = [1, 3, 6, 78, 35, 55] list2 = [12, 24, 35, 24, 88, 120, 155]
-
Remove Duplicates:
Write a Python program to remove duplicate values from the following list while preserving the original order:list_with_duplicates = [12, 24, 35, 24, 88, 120, 155, 88, 120, 155]
-
Remove Specific Value:
Using list comprehension, write a Python program to remove the value24
from the following list:original_list = [12, 24, 35, 24, 88, 120, 155]
-
Remove by Index:
Using list comprehension, write a program to remove the elements at the 0th, 4th, and 5th indexes from the following list:original_list = [12, 24, 35, 70, 88, 120, 155]
-
Filter Divisible Numbers:
Write a Python program using list comprehension to remove numbers divisible by both 5 and 7 from the following list:original_list = [12, 24, 35, 70, 88, 120, 155]
-
Random List Generation:
Write a program to randomly generate a list with 5 numbers divisible by 5 and 7, within the range of 1 to 1000 (inclusive). -
Fraction Sum Calculation:
Write a Python program to compute the sum of the series1/2 + 2/3 + 3/4 + ... + n/(n+1)
for a givenn > 0
.
Example input:n = 5
Example output:3.55