Common Operations¶
There are some functions which can be applied on all collections. Here we will see details related to list
and set
.* in
– check if element exists
len
– to get the number of elements.sorted
– to sort the data (original collection will be untouched). Typically, we assign the result of sorting to a new collection.sum
,min
,max
, etc – arithmetic operations.- There can be more such functions.
In [1]:
l = [1, 2, 3, 4] # list
In [2]:
1 in l
Out[2]:
True
In [3]:
5 in l
Out[3]:
False
In [4]:
len(l)
Out[4]:
4
In [5]:
sorted(l)
Out[5]:
[1, 2, 3, 4]
In [6]:
sum(l)
Out[6]:
10
In [7]:
s = {1, 2, 3, 4} # set
In [8]:
1 in s
Out[8]:
True
In [9]:
5 in s
Out[9]:
False
In [10]:
len(s)
Out[10]:
4
In [11]:
sorted(s)
Out[11]:
[1, 2, 3, 4]
In [12]:
sum(s)
Out[12]:
10