Python Coding Question Solution
Python Coding Question Solution List , Tuple etc…
Table of Contents
List Manipulation
- Write a function that takes a list of numbers and returns a new list with each number doubled.
def double_numbers(numbers):
return [x * 2 for x in numbers]
# Example usage:
numbers = [1, 2, 3, 4, 5]
doubled = double_numbers(numbers)
print(doubled) # Output: [2, 4, 6, 8, 10]
- Create a function that takes two lists and returns a list that contains only the common elements.
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]
Working with Tuples
- Write a function that takes a list of tuples, each containing a name and age, and returns a list of names of people who are above 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']
- Create a function that accepts a tuple of numbers and returns a new tuple with the numbers sorted in ascending order.
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)
Set Operations
- Write a function that takes two sets and returns their symmetric difference (elements that are in either set, but not in both).
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}
- Create a function to check if one set is a subset of another.
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
Nested Data Structures
- Given a list of dictionaries representing students (each dictionary containing keys like name, age, and grades), write a function to calculate the average grade of all students.
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
- Write a function that flattens a list of lists into a single list (e.g., [[1, 2], [3, 4]] should become [1, 2, 3, 4]).
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]
These solutions demonstrate basic list, tuple, and set operations in Python, as well as handling more complex nested data structures.
codinn
Python program that takes a list of tuples, each containing a name and age, and returns a list of names of people who are above 18, without using a function call.
“`python
# List of tuples with names and ages
people = [(“Alice”, 17), (“Bob”, 20), (“Charlie”, 16), (“Diana”, 22), (“Edward”, 19)]
# The journey to find those who have crossed the threshold of adulthood
adults = []
# The moment of truth – checking each person’s age
for person in people:
name, age = person
if age > 18:
adults.append(name)
# Celebrating the outcome – those who are above 18!
print(“These are the legends, the ones who stand tall above 18: “, adults)
“`
2 comments