Semester I BCA Syllabus BCA101 T – Programming Principles Using Python
Semester I Paper BCA101 T Unit VI – Built-in data structures:
- Tuples
- Sets
- Dictionary
- Stacks and Queues;
- Sorting and Searching
Introduction:
In Python, data structures are essential tools for organizing and manipulating data efficiently. Understanding built-in data structures like tuples, sets, dictionaries, stacks, queues, sorting, and searching is crucial for any aspiring programmer. In this course, we’ll explore these data structures in detail, providing code examples and illustrations for practical usage.
Tuples
What is a Tuple?
A tuple is an ordered collection of elements, similar to a list. However, tuples are immutable, meaning their values cannot be changed after creation. They are defined using parentheses.
Example:
my_tuple = (1, 2, 3)
Tuple Operations
- Accessing elements
- Slicing
- Concatenation
- Tuple packing and unpacking
- Tuple comprehension (in Python 3.8+)
Code Example:
my_tuple = (1, 2, 3)
print(my_tuple[0]) # Accessing elements
print(my_tuple[1:3]) # Slicing
Sets
What is a Set?
A set is an unordered collection of unique elements. Sets are defined using curly braces {}
or the set()
constructor.
Example:
my_set = {1, 2, 3}
Set Operations
- Adding and removing elements
- Set operations (union, intersection, difference, etc.)
- Checking membership
- Set comprehension
Code Example:
my_set.add(4) # Adding an element
my_set.remove(2) # Removing an element
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2) # Union
intersection_set = set1.intersection(set2) # Intersection
Dictionary
What is a Dictionary?
A dictionary is an unordered collection of key-value pairs. Each key must be unique. Dictionaries are defined using curly braces {}
with key-value pairs separated by colons :
.
Example:
my_dict = {‘name’: ‘Marianna’, ‘age’: 45}
Dictionary Operations
- Accessing values by keys
- Adding, updating, and deleting key-value pairs
- Dictionary comprehension
print(my_dict[‘name’]) # Accessing values by key
my_dict[‘city’] = ‘London’ # Adding a new key-value pair
my_dict[‘age’] = 27 # Updating a value
del my_dict[‘age’] # Deleting a key-value pair
Stacks and Queues
Stacks
A stack is a data structure that follows the Last-In-First-Out (LIFO) principle. It’s like a stack of books where you can only add or remove the topmost book.
Operations:
- Push (add an item to the top)
- Pop (remove the top item)
- Peek (view the top item without removing it)
Code Example:
stack = []stack.append(1) # Push
stack.append(2)
top = stack.pop() # Pop
Queues
A queue follows the First-In-First-Out (FIFO) principle. It’s like people waiting in line.
Operations:
- Enqueue (add an item to the rear)
- Dequeue (remove an item from the front)
Code Example:
from collections import deque
queue = deque()
queue.append(1) # Enqueue
queue.append(2)
front = queue.popleft() # Dequeue
Sorting and Searching
Sorting
Sorting is arranging elements in a specific order, such as ascending or descending. Python provides built-in sorting functions.
Code Example:
my_list = [3, 1, 2]sorted_list = sorted(my_list) # Ascending order
sorted_list_desc = sorted(my_list, reverse=True) # Descending order
Searching
Searching involves finding a specific element within a data structure. Common search algorithms include linear search and binary search.
Code Example (Linear Search):
my_list = [3, 1, 2, 4, 5]element_to_find = 4
found = False
for item in my_list:
if item == element_to_find:
found = True
break
Code Example (Binary Search – Assumes a sorted list):
def binary_search(arr, target):
left, right = 0, len(arr) – 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid – 1
return -1
The above explanation gives you a solid foundation in built-in data structures in Python, essential for any BCA student aiming to become proficient in programming and data manipulation. Practice these concepts with real-world scenarios to enhance your skills further.