Python Logical Questions (NCERT Class 12 Level)
Strings
1. Count the number of vowels in a string. Example: Input: 'hello world' -> Output: 3
2. Check whether a given string is a palindrome. Example: Input: 'madam' -> Output: True
3. Replace all occurrences of 'a' with '@'. Example: Input: 'apple' -> Output: '@pple'
4. Convert the first letter of each word to uppercase. Input: 'hello world' -> Output: 'Hello World'
5. Find the frequency of each character. Input: 'banana' -> Output: {'b':1,'a':3,'n':2}
6. Reverse a string. Input: 'python' -> Output: 'nohtyp'
7. Find the longest word in a string. Input: 'I love programming' -> Output: 'programming'
8. Count the number of words. Input: 'Hello from the other side' -> Output: 5
9. Check if two strings are anagrams. Input: 'listen', 'silent' -> Output: True
10. Remove all punctuation. Input: 'Hello, world!' -> Output: 'Hello world'
11. Count occurrences of a specific word. Input: 'hello world hello' -> Output for 'hello': 2
12. Replace spaces with hyphens. Input: 'hello world' -> Output: 'hello-world'
13. Check whether the string starts with a vowel. Input: 'apple' -> Output: True
14. Swap the case of each letter. Input: 'HeLLo' -> Output: 'hEllO'
15. Print all substrings. Input: 'abc' -> Output: 'a','ab','abc','b','bc','c'
16. Check if string contains only digits. Input: '12345' -> Output: True
17. Count uppercase and lowercase letters. Input: 'Hello' -> Output: upper:1, lower:4
18. Remove duplicate characters. Input: 'aabbcc' -> Output: 'abc'
19. Find index of first occurrence of substring. Input: 'hello', 'l' -> Output: 2
20. Remove all spaces. Input: 'a b c' -> Output: 'abc'
Lists
1. Find max and min in a list. Input: [3,1,4] -> Output: max=4, min=1
2. Count specific element. Input: [1,2,2,3], element: 2 -> Output: 2
3. Remove all even numbers. Input: [1,2,3,4] -> Output: [1,3]
4. Reverse a list manually. Input: [1,2,3] -> Output: [3,2,1]
5. Sort without sort(). Input: [3,1,2] -> Output: [1,2,3]
6. Find sum and average. Input: [2,4,6] -> Output: sum=12, avg=4
7. Merge two lists. Input: [1,2], [3,4] -> Output: [1,2,3,4]
8. Remove duplicates. Input: [1,2,2,3] -> Output: [1,2,3]
9. Find second largest. Input: [5,1,4] -> Output: 4
10. Separate even and odd. Input: [1,2,3,4] -> Output: even=[2,4], odd=[1,3]
11. Count prime numbers. Input: [2,3,4] -> Output: 2
12. Find product of elements. Input: [1,2,3] -> Output: 6
13. List of squares. Input: [1,2,3] -> Output: [1,4,9]
14. Rotate right by 2. Input: [1,2,3,4] -> Output: [3,4,1,2]
15. Check if list is sorted. Input: [1,2,3] -> Output: True
16. Find common elements. Input: [1,2], [2,3] -> Output: [2]
17. Check if lists are identical. Input: [1,2], [1,2] -> Output: True
18. Find difference between max and min. Input: [3,7] -> Output: 4
19. List of positives. Input: [-1,2,-3,4] -> Output: [2,4]
20. Flatten nested list. Input: [[1,2],[3,4]] -> Output: [1,2,3,4]
Dictionaries
1. Create dictionary of student marks. Input: names=['A','B'], marks=[90,80] -> Output: {'A':90,'B':80}
2. Update a value. Input: {'a':1}, update 'a' to 2 -> Output: {'a':2}
3. Delete a key. Input: {'a':1,'b':2}, delete 'a' -> Output: {'b':2}
4. Check if key exists. Input: 'a' in {'a':1} -> Output: True
5. Word frequency. Input: 'a a b' -> Output: {'a':2,'b':1}
6. Merge dictionaries. Input: {'a':1}, {'b':2} -> Output: {'a':1,'b':2}
7. Dict from lists. keys=[1,2], vals=['a','b'] -> Output: {1:'a',2:'b'}
8. Sort dict by value. Input: {'a':2,'b':1} -> Output: {'b':1,'a':2}
9. Key with max value. Input: {'a':5,'b':9} -> Output: 'b'
10. Dict to list of tuples. Input: {'a':1,'b':2} -> Output: [('a',1),('b',2)]
11. Dict of squares. Input: [1,2,3] -> Output: {1:1,2:4,3:9}
12. Check if value exists. Input: 2 in {'a':2} -> Output: True
13. Char frequency. Input: 'aaabb' -> Output: {'a':3,'b':2}
14. Filter values >50. Input: {'a':60,'b':30} -> Output: {'a':60}
15. Swap keys and values. Input: {'a':1,'b':2} -> Output: {1:'a',2:'b'}
16. Sum all values. Input: {'a':1,'b':2} -> Output: 3
17. Average of values. Input: {'a':10,'b':20} -> Output: 15
18. Add new key. Input: {'a':1}, add 'b':2 -> Output: {'a':1,'b':2}
19. Copy dictionary. Input: {'a':1} -> Output: {'a':1}
20. Clear dictionary. Input: {'a':1} -> Output: {}
Tuples
1. Access tuple element. Input: (1,2,3)[1] -> Output: 2
2. Count element. Input: (1,2,2,3), count 2 -> Output: 2
3. Find index. Input: (1,2,3), index of 2 -> Output: 1
4. Tuple to list. Input: (1,2) -> Output: [1,2]
5. Check membership. Input: 2 in (1,2,3) -> Output: True
6. Concatenate tuples. (1,2)+(3,) -> Output: (1,2,3)
7. Sum elements. Input: (1,2,3) -> Output: 6
8. Find max/min. Input: (1,3,2) -> Output: max=3, min=1
9. Slice tuple. Input: (1,2,3,4)[1:3] -> Output: (2,3)
10. List of tuples to dict. [(1,'a'),(2,'b')] -> Output: {1:'a',2:'b'}
11. Sort tuple. Input: tuple(sorted((3,1,2))) -> Output: (1,2,3)
12. Repeat tuple. Input: (1,2)*2 -> Output: (1,2,1,2)
13. Unpack tuple. Input: (1,2) -> Output: a=1, b=2
14. Merge element-wise. (1,2)+(3,4) -> Output: (1,2,3,4)
15. Count even/odd. Input: (1,2,3,4) -> Output: even=2, odd=2
16. Remove duplicates. Input: tuple(set((1,2,2))) -> Output: (1,2)
17. Replace element. (1,2,3) -> list->change->tuple -> Output: (1,9,3)
18. Multiply elements. Input: (1,2,3) -> Output: 6
19. Filter strings. Input: (1,'a',2) -> Output: ('a',)
20. Length of strings. Input: ('hi','there') -> Output: [2,5]