Python Coding Questions and Solutions
Python coding practice is an effective way to strengthen your understanding of built-in data structures and problem-solving techniques. Lists, tuples, sets, and nested data structures are frequently used in Python programs, so learning how to manipulate them is an important part of building strong programming fundamentals.
The following coding questions provide practical examples of working with these structures through reusable Python functions.
Table of Contents

YT:- DecodeIT
List Manipulation
1. Double Every Number in a List
Write a function that receives a list of numbers and creates another list containing twice the value of each element.
def double_numbers(numbers):
return [number * 2 for number in numbers]
# Example usage:
numbers = [1, 2, 3, 4, 5]
doubled = double_numbers(numbers)
print(doubled)
# Output: [2, 4, 6, 8, 10]
The list comprehension processes each value individually and places its doubled result into the new list.
2. Find Common Values Between Two Lists
Create a function that identifies the elements shared by two lists.
def common_elements(list1, list2):
return list(set(list1) & set(list2))
# Example usage:
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
common = common_elements(list1, list2)
print(common)
# Output: [4, 5]
The lists are converted into sets so that the intersection operator can be used to identify values appearing in both collections.
Working with Tuples
1. Find People Above the Age of 18
Suppose each tuple contains a person’s name and age. Write a function that extracts the names of people whose age is greater than 18.
def names_above_18(people):
return [name for name, age in people if age > 18]
# Example usage:
people = [
("Alice", 17),
("Bob", 20),
("Charlie", 19)
]
adults = names_above_18(people)
print(adults)
# Output: ['Bob', 'Charlie']
The function unpacks each tuple into name and age, then keeps only the records that satisfy the age condition.
2. Sort the Values of a Tuple
Write a function that accepts a tuple containing numbers and returns another tuple with the values arranged from smallest to largest.
def sort_tuple(numbers):
return tuple(sorted(numbers))
# Example usage:
numbers = (5, 2, 3, 1, 4)
sorted_numbers = sort_tuple(numbers)
print(sorted_numbers)
# Output: (1, 2, 3, 4, 5)
The sorted() function produces an ordered sequence, which is then converted back into a tuple.
Set Operations
1. Calculate the Symmetric Difference
Create a function that returns values belonging to either of two sets but excludes values that occur in both sets.
def symmetric_difference(set1, set2):
return set1 ^ set2
# Example usage:
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
sym_diff = symmetric_difference(set1, set2)
print(sym_diff)
# Output: {1, 2, 5, 6}
The ^ operator performs a symmetric difference between the two sets.
2. Check Whether One Set Is a Subset
Write a function that determines whether every element in one set is also present in another set.
def is_subset(subset, superset):
return subset <= superset
# Example usage:
set1 = {1, 2}
set2 = {1, 2, 3, 4}
result = is_subset(set1, set2)
print(result)
# Output: True
The <= operator checks whether the first set is completely contained within the second set.
Working with Nested Data Structures
1. Calculate the Average Grade
Consider a list containing dictionaries for different students. Each dictionary stores information such as the student’s name, age, and grade. Create a function that calculates the average grade.
def average_grade(students):
total_grade = sum(student["grade"] for student in students)
return total_grade / len(students)
# Example usage:
students = [
{"name": "Alice", "age": 20, "grade": 85},
{"name": "Bob", "age": 21, "grade": 90},
{"name": "Charlie", "age": 19, "grade": 95}
]
avg_grade = average_grade(students)
print(avg_grade)
# Output: 90.0
The function adds the grade from each dictionary and divides the total by the number of student records.
2. Convert a Nested List into a Single List
Write a function that takes a list containing multiple smaller lists and combines their elements into one list.
def flatten_list(nested_list):
return [item for sublist in nested_list for item in sublist]
# Example usage:
nested_list = [
[1, 2],
[3, 4],
[5, 6]
]
flattened = flatten_list(nested_list)
print(flattened)
# Output: [1, 2, 3, 4, 5, 6]
The nested list comprehension visits each inner list and then adds its individual elements to the resulting list.
Final Thoughts
These Python coding exercises provide hands-on practice with lists, tuples, sets, dictionaries, and nested structures. They also demonstrate useful techniques such as list comprehensions, set operations, tuple conversion, filtering, and working with dictionary data.
Try changing the input values and modifying the functions to solve similar problems. Practicing variations of these questions can improve your understanding of Python data structures and strengthen your problem-solving skills.
Complete Advance AI Topics: Click Here
SQL Tutorial: Click Here
Frequently Asked Questions
1. Which Python data structures are covered in these coding questions?
The examples cover lists, tuples, sets, dictionaries, and nested combinations of these structures.
2. Why are list comprehensions useful in Python?
List comprehensions provide a concise way to create a new list by processing or filtering elements from an existing iterable.
3. What is the purpose of converting lists to sets?
Converting lists to sets can be useful when performing set operations such as intersection, union, and symmetric difference.
4. What does the symmetric difference operator do?
It returns elements that belong to either set but excludes elements that are present in both sets.
5. What is a nested data structure?
A nested data structure is a structure that contains another data structure inside it, such as a list containing dictionaries or a list containing other lists.
Keywords: Python coding questions, Python coding questions and answers,Python Coding Questions and Solutions, Python coding solutions, Python list coding questions, Python tuple questions, Python set questions, Python data structure coding questions, Python programming exercises, Python practice questions, Python coding problems,Python Coding Questions and Solutions