Interview Question

Python Coding Question Solution

Python Coding Question Solution
Menu

Python Coding Question Solution List , Tuple etc…

List Manipulation

  1. 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]

  1. 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

  1. 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']

  1. 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

  1. 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}

  1. 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

  1. 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

  1. 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.

 

Source Code Available

Interested in This Project?

Get the complete source code for this project at a very affordable price — perfect for your portfolio, college submission, or learning. Message us on WhatsApp and we'll get back to you instantly!

Full source code included Step-by-step setup guide Instant delivery on WhatsApp Instant reply on WhatsApp
Chat on WhatsApp

We usually reply within a few minutes

Leave a Reply

Your email address will not be published. Required fields are marked *

Chat with us