Python Assignment Questions: Data Structures, Strings, Functions & Recursion¶
1. String Creation and Basic Operations
Write a program that creates different types of strings (single quotes, double quotes, triple quotes) and prints them.
Hint: Use different quote types for string creation.
2. String Indexing
Take a string as input and print the first, last, and middle characters.
Hint: Use positive and negative indexing.
3. String Slicing
Input a sentence and extract the first 5 characters, last 5 characters, and every second character.
Hint: Use slicing with different parameters.
4. String Immutability Demonstration
Show that strings are immutable by trying to change a character and then creating a new string.
Hint: Attempt to modify a string character and handle the error.
5. Uppercase and Lowercase
Take a string input and print it in both uppercase and lowercase.
Hint: Use upper() and lower() methods.
6. Find Substring
Input a main string and a substring, then find the position of the substring.
Hint: Use find() method and handle the case when substring is not found.
7. Replace Words
Take a sentence and replace all occurrences of “old” with “new”.
Hint: Use replace() method.
8. Split and Join
Input a comma-separated string, split it into a list, then join it back with spaces.
Hint: Use split() and join() methods.
9. List Creation and Access
Create a list of 5 numbers, print the entire list, first element, and last element.
Hint: Use square brackets for list creation and indexing.
10. List Modification
Create a list, add an element, remove an element, and modify an existing element.
Hint: Use append(), remove(), and indexing for modification.
11. List Slicing
Create a list of 10 numbers and demonstrate different slicing operations.
Hint: Use various slicing patterns like [2:5], [::2], [::-1].
12. Dictionary Operations
Create a dictionary with student information, add a new key-value pair, and modify an existing value.
Hint: Use dictionary literal syntax and key access.
13. Dictionary Methods
Create a dictionary and demonstrate keys(), values(), and items() methods.
Hint: Call these methods on the dictionary object.
14. Set Operations
Create two sets and demonstrate union, intersection, and difference operations.
Hint: Use |, &, and - operators or corresponding methods.
15. Function Definition
Write a function that takes two numbers and returns their sum.
Hint: Use def keyword, parameters, and return statement.
16. Function with Default Parameters
Write a function that greets a person with a default greeting message.
Hint: Use default parameter values in function definition.
17. Recursion - Factorial
Write a recursive function to calculate factorial of a number.
Hint: Base case: n == 0 or n == 1, recursive case: n * factorial(n-1).
18. Recursion - Fibonacci
Write a recursive function to find the nth Fibonacci number.
Hint: Base cases: n == 0 or n == 1, recursive case: fibonacci(n-1) + fibonacci(n-2).
19. Lambda Function
Create a lambda function that squares a number and use it with map().
Hint: Use lambda x: x**2 and map() function.
20. Filter with Lambda
Use filter() with a lambda function to get even numbers from a list.
Hint: Use lambda x: x % 2 == 0 with filter().