UNIT 3 (Part 1) Data Structures
UNIT 3 (Part 1) Data Structures
Python
Data Structures
Syllabus
List and List Operations
Using Lists to represent Matrices
Strings, String operations,
Tuples
Dictionaries
Sets
Iterators and generators
comprehensions.
Lists:
Ø List is a data structure and is a collection of ordered & Mutable(changeable)
elements.
Ø List allows duplicate members.
Ø A list is created by placing all the items (elements) inside a square bracket [], separated
by commas.
Ø List is dynamic. They grow and shrink on demand.
Ø It can have elements of same or different data types #List of integers
a=[1,2,3]
a=[] a=[1,2,3] print(a)
print(a) print(a)
print(*a) #List with mixed datatypes
a=[1,”hai”,3.4]
print(a)
Basic List operations
2.Repetition:
1.concatenation:
Syntax: list*int-value
Syntax: listname1+listname2
l=[1,2,3]
l1=[1,2,3] print(l*3)
l2=[4,5,6]
print(l1+l2) o/p:[1,2,3,1,2,3,1,2,3]
o/p:[1,2,3,4,5,6]
5.Iteration:
Syntax: for i in list:
for i in [1,2,3]:
print(i)
7.slicing: -7 -6 -5 -4 -3 -2 -1
t = [‘a’,’b’,’c’,’d’,’e’,’f’,’g’] t = [‘a’,’b’,’c’,’d’,’e’,’f’,’g’]
print(t) 0 1 2 3 4 5 6
print(t[1])
print(t[1:3]) ['a', 'b', 'c', 'd', 'e', 'f', 'g']
b
print(t[1:-1]) ['b', 'c']
print(t[:3]) ['b', 'c', 'd', 'e', 'f']
['a', 'b', 'c']
print(t[2:]) ['c', 'd', 'e', 'f', 'g']
print(t[:-1]) ['a', 'b', 'c', 'd', 'e', 'f']
['b', 'd', 'f']
print(t[1::2]) ['g', 'f', 'e', 'd', 'c', 'b', 'a']
print(t[::-1])
List:
list methods:
1.append()
2.insert()
3.sort()
4.count()
5.index()
6.pop()
7.remove()
8.reverse()
9.extend()
10.copy()
1.append():
o/p:[12,14,13] l=[1,3,5,7,8,3]
l.remove(3)
l=[12,14,13,10] print(l)
print(l.pop(1))
print(l) o/p:[1,5,7,8,3]
o/p:[12,13,10]
8.reverse(): 9.extend():
for i in (1,2,3):
print(i)
7.slicing: -7 -6 -5 -4 -3 -2 -1
t = (‘a’,’b’,’c’,’d’,’e’,’f’,’g’) t = (‘a’,’b’,’c’,’d’,’e’,’f’,’g’)
print(t) 0 1 2 3 4 5 6
print(t[1])
print(t[1:3])
print(t[1:-1])
print(t[:3])
print(t[2:])
print(t[:-1])
print(t[1::2])
print(t[::-1])
Methods:
Tuple does not support remove(),pop(),append(),sort(),reverse(),insert()
,extend()
2) count():
1) index():
syntax:
Syntax: tuplename.index(element) tuplename.count(element)
t=(1,2,3,4,7,10,13,11,12) t=(1,2,3,3,4,5)
print(t.index(13)) print(t.count(3))
3)copy: 4)zip():
t1=(1,2,3) t1=(1,2,3)
t2=t1 t2=(5,7,8,9,10)
print(t2) t3=tuple(zip(t1,t2))
print(t3)
l1=[1,2,3]
l2=[5,7,8,9,10]
l3=list(zip(l1,l2))
print(t3)
Sets:
set is unordered collection of elements and it is mutable
Every element is unique and must be immutable(Numbers,Strings,tuples)
Represented by dict() or {}
Keys in dictionary are unique and they are immutable(strings,numbers,tuples)
7)pop():
Syntax: dictname.pop(key)
f={1:’app’,2:’man’,3:’stra’}
f.pop(1)
print(f)
#output: {2:’man’,3:’stra’}
Strings
Multiline Strings:
Strings are immutable
a=””” python is
Invented by
Van Rossum”””
print(a)
String Operations: Repetition:
Index:
s1=’python’
Concatenation: a=’Hello python’
print(s1*3)
s1=’python’ print(a[9])
s2=’program’ print(a[-5])
s = ‘abcdefg’
print(s1+s2)
print(s)
Slicing: print(s[-1])
print(s[1:3])
print(s[1:-1])
print(s[:3])
print(s[2:])
print(s[:-1])
print(s[1::2])
print(s[::-1])
Len:
Membership
s=’string’ Iteration:
print(len(s))
s=’python’
s=’python’
print(‘y’ in s)
for i in s: print(‘a’ in s)
print(i) print(‘b’ not in s)
Methods:
2 1
[1,4,9,16…...100]
s=[x*x for x in range(1,11)]
print(s)
Range: 1 to 10
print(x)
Dict Comprehensions
print(x)