0% found this document useful (0 votes)
8 views4 pages

2.python Data Structures1

The document provides a comprehensive guide on data structures in Python, covering lists, tuples, sets, and dictionaries. It includes examples of operations such as indexing, slicing, adding, removing, and comprehensions. Additionally, it explains key concepts like immutability, membership operators, and dictionary methods.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

2.python Data Structures1

The document provides a comprehensive guide on data structures in Python, covering lists, tuples, sets, and dictionaries. It includes examples of operations such as indexing, slicing, adding, removing, and comprehensions. Additionally, it explains key concepts like immutability, membership operators, and dictionary methods.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

SANKALP STUDY SUCCESS

Way to Learn and Key to Success

Data Structures Codings


Lists

Q1. Create a list and perform indexing/slicing.


nums = [10, 20, 30, 40, 50]
print(nums[0]) # first element → 10
print(nums[-1]) # last element → 50
print(nums[:3]) # first 3 elements → [10,20,30]
print(nums[-2:]) # last 2 elements → [40,50]

Q2. Add, insert, remove, pop, delete, clear.


letters = ['a','b','c']
[Link]('d') # add
[Link](0,'z') # insert at index 0
[Link]('b') # remove value
[Link]() # remove last
del letters[1] # delete by index
[Link]() # clear all
print(letters) # []

Q3. Difference between copy vs reference.


l1 = [1,2,3]
l2 = l1 # reference (same memory)
l3 = [Link]() # shallow copy (new object)

l1[0] = 99
print(l1) # [99,2,3]
print(l2) # [99,2,3] → changes reflect
print(l3) # [1,2,3] → independent
Q4. Membership operator.
langs = ['c','java','python']
print('python' in langs) # True
print('go' not in langs) # True

Q5. Loop with enumerate.


langs = ['c','java','python']
for i, v in enumerate(langs):
print(i, v)
#0c
# 1 java
# 2 python

Q6. Sorting: ascending, descending, sort() vs sorted().


data = [9,5,2,99,12,88,34]

print(sorted(data)) # [2,5,9,12,34,88,99]
print(sorted(data, reverse=True)) # [99,88,34,12,9,5,2]

[Link]() # in-place ascending


print(data) # [2,5,9,12,34,88,99]

Q7. List comprehension.


evens = [i for i in range(20) if i % 2 == 0]
squares = [n*n for n in range(10)]
filtered = [i for i in range(200) if i%3==0 if i%9==0 if i%12==0]

print(evens) # [0,2,4,...,18]
print(squares) # [0,1,4,9,16,25,36,49,64,81]
print(filtered) # [0,36,72,108,144,180]

Q8. Extract digits and letters from a string.


s = "One 1 two 2 three 3 four 4 six 6789"

digits = [ch for ch in s if [Link]()]


letters = [ch for ch in s if [Link]()]
print(digits) # ['1','2','3','4','6','7','8','9']
print(letters) # ['O','n','e','t','w','o','t','h','r','e','e',...]
Tuples

Q9. Indexing and slicing.


t = ('one','two','three','four','five')
print(t[0]) # one
print(t[-1]) # five
print(t[1:4]) # ('two','three','four')

Q10. Immutability check.


t = (1,2,3)
# t[0] = 10 # ❌ Error → 'tuple' object does not support item assignment

Q11. Tuple methods: count() and index().


t = (1,2,3,2,4,2,5)
print([Link](2)) #3
print([Link](4)) #4

Sets
Q12. Set creation.
s = {1,2,3,4}
print(s) # {1,2,3,4}

Q13. Operations: union, intersection, difference, symmetric difference.


a = {1,2,3,4}
b = {3,4,5,6}
print(a | b) # {1,2,3,4,5,6}
print(a & b) # {3,4}
print(a - b) # {1,2}
print(a ^ b) # {1,2,5,6}

Q14. add(), remove(), discard(), pop(), clear().


s = {1,2,3}
[Link](4)
[Link](2)
[Link](10) # no error if not present
[Link]() # removes random element
[Link]() # empty set
print(s) # set()
Dictionaries

Q15. Dictionary creation.


student = {'id':101, 'name':'Alice', 'marks':90}
print(student)

Q16. Access values: [] vs get().


student = {'id':101, 'name':'Alice'}
print(student['id']) # 101
print([Link]('age')) # None (safe access)

Q17. Add new key, update value, delete key.


d = {'a':1, 'b':2}
d['c'] = 3 # add
d['a'] = 10 # update
del d['b'] # delete
print(d) # {'a':10, 'c':3}

Q18. Loop through keys, values, items.


d = {'x':10,'y':20,'z':30}

for k in d:
print(k) # keys

for v in [Link]():
print(v) # values

for k,v in [Link]():


print(k,v) # key-value pairs

Q19. Dictionary comprehension.


squares = {n:n*n for n in range(1,6)}
print(squares) # {1:1, 2:4, 3:9, 4:16, 5:25}

You might also like