Python DSA


DSA stands for Data Structures and Algorithms. In Python, DSA helps in solving real-world problems efficiently using organized data and logical problem-solving steps.


🔹 What are Data Structures?

Data Structures are ways to store and organize data.

Built-in Python Data Structures:
Data Structure Example Type Use for
List [] Ordered, dynamic collections
Tuple () Immutable ordered collections
Set {} Unordered unique items
Dictionary {key: value} Key-value pairs

🔹 Algorithms in Python

An algorithm is a step-by-step procedure to solve a problem.

Example: A sorting algorithm arranges data in a specific order.


🔹 Common DSA Topics in Python

1. Searching
  • Linear Search

  • Binary Search

2. Sorting
  • Bubble Sort

  • Insertion Sort

  • Merge Sort

  • Quick Sort

3. Recursion
  • A function that calls itself to break problems into subproblems.

4. Linked Lists
  • Nodes connected in a sequence

5. Stacks & Queues
  • Stack → LIFO (Last In, First Out)

  • Queue → FIFO (First In, First Out)


🔹 Example: Linear Search

def linear_search(arr, target):
    for i in range(len(arr)):
        if arr[i] == target:
            return i
    return -1

print(linear_search([1, 3, 5, 7], 5))  # Output: 2

🔹 Example: Bubble Sort

def bubble_sort(arr):
    n = len(arr)
    for i in range(n):
        for j in range(n - i - 1):
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]
    return arr

print(bubble_sort([5, 1, 4, 2, 8]))  # Output: [1, 2, 4, 5, 8]

Practice Questions

Q1. Write a Python program to create a list of numbers and search for a specific number using a loop.

Q2. Write a Python program to create a function that reverses a string using a stack.

Q3. Write a Python program to create a dictionary with student names as keys and their marks as values.

Q4. Write a Python program to implement bubble sort on a list of integers.

Q5. Write a Python program to use a tuple to store a date of birth (day, month, year).

Q6. Write a Python program to insert and remove elements from a queue using a list.

Q7. Write a Python program to implement factorial using recursion.

Q8. Write a Python program to use binary search to find an element in a sorted list.

Q9. Write a Python program to create a set of unique cities visited and display them.

Q10. Write a Python program to manually traverse a linked list structure using class and objects.


Go Back Top