Practice Top 30 Python Programming Questions on Iterators and Iterables
Q1. Write a class Counter that implements an iterator to yield numbers starting from 1 up to a given number n.
Input:
Enter n: 5
Expected Output:
1
2
3
4
5
Q2. Write a class ReverseString that implements an iterator to iterate through a string in reverse.
Input:
Enter a string: Python
Expected Output:
n
o
h
t
y
P
Q3. Use the map function to double each number in a given list of numbers.
Input:
[1, 2, 3, 4, 5]
Expected Output:
[2, 4, 6, 8, 10]
Q4. Write an iterator InfiniteCounter that starts from a given number and keeps incrementing indefinitely. Print the first 5 numbers.
Input:
Start at 10
Expected Output:
10
11
12
13
14
Q5. Use the filter function to filter out even numbers from a list of numbers.
Input:
[1, 2, 3, 4, 5, 6]
Expected Output:
[2, 4, 6]
Q6. Write an iterator FibonacciIterator that generates Fibonacci numbers up to a given limit n.
Input:
Enter n: 10
Expected Output:
0
1
1
2
3
5
8
Q7. Use the zip function to combine two lists into a list of tuples.
Input:
List1: [1, 2, 3]
List2: ['a', 'b', 'c']
Expected Output:
[(1, 'a'), (2, 'b'), (3, 'c')]
Q8. Write a class Countdown that implements an iterator to count down from a given number n to 0.
Input:
Enter n: 3
Expected Output:
3
2
1
0
Q9. Use the map function to convert all strings in a list to uppercase.
Input:
['apple', 'banana', 'cherry']
Expected Output:
['APPLE', 'BANANA', 'CHERRY']
Q10. Write an iterator PrimeIterator that generates prime numbers up to a given limit n.
Input:
Enter n: 10
Expected Output:
2
3
5
7
Q11. Use the filter function to extract all odd numbers from a list.
Input:
[1, 2, 3, 4, 5, 6]
Expected Output:
[1, 3, 5]
Q12. Use the zip function to combine two lists of different lengths into a list of tuples, padding with None for missing values.
Input:
List1: [1, 2, 3]
List2: ['a', 'b']
Expected Output:
[(1, 'a'), (2, 'b'), (3, None)]
Q13. Write an iterator CustomRange that mimics Python's range(start, stop, step) functionality.
Input:
start: 2
stop: 10
step: 2
Expected Output:
2
4
6
8
Q14. Use the map function to square each number in a given list.
Input:
[1, 2, 3, 4, 5]
Expected Output:
[1, 4, 9, 16, 25]
Q15. Write an iterator EvenIterator that generates even numbers starting from 0 up to a given limit n.
Input:
Enter n: 10
Expected Output:
0
2
4
6
8
10
Q16. Use the filter function to remove negative numbers from a list.
Input:
[1, -2, 3, -4, 5]
Expected Output:
[1, 3, 5]
Q17. Write a class AlphabetIterator that generates the alphabet from 'a' to 'z'.
Expected Output:
a
b
c
d
e
...
z
Q18. Use the map function to add corresponding elements from two lists.
Input:
List1: [1, 2, 3]
List2: [4, 5, 6]
Expected Output:
[5, 7, 9]
Q19. Write an iterator PalindromeIterator that generates palindromes up to a given limit n.
Input:
Enter n: 100
Expected Output:
1
2
3
4
5
...
99
Q20. Use the zip function to combine two lists with different types of elements into a list of tuples.
Input:
List1: [1, 2, 3]
List2: ['a', 'b', 'c']
Expected Output:
[(1, 'a'), (2, 'b'), (3, 'c')]
Q21. Use the filter function to extract positive numbers from a list.
Input:
[1, -2, 3, -4, 5]
Expected Output:
[1, 3, 5]
Q22. Use the map function to concatenate a prefix to each string in a list.
Input:
List: ['apple', 'banana', 'cherry']
Prefix: 'fruit_'
Expected Output:
['fruit_apple', 'fruit_banana', 'fruit_cherry']
Q23. Write an iterator MultiplicationIterator that multiplies each number from a given list by a given factor.
Input:
List: [1, 2, 3, 4]
Factor: 3
Expected Output:
3
6
9
12
Q24. Use the zip function to combine two lists of different lengths, filling missing values with a placeholder.
Input:
List1: [1, 2, 3]
List2: ['a', 'b']
Expected Output:
[(1, 'a'), (2, 'b'), (3, None)]
Q25. Write an iterator DivisibleBy3Iterator that generates numbers divisible by 3 up to a given number n.
Input:
Enter n: 15
Expected Output:
3
6
9
12
15
Q26. Write an iterator DivisibleBy3Iterator that generates numbers divisible by 3 up to a given number n.
Input:
Enter n: 15
Expected Output:
3
6
9
12
15
Q27. Use the map function to find the length of each string in a given list.
Input:
['apple', 'banana', 'cherry']
Expected Output:
[5, 6, 6]
Q28. Write an iterator ReverseListIterator that iterates over a list in reverse order.
Input:
[10, 20, 30, 40]
Expected Output:
40
30
20
10
Q29. Use the zip function to create a dictionary by combining two lists, one containing keys and the other containing values.
Input:
Keys: ['name', 'age', 'city']
Values: ['John', 25, 'New York']
Expected Output:
{'name': 'John', 'age': 25, 'city': 'New York'}
Q30. Use the filter function to extract non-empty strings from a list.
Input:
['apple', '', 'banana', '', 'cherry']
Expected Output:
['apple', 'banana', 'cherry']
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!