0% found this document useful (0 votes)
2 views

Class 8th Practice set of Python

This document is a practice set for Class 8 students focusing on Python programming concepts. It contains 10 sections, each with 5 questions covering topics such as strings, lists, tuples, dictionaries, indexing, slicing, loops, sets, and nested conditional loops. The questions are designed to test and enhance students' understanding and skills in Python programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Class 8th Practice set of Python

This document is a practice set for Class 8 students focusing on Python programming concepts. It contains 10 sections, each with 5 questions covering topics such as strings, lists, tuples, dictionaries, indexing, slicing, loops, sets, and nested conditional loops. The questions are designed to test and enhance students' understanding and skills in Python programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Class 8 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.

7. For Loop (5 Questions)


1. Write a for loop to print each character in "Python" without using the in keyword.
2. How can you use a for loop to find the sum of all even numbers from 1 to 100?
3. Write a for loop to print only the unique elements from a list list1 = [1, 2, 2, 3, 4, 4,
5].
4. How can you create a for loop to iterate through both keys and values of a dictionary
at once?
5. Can you write a for loop to print numbers from 10 to 1 in reverse order without using
range()?

8. While Loop (5 Questions)


1. Write a while loop to reverse a number 12345 without using string conversion.
2. How can you use a while loop to print only the odd numbers between 1 and 50?
3. Write a while loop that counts how many digits are in a number like 1005201.
4. How would you create a while loop that continues until a user inputs the word
"stop"?
5. Write a while loop to find the sum of digits in a number 54321.

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.

10. Nested Conditional Loops (5 Questions)


1. Write a nested loop to print all possible pairs (i, j) where i and j range from 1 to 5, but
only if i is not equal to j.
2. Write a nested for loop with an if condition to print all prime numbers between 1 and
50.
3. Given two lists, A = [1, 2, 3] and B = [4, 5, 6], write a nested loop to multiply each
element in A with each element in B and store the result in a list.
4. Using a while loop within a for loop, create a code to find the factors of all numbers
from 1 to 20.
5. Write a nested loop with conditional statements to print a pattern where even
numbers are ignored and only odd numbers from 1 to 10 are printed in rows.

Prepared by – Anupam J

You might also like