



























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




























Data structure are specific methods of organizing, managing and storing data
efficiently in a computer so that operations can be performed effectively.
Built-in data structure :
❖ List
❖ tuple
❖ set
❖ dictionary
There are TWO types of data structure in Python:
✓ Built-in
✓ User-defined/ Advanced Data structures
User-defined/Advance data structure:
✓ stack
✓ Queue
✓ Link list
✓ Tree and Graph
It represented by the group of characters.
Example: str = “Hello”
There are THREE operations that can generally perform on any sequence.
1. Indexing: It means retrieving any element by its position number.
Example:
s = “HELLO”
Index: 0 1 2 3 4
Char:
Negative index: - 5 - 4 - 3 - 2 - 1
✓ Positive Index starts form 0(left to right)
✓ Negative index starts form - 1(right to left)
✓ Access using s [ index ]
2. Slicing: ❖ It means retrieving a range (or piece) of elements.
❖ Extract part of string using s[start: stop] / s[start: stop: step]
Example:
s = “PYTHON”
Index: 0 1 2 3 4 5
s:
Negative index:- 6 - 5 - 4 - 3 - 2 - 1
s = “PYTHON”
print(s[0:6]) # PYTHON (full string)
print(s[0:3]) # PYT (start 3-1)
print(s[1:5:2]) # YT (step 2)
print(s[::-1]) # NOHTYP (reverse string)
3. Repetition: ❖ It means repeating a sequence.
Example:
s = “PYTHON”
s*2 #PYTHONPYTHON
String Operation Operation Example
len() Finds the length of a string str = “hello”
len(str) #
upper() Converts a string into capital letter str.upper # HELLO
Lower() Converts a string into small letter str.lower() #hello
A list with different types of items
items = [1, 2, 'N', 'Go', 3.14]
❖ A collection of similar or different types of data items.
❖ A list is created by placing items inside [ ] separated by commas.
Example
A list with duplicate items
numbers = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
items:
index: 0 1 2 3 4 5 6 7 8 9
1 2 N Go 3.
number:
index:
➢ It is a list where elements are listed one after the other.
➢ Each element is allocated a unique number called index.
Examples: A list containing multiple of 5 up to 20
my_list = [5, 10, 15, 20]
my_list[0] = 5
my_list[2] = 15
my_list:
0 1 2 3
my_list[-1] = 20
my_list[-4] = 5
- 4 - 3 - 2 - 1
Positive Indexing
Negative Indexing
➢ Element can be added to a list in different ways, following are the methods to
add element to a list.
✓ append()
✓ insert()
✓ extend()
SYNTAX: list.append( value )
Example: Language = ['c', 'cpp', 'Java’]
Language.append('Python’)
=> ['c', 'cpp', 'Java', 'Python']
Example: Language = ['c', 'cpp', 'Java’]
Language.append([‘Python’,’JavaScript’])
=> ['c', 'cpp', 'Java', [‘Python’,’JavaScript’]]
A list as an item can be added
SYNTAX: list.insert(position, value)
Example: Language = ['c', 'cpp', 'Java’]
index: 0 1 2
Language.insert(0,'Python’)
=> ['Python','c', 'cpp', 'Java']
SYNTAX: (^) list1.extend(list2)
Example: Languages = ['c', 'cpp', 'Java’]
more_language = ['Python', 'Javascript’]
Languages.extend(more_language)
=> ['c', 'cpp', 'Java', 'Python', 'Javascript']
Example: a = [1, 2, 3]
del a[1]
print(a) => [1, 3]
Removes all item.
Example: a = [1, 2, 3]
a.clear()
print(a) => []
List Function Operation Example
len() Finds length of a list
lst = [1, 2, 3]
len(lst) → 3
index()
Returns the index of the first
occurrence of a value
lst = [10, 20, 30]
lst.index(20) → 1
count()
Counts how many times a value
appears
lst = [1, 2, 2, 3]
lst.count(2) → 2
sort()
Sorts the list in ascending order
(default)
lst = [3, 1, 2]
lst.sort() → [1, 2, 3]
reverse() Reverses the order of the list
lst = [1, 2, 3]
lst.reverse() → [3, 2, 1]
copy() Returns a shallow copy of the list
lst = [1, 2, 3]
new_lst = lst.copy() → [1, 2, 3]
➢ Each item of a tuple can be accessed through its index.
➢ Trying to access item out of the index range will result an IndexError.
t[0] = 1
t[3] = hello
my_list[-1] = 20
my_list[-4] = 5
Positive Indexing
Negative Indexing
t^1 2 3 Hello
Examples:
t = (1, 2, 3, "Hello")
index:
t^1 2 3 Hello
index:
tuple_name[index]
Length of tuples
❖ Can be determined using the len() function.
Examples: t = (1, 2, 3, "Hello")
len(t) # 4
➢ Not possible to directly add items to a tuple as tuple are immutable.
➢ Workaround: convert a tuple into a list.
Adding Items to a Tuple
Examples:
t = (1, 2, 3, "Hello")
temp = list(t)
temp.append(‘hi’)
t = tuple(temp)
t = (1, 2, 3, "Hello“,’hi”)
➢ Not possible to directly remove items of a tuple as tuple are immutable.
➢ Workaround: convert a tuple into a list.
Removing Item of a Tuple
Examples:
t = (1, 2, 3, "Hello")
temp = list(t)
temp.remove( ‘Hello’)
print(t) # [1, 2, 3]
t = tuple(temp)
t = (1, 2, 3)
➢ Remove the entire tuple using del keyword.
Examples:
t = (1, 2, 3, "Hello")
del t
Tuple Function Operation Example
len() Finds length of a tuple
t = (1, 2, 3)
len(t) → 3
index()
Returns the index of the
first occurrence of a value
t = (10, 20, 30)
t.index(20) → 1
count()
Counts how many times a
value appears
t = (1, 2, 2, 3)
t.count(2) → 2
slicing Extracts part of the tuple
t = (1, 2, 3, 4)
t[1:3] → (2, 3)
immutability
Tuples cannot be changed
(no append/remove)
t = (1, 2, 3) → cannot
modify directly