Python Practice Questions with Answers
1. Find the Largest Number in a List
Question: Take a list of numbers and find the largest number.
Answer
numbers = [12, 45, 8, 67, 23]
largest = numbers[0]
for num in numbers:
if num > largest:
largest = num
print("Largest:", largest)
2. Count Even Numbers in a List
Question: Count how many even numbers are present in a list.
Answer
numbers = [1, 2, 4, 7, 8, 10, 13]
count = 0
for num in numbers:
if num % 2 == 0:
count += 1
print(count)
3. Reverse a List Using Slicing
Question: Reverse a list without using reverse().
Answer
numbers = [10, 20, 30, 40, 50]
reversed_list = numbers[::-1]
print(reversed_list)
4. Find Length of a Tuple
Question: Find the number of elements in a tuple.
Answer
data = (10, 20, 30, 40, 50)
print(len(data))
5. Create Squares Using List Comprehension
Question: Create a list containing squares of numbers from 1 to 10.
Answer
squares = [x**2 for x in range(1, 11)]
print(squares)
6. Use Ternary Operator
Question: Print "Pass" if marks are 40 or above, otherwise print "Fail".
Answer
marks = 55
result = "Pass" if marks >= 40 else "Fail"
print(result)
7. Find Unique Elements
Question: Remove duplicate elements from a list.
Answer
numbers = [1, 2, 2, 3, 4, 4, 5]
unique = list(set(numbers))
print(unique)
8. Count Frequency of Each Element
Question: Count how many times each element appears in a list.
Answer
numbers = [1, 2, 2, 3, 1, 4, 2]
freq = {}
for num in numbers:
freq[num] = [Link](num, 0) + 1
print(freq)
9. Find Common Elements Between Two Lists
Answer
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7]
common = []
for item in list1:
if item in list2:
[Link](item)
print(common)
10. Separate Even and Odd Numbers
Answer
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
even = []
odd = []
for num in numbers:
if num % 2 == 0:
[Link](num)
else:
[Link](num)
print("Even:", even)
print("Odd:", odd)
11. Merge Two Dictionaries
Answer
d1 = {"a": 1, "b": 2}
d2 = {"c": 3, "d": 4}
[Link](d2)
print(d1)
12. Find Students with Marks Above 75
Answer
marks = {
"Aman": 82,
"Riya": 65,
"Raj": 91,
"Priya": 74
}
for student, score in [Link]():
if score > 75:
print(student)
13. Create List of Even Numbers Using List Comprehension
Answer
evens = [x for x in range(1, 51) if x % 2 == 0]
print(evens)
14. Find Second Largest Number
Answer
numbers = [12, 45, 8, 67, 23, 55]
[Link]()
print(numbers[-2])
15. Find Missing Number
Question: A list contains numbers from 1 to 10 with one number missing. Find the missing number.
Answer
numbers = [1, 2, 3, 4, 5, 7, 8, 9, 10]
for i in range(1, 11):
if i not in numbers:
print("Missing:", i)
16. Word Frequency Counter
Answer
sentence = "python is easy and python is powerful"
words = [Link]()
freq = {}
for word in words:
freq[word] = [Link](word, 0) + 1
print(freq)
17. Find Duplicate Elements
Answer
numbers = [1, 2, 3, 2, 4, 5, 1, 6]
duplicates = set()
for num in numbers:
if [Link](num) > 1:
[Link](num)
print(duplicates)
18. Matrix Row Sum
Question: Find the sum of each row in a 2D list.
Answer
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
for row in matrix:
print(sum(row))
19. Find Dictionary Key with Highest Value
Answer
scores = {
"Aman": 85,
"Riya": 92,
"Raj": 88
}
highest = max(scores, key=[Link])
print(highest)
20. Set Operations
Question: Find union, intersection, and difference of two sets.
Answer
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7}
print("Union:", A | B)
print("Intersection:", A & B)
print("Difference:", A - B)