0% found this document useful (0 votes)
4 views9 pages

Python

The document contains 30 Python questions ranging from beginner to intermediate level, covering basic operations, data structures, and string manipulations. Additionally, it includes 10 scenario-based questions that apply Python concepts to practical problems, such as counting word frequency and finding duplicates in a list. Each question is accompanied by sample code and expected output.

Uploaded by

EBWW
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

Python

The document contains 30 Python questions ranging from beginner to intermediate level, covering basic operations, data structures, and string manipulations. Additionally, it includes 10 scenario-based questions that apply Python concepts to practical problems, such as counting word frequency and finding duplicates in a list. Each question is accompanied by sample code and expected output.

Uploaded by

EBWW
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

30 Python Questions (Beginner to Intermediate)

----------------------------------------------------------

Q1. Print "Hello World"


print("Hello World")
# Output: Hello World

Q2. Add two numbers


a, b = 10, 5
print(a + b)
# Output: 15

Q3. Swap two variables


a, b = 5, 10
a, b = b, a
print(a, b)
# Output: 10 5

Q4. Check even or odd


n=4
print("Even" if n % 2 == 0 else "Odd")
# Output: Even

Q5. Factorial using loop


n=5
fact = 1
for i in range(1, n+1):
fact *= i
print(fact)
# Output: 120
Q6. Check prime number
n=7
is_prime = all(n % i != 0 for i in range(2, n))
print(is_prime)
# Output: True

Q7. Fibonacci series ( rst 5 terms)


a, b = 0, 1
for _ in range(5):
print(a, end=" ")
a, b = b, a + b
# Output: 0 1 1 2 3

Q8. Reverse a string


s = "hello"
print(s[::-1])
# Output: olleh

Q9. Check palindrome


s = "madam"
print("Palindrome" if s == s[::-1] else "Not")
# Output: Palindrome

Q10. Count vowels


s = "Python"
print(sum(1 for c in s.lower() if c in "aeiou"))
# Output: 1

Q11. ASCII value of a character


print(ord('A'))
# Output: 65
fi
Q12. Character from ASCII
print(chr(66))
# Output: B

Q13. Largest of 3 numbers


a, b, c = 5, 12, 7
print(max(a, b, c))
# Output: 12

Q14. Sum of digits


n = 123
print(sum(int(d) for d in str(n)))
# Output: 6

Q15. Check Armstrong number (153)


n = 153
print(n == sum(int(d)**3 for d in str(n)))
# Output: True

Q16. Find length of list


l = [1, 2, 3]
print(len(l))
# Output: 3

Q17. List comprehension: squares of 1 to 5


print([x**2 for x in range(1, 6)])
# Output: [1, 4, 9, 16, 25]
Q18. Remove duplicates from list
l = [1, 2, 2, 3]
print(list(set(l)))
# Output: [1, 2, 3]

Q19. Sort a list


l = [4, 2, 7, 1]
l.sort()
print(l)
# Output: [1, 2, 4, 7]

Q20. Dictionary access


d = {"a": 1, "b": 2}
print(d["b"])
# Output: 2

Q21. Iterate through dictionary


for k, v in {"x": 1, "y": 2}.items():
print(k, v)
# Output:
#x1
#y2

Q22. Sum dictionary values


d = {"a": 1, "b": 2}
print(sum(d.values()))
# Output: 3
Q23. Tuple unpacking
a, b = (1, 2)
print(a + b)
# Output: 3

Q24. String formatting


name = "Alice"
print(f"Hello, {name}")
# Output: Hello, Alice

Q25. Check substring


print("py" in "python")
# Output: True

Q26. Replace substring


print("apple".replace("p", "b"))
# Output: abble

Q27. Split string


print("a,b,c".split(","))
# Output: ['a', 'b', 'c']

Q28. Join list to string


print("-".join(['1', '2', '3']))
# Output: 1-2-3

Q29. Reverse a list


l = [1, 2, 3]
l.reverse()
print(l)
# Output: [3, 2, 1]
Q30. Count element in list
l = [1, 1, 2]
print(l.count(1))
# Output: 2

10 Scenario-Based Python Questions with Output


----------------------------------------------

Q1. Count word frequency in a paragraph

text = "Python is great. Python is easy."


words = text.lower().replace(".", "").split()
freq = {w: words.count(w) for w in set(words)}
print(freq)

# Output: {'great': 1, 'is': 2, 'easy': 1, 'python': 2}

Q2. Find duplicate elements in a list

lst = [1, 2, 3, 2, 4, 1]
duplicates = set([x for x in lst if lst.count(x) > 1])
print(duplicates)

# Output: {1, 2}

Q3. Remove special characters from string

s = "H@e#l$l%o^"
cleaned = ''.join(c for c in s if c.isalnum())
print(cleaned)

# Output: Hello

Q4. Find most frequent character

s = "banana"
from collections import Counter
print(Counter(s).most_common(1)[0][0])

# Output: a

Q5. Reverse each word in a sentence

sentence = "Python is fun"


reversed_words = ' '.join(word[::-1] for word in
sentence.split())
print(reversed_words)

# Output: nohtyP si nuf

Q6. Flatten a nested list

nested = [[1, 2], [3, 4], [5]]


at = [item for sublist in nested for item in sublist]
print( at)

# Output: [1, 2, 3, 4, 5]
fl
fl
Q7. Find common elements in two lists

a = [1, 2, 3, 4]
b = [3, 4, 5, 6]
print(list(set(a) & set(b)))

# Output: [3, 4]

Q8. Group words by their length

words = ["apple", "cat", "banana", "dog"]


from collections import defaultdict
grouped = defaultdict(list)
for word in words:
grouped[len(word)].append(word)
print(dict(grouped))

# Output: {5: ['apple'], 3: ['cat', 'dog'], 6: ['banana']}

Q9. Find numbers divisible by both 3 and 5 from 1–


100

print([i for i in range(1, 101) if i % 3 == 0 and i % 5 == 0])

# Output: [15, 30, 45, 60, 75, 90]


Q10. Count unique characters in a string

s = "hello world"
print(len(set(s.replace(" ", “"))))

# Output: 7

You might also like