Class 8th Practice set of Python
Class 8th Practice set of Python
th
1. Strings (5 Questions)
1. How would you extract every alternate character from the string "butterfly" and
store it in a new variable?
2. If you have a string "banana" and you want to count how many times the letter 'a'
appears, what is the most efficient way to do so?
3. Given a string "abcde", write a Python statement to replace 'c' with 'z' without
modifying the original string.
4. How can you check if a string starts with a certain substring (like "pre") and ends with
another (like "ing")?
5. If str1 = "Hello World", write a code to reverse only the second word, so the output is
"Hello dlroW".
2. Lists (5 Questions)
1. If you have a list [1, 2, 3, [4, 5, [6, 7]], 8], how would you access the element 6?
2. How can you find the second largest number in a list without using any sorting
functions?
3. Write a Python statement to remove all duplicates from a list, but keep the original
order of elements.
4. If list1 = [1, 2, 3, 4], write a one-liner code to convert it to [4, 3, 2, 1].
5. Given a list of names ["Alice", "Bob", "Alice", "Eve", "Bob"], how would you count the
occurrence of each name and display it in a dictionary?
3. Tuples (5 Questions)
1. If tuple1 = (1, 2, 3, 4), can you change the third element to 9? If not, how would you
create a similar tuple with 9 in place of 3?
2. How can you unpack a tuple (3, 6, 9, 12) so that each element is assigned to a unique
variable?
3. Given tuple1 = (1, [2, 3], 4), can you modify the list inside the tuple to add the
number 5? Why or why not?
4. If you have a tuple ("apple", "banana", "apple"), how can you determine the index of
the first occurrence of "apple"?
5. Is it possible to concatenate (1, 2) and (3, 4, 5) without creating a new tuple variable?
Explain.
4. Dictionaries (5 Questions)
1. Given dict1 = {'a': 1, 'b': 2, 'c': 3}, write a one-liner code to swap keys and values.
2. How would you merge two dictionaries, dict1 = {'a': 1, 'b': 2} and dict2 = {'b': 3, 'c': 4},
so that in case of overlapping keys, dict2’s value is kept?
3. How would you check if two dictionaries have any keys in common?
4. Write a Python expression to retrieve the maximum value from a dictionary {'x': 5,
'y': 10, 'z': 2} without using a loop.
5. How would you remove a key from a dictionary and return its value at the same
time?
5. Indexing (5 Questions)
1. Given the list numbers = [10, 20, 30, 40, 50], write a statement to get the last
element using negative indexing.
2. In a list letters = ['a', 'b', 'c', 'd'], how would you access the second-to-last item?
3. If str1 = "programming", what index would you use to get the substring "gram"?
4. Given list1 = [10, 20, [30, 40], 50], how would you retrieve 40 using indexing?
5. If you have a list of numbers [1, 3, 5, 7, 9], what happens if you try to access an index
that does not exist, like numbers[5]? How can you handle this in Python?
6. Slicing (5 Questions)
1. Given a list numbers = [1, 2, 3, 4, 5, 6, 7], write a statement to slice and get only the
odd numbers.
2. In a string "abcdefgh", how would you use slicing to reverse it?
3. If you have list1 = [10, 20, 30, 40, 50], what slice would you use to get [20, 30, 40]?
4. How can you get every third character from the string "abcdefghijkl" using slicing?
5. What does list1[2:5:-1] return if list1 = [1, 2, 3, 4, 5, 6]? Explain why.
9. Sets (5 Questions)
1. Given two sets, A = {1, 2, 3, 4} and B = {3, 4, 5, 6}, write a code to find elements that
are in either set, but not both.
2. How can you use sets to check if all elements in list1 are unique?
3. If you have set1 = {10, 20, 30}, how would you add multiple values [40, 50, 60] at
once?
4. How can you remove an element from a set without knowing if it exists in the set?
5. Write a Python statement that takes two sets and returns their symmetric difference.
Prepared by – Anupam J