0% found this document useful (0 votes)
4 views

DSA Python Examples for Data Science

The document provides examples of various data structures and algorithms in Python, including lists, tuples, sets, dictionaries, NumPy arrays, searching algorithms, sorting techniques, and implementations of stacks and queues. Each section contains code snippets demonstrating how to create, manipulate, and perform operations on these data structures. The examples are aimed at aiding understanding for data science applications.

Uploaded by

jagrutipendor9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

DSA Python Examples for Data Science

The document provides examples of various data structures and algorithms in Python, including lists, tuples, sets, dictionaries, NumPy arrays, searching algorithms, sorting techniques, and implementations of stacks and queues. Each section contains code snippets demonstrating how to create, manipulate, and perform operations on these data structures. The examples are aimed at aiding understanding for data science applications.

Uploaded by

jagrutipendor9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

DSA Python Examples for Data Science

1. Lists (Examples)

**Create and manipulate lists**

data = [1, 2, 3]

data.append(4) # [1, 2, 3, 4]

data.remove(2) # [1, 3, 4]

data[1] = 10 # [1, 10, 4]

print(data[:2]) # [1, 10]

**List comprehensions**

squares = [x**2 for x in range(5)] # [0, 1, 4, 9, 16]

**Nested Lists**

matrix = [[1, 2], [3, 4]]

print(matrix[1][0]) #3

2. Tuples (Examples)

**Creating tuples**

t = (1, 2, 3)

print(t[1]) #2

**Tuple unpacking**

a, b = (10, 20)

print(a, b) # 10 20

**Returning multiple values**


def stats(x, y):

return (x+y, x*y)

print(stats(3, 4)) # (7, 12)

3. Sets (Examples)

**Removing duplicates**

nums = [1, 2, 2, 3]

unique = list(set(nums)) # [1, 2, 3]

**Set operations**

a = {1, 2, 3}

b = {3, 4, 5}

print(a | b) # Union: {1, 2, 3, 4, 5}

print(a & b) # Intersection: {3}

print(a - b) # Difference: {1, 2}

4. Dictionaries (Examples)

**Create dictionary**

info = {"name": "Jaya", "age": 25}

print(info["name"]) # Jaya

**Add/update entry**

info["city"] = "Nagpur"

**Iterating through dictionary**

for k, v in info.items():

print(k, v)
5. NumPy Arrays (Examples)

import numpy as np

**1D array**

arr = np.array([1, 2, 3])

print(arr.mean()) # 2.0

**2D array and indexing**

mat = np.array([[1, 2], [3, 4]])

print(mat[0][1]) #2

**Broadcasting**

print(arr + 2) # [3 4 5]

**Matrix multiplication**

A = np.array([[1, 2], [3, 4]])

B = np.array([[2, 0], [1, 2]])

print(np.dot(A, B))

6. Searching Examples

**Linear Search**

def linear_search(lst, val):

for item in lst:

if item == val:

return True

return False
**Binary Search (on sorted list)**

def binary_search(lst, val):

low, high = 0, len(lst) - 1

while low <= high:

mid = (low + high) // 2

if lst[mid] == val:

return True

elif lst[mid] < val:

low = mid + 1

else:

high = mid - 1

return False

7. Sorting Examples

**Basic sort**

nums = [5, 3, 6, 2]

nums.sort() # [2, 3, 5, 6]

**Custom sort**

pairs = [(1, 'b'), (2, 'a'), (3, 'c')]

pairs.sort(key=lambda x: x[1]) # sort by second value

**Using sorted()**

sorted_nums = sorted(nums, reverse=True)

8. Stacks & Queues (via list & deque)

**Stack using list**

stack = []
stack.append(1)

stack.append(2)

print(stack.pop()) #2

**Queue using deque**

from collections import deque

queue = deque([1, 2, 3])

queue.append(4)

print(queue.popleft()) #1

You might also like