a = [1,2,3,4,5]
a[3] = 20
a
[1, 2, 3, 20, 5]
a = (1,2,3,4,5)
a[3] = 20
a
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-3-89b1b6bece21> in <module>() 1 a = (1,2,3,4,5) ----> 2 a[3] = 20 3 a TypeError: 'tuple' object does not support item assignment
Test whether an item is present in the collection on not:
True
if presentFalse
in not present't' in 'Python'
True
3 in [1,2,3]
True
[1,2] in ['abc',[1,2],'xyz']
True
[1,2] in ['abc',[1,2, 3],'xyz']
False
'abc' + '123'
'abc123'
['a', 'c'] + [1,2]
['a', 'c', 1, 2]
(1,2,3) + (4,'five',6)
(1, 2, 3, 4, 'five', 6)
'atul' + ['1','2']
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-11-308775c8301d> in <module>() ----> 1 'atul' + ['1','2'] TypeError: cannot concatenate 'str' and 'list' objects
*
operator is overridden on ordered collection types to interpret as 'times'
'Hello ' * 5
'Hello Hello Hello Hello Hello '
[1,2,3] * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
(6,'seven',7) * 2
(6, 'seven', 7, 6, 'seven', 7)
lang = 'Python'
lang[1], lang[-1]
('y', 'n')
even = [2,4,6,8,10]
even[3]
8
row = ('123', 'Python', 'Active', '12/31/1999')
row[0], row[-2]
('123', 'Active')
slicing is similar to substring operation on sequences / ordered collections
seq[ start : finish-1 : stride ]
start
is not provided, default is 0finish
is not provided, default is -1stride
is hop and defaulted to 0a = range(10)
a[:]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
a[3:7]
[3, 4, 5, 6]
a[:7:2]
[0, 2, 4, 6]
a[-1:-5]
[]
a[-5:-1]
[5, 6, 7, 8]
a[-1:-5:-1]
[9, 8, 7, 6]
returns the length of the sequence in terms of number of immediate children
a = [1,2,3,4,5]
len(a)
5
b = tuple()
b
()
len(b)
0
a = [1,2,[3,4],5]
len(a)
4
__lt__
needs to be defined on that classb = [3,5,3,5,7,4,2,8,3,2,4]
max(b), min(b)
(8, 2)
max('Python')
'y'
langs = ['Python', 'Scala', 'Java', 'C++']
max(langs), min(langs)
('Scala', 'C++')
Can sort sequence in ascending (default) or descending order. For non basic types, __lt__
needs to be defined, or key needs to be provided
sorted(lang)
['P', 'h', 'n', 'o', 't', 'y']
sorted(lang, reverse=True)
['y', 't', 'o', 'n', 'h', 'P']
sorted(langs)
['C++', 'Java', 'Python', 'Scala']
In above scenarios, Unordered collections viz. Sets (set
) and Dictionaries (dict
) are useful
{}
s = {1,2,3,4,5,3,2,4,6,72,4,6,3,2,46,'OK',2,2,4}
s
{1, 2, 3, 4, 5, 6, 46, 72, 'OK'}
# Below is a dict
es = {}
type(es)
dict
# this is how to initialize an empty set
es = set()
type(es)
set
s.add('tomato')
s
{1, 2, 3, 4, 5, 6, 46, 72, 'OK', 'tomato'}
s.remove(72)
s
{1, 2, 3, 4, 5, 6, 46, 'OK', 'tomato'}
s.add(2)
s
{1, 2, 3, 4, 5, 6, 46, 'OK', 'tomato'}
a = {(1,2,'something'):'Amazing', 'id#': 342342432, 'id#':3}
a
{'id#': 3, (1, 2, 'something'): 'Amazing'}
a['id#']
3
a[(1,2, 'something')]
'Amazing'
a[0]
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-44-5ccf417d7af1> in <module>() ----> 1 a[0] KeyError: 0
'id#' in a
True
if 'id#' in a:
print 'Dups!!'
else:
a['id#'] = 'Spmething else'
a
Dups!!
{'id#': 3, (1, 2, 'something'): 'Amazing'}
for ele in coll:
# do something
for x in 'Python':
print x
P y t h o n
for i,l in enumerate(langs, start=1):
print i,l + '!'
1 Python! 2 Scala! 3 Java! 4 C++!
for i in range(len(langs)):
print i, langs[i] + '!'
0 Python! 1 Scala! 2 Java! 3 C++!
for i in a:
print a[i]
Amazing 3
l = ['c++', 'Java', 'Python', 'Scala']
sorted(l)
['Java', 'Python', 'Scala', 'c++']