Skip to content
  • SiteMap
  • Our Services
  • Frequently Asked Questions (FAQ)
  • Support
  • About Us

UpdateGadh

Update Your Skills.

  • Home
  • Projects
    •  Blockchain projects
    • Python Project
    • Data Science
    •  Ai projects
    • Machine Learning
    • PHP Project
    • React Projects
    • Java Project
    • SpringBoot
    • JSP Projects
    • Java Script Projects
    • Code Snippet
    • Free Projects
  • Tutorials
    • Ai
    • Machine Learning
    • Advance Python
    • Advance SQL
    • DBMS Tutorial
    • Data Analyst
    • Deep Learning Tutorial
    • Data Science
    • Nodejs Tutorial
  • Blog
  • Contact us
  • Toggle search form

Python Coding Question Solution

Posted on August 23, 2024March 14, 2025 By Updategadh No Comments on Python Coding Question Solution

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.

 

Post Views: 644
Interview Question Tags:Coding Question Solution, Free Project, Interview Question, Java

Post navigation

Previous Post: Top 10 Spring Boot Projects You Must Try: Detailed Guide
Next Post: Snake Game In Python With free Source Code

More Related Articles

Java Interview Questions Java Interview Questions -Set 8 Interview Question
Top 10 Final Year Project Ideas for Java - Final Year Project Ideas for Java Top 10 Final Year Project Ideas for Java Interview Question
Best Project Ideas for Beginner Students - Best Project Ideas for Beginner Students Best Project Ideas for Beginner Students Interview Question

Leave a Reply Cancel reply

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

You may also like

  1. How to build an AI System Step-By-Step Guide [ Create an Ai ]
  2. Top 30 Coding Interview Questions You Should Know !
  3. Top 10 Real-Time Python Projects – Get Started Today
  4. Top 20 Web Application Interview Questions
  5. Swift Interview Questions and Answers: A Comprehensive Guide
  6. Popular Java Coding Questions & Answer for 2025

Most Viewed Posts

  1. Top Large Language Models in 2025
  2. Online Shopping System using PHP, MySQL with Free Source Code
  3. login form in php and mysql , Step-by-Step with Free Source Code
  4. Flipkart Clone using PHP And MYSQL Free Source Code
  5. News Portal Project in PHP and MySql Free Source Code
  6. User Login & Registration System Using PHP and MySQL Free Code
  7. Top 10 Final Year Project Ideas in Python
  8. Online Bike Rental Management System Using PHP and MySQL
  9. E learning Website in php with Free source code
  10. E-Commerce Website Project in Java Servlets (JSP)
  • AI
  • ASP.NET
  • Blockchain
  • ChatCPT
  • code Snippets
  • Collage Projects
  • Data Science Project
  • Data Science Tutorial
  • DBMS Tutorial
  • Deep Learning Tutorial
  • Final Year Projects
  • Free Projects
  • How to
  • html
  • Interview Question
  • Java Notes
  • Java Project
  • Java Script Notes
  • JAVASCRIPT
  • Javascript Project
  • JSP JAVA(J2EE)
  • Machine Learning Project
  • Machine Learning Tutorial
  • MySQL Tutorial
  • Node.js Tutorial
  • PHP Project
  • Portfolio
  • Python
  • Python Interview Question
  • Python Projects
  • PythonFreeProject
  • React Free Project
  • React Projects
  • Spring boot
  • SQL Tutorial
  • TOP 10
  • Uncategorized
  • Online Examination System in PHP with Source Code
  • AI Chatbot for College and Hospital
  • Job Portal Web Application in PHP MySQL
  • Online Tutorial Portal Site in PHP MySQL — Full Project with Source Code
  • Online Job Portal System in JSP Servlet MySQL

Most Viewed Posts

  • Top Large Language Models in 2025 (8,612)
  • Online Shopping System using PHP, MySQL with Free Source Code (5,210)
  • login form in php and mysql , Step-by-Step with Free Source Code (4,863)

Copyright © 2026 UpdateGadh.

Powered by PressBook Green WordPress theme