different types of collections in python

different types of

collections in python

1.List
2.Tuple
3. Set
4. Dictionary

1.List:

List is a mutable datatype it stores the different types of values or item in one variable.
below is example it stores different types values like integer, float, string values
l=[1,2,3,"obula",1.23]
print our list values
l=[1,2,3,"obula",1.23]
print(l)
OUTPUT: [1,2,3,"obula",1.23]
 we can store any number of values there is no limit to store values in list
list follows indexing.
index starts from 0
adding two list by using ' + '
l2=[1,2,3,"obula",1.23]
print(l+l2)
OUTPUT: [1,2,3,"obula",1.23,1,2,3,"obula",1.23]

1.1 List slicing:
Slicing means here we can mention start ,end and step values
print(l2[1:4])
OUTPUT: [2,3,"obula"]
1.2 List updating:
l2[1]="python"
print(l2)
OUTPUT=[1,"python",3,"obula",1.23]
1.3 List appending:
l2.append("company")
print(l2)
OUTPUT=[1,"python",3,"obula",1.23,"company"]
1.4 List delete:
del l2[3]
print(l2)
OUTPUT=[1,"python",3,1.23,"company"]
1.5 List Max and Min:
l3=[10,576,12,776,872,5,23]
print(min(l3))
print(max(l3))
OUTPUT:
5
872
1.6 List length:
l3=[10,576,12,776,872,5,23]
print(len(l3))
OUTPUT: 7
1.7 Converting Tuple to List:
l4=(2,3,3,1,4)
print(list(l4))
OUTPUT: [2,3,3,1,4]
1.8 Find index position an element:
print(l3.index(12)
OUTPUT: 2
1.9 Find repeated number:
l5=[2,3,4,2,4,2,4]
print(l5.count(4)
OUTPUT: 3
1.10: Insert at particular position:
# insert(index,value)
print(l5.insert(2,10))
OUTPUT: [2,3,10,4,2,4,2,4]
1.11 List Reverse:
l5=[2,3,4,2,4,2,4]
print(l5.reverse())
OUTPUT: [4,2,4,2,4,3,2]
1.12:List Sort:
# ascending
l5=[2,3,4,2,4,2,4]
print(l5.sort())
OUTPUT: [2,2,2,3,4,4,4]
# descending
l5=[2,3,4,2,4,2,4]
l5=l5.sort()
print(l5.reverse())
OUTPUT: [4,4,4,3,2,2,2]

2.Tuple:

Tuple is one type of datatype available in python. Tuples are immutable that means we can change the values in tuple directly
' ( ) ' elements define inside parenthesis 
example:
2.1 Tuple Create:
t1=(2,3,42,3)
print(type(t1))
print(t1)
OUTPUT: 
tuple
(2,3,42,3)
index starting from zero
but we can modify or update tuple because of that it is immutable
if you want to define one element after first element ',' should present
t1=(2,)
print(t1)
OUTPUT: (2)
2.2:Updating tuple:
directly we can't add update by using index but we can add two tuples
t1=(2,3,4,2,3)
t2=(2,3,4)
print(t1+t2)
OUTPUT:(2,3,4,2,3,2,3,4)
2.3 deleting Tuple:
directly we can't delete elements from tuple
t2=(2,3,4)
del t2
t2 tuple deleted
2.4: accessing tuple elements:
by using slicing operator we can access
t2=(2,3,4)
print(t2[0:2])
OUTPUT: (2,3)
2.5 find length:
t2=(2,3,4)
print(len(t2))
OUTPUT:3
2.6 Tuple concatenation :
t1=(1,3)
t2=(2,3,4)
print(t1+t2)
OUTPUT:(1,3,2,3,4)
2.7: repeated elements:
t2=(2,3,4,4)
print(t2.count(4))
OUTPUT:2
2.8 Min and Max:
t2=(2,3,4,4)
print(max(t2))
print(min(t2))
OUTPUT: 
4
2
2.9 Index of element:
t2=(2,3,4,4)
print(t2.index(4))
OUTPUT: 2
2.10 Converting list to Tuple:
t2=[2,3,4,4]
print(tuple(t2))
OUTPUT:(2,3,4,4)

3.SET:

Set is also one type of collection in python but it will not store duplicate values
defined inside '{ }' .
on set we can't perform indexing and slicing
3.1 Create Set:
s1={1,2,3}
print(s1)
OUTPUT:{1,2,3}
s1={1,2,3,1,2,3,3}
print(s1)
OUTPUT: {1,2,3}
3.2 Add:
s1={1,2,3}
s1.add(4)
print(s1)
OUTPUT:{1,2,3,4}
3.3 Update:
s1={1,2,3}
s2={4,5}
s1.update(s2)
print(s1)
print(s2)
OUTPUT:
{1,2,3,4,5}
{4,5}
3.4 len:
s1={1,2,3}
print(len(s1))
OUTPUT: 3
3.5 remove:
s1={1,2,3}
s1=s1.remove(2)
print(s1)
OUTPUT:{1,3}
s1={1,2,3}
s1=s1.remove(8)
print(s1)
OUTPUT: key error due to value not present . we can overcome this error by discard
3.6 discard:
s1={1,2,3}
s1=s1.remove(8)
print(s1)
OUTPUT: {1,2,3} 
it will through any error
3.7 pop:
first element will take out first element
s1={1,2,3}
print(s1.pop())
OUTPUT: 1
3.8 clear:
s1={1,2,3}
print(s1.clear())
OUTPUT: {}
3.9 max and min:
s1={1,2,3}
print(max(s1))
OUTPUT: 3
s1={1,2,3}
print(min(s1))
OUTPUT: 1
3.10 sum:
s1={1,2,3}
print(sum(s1))
OUTPUT: 6
3.11 issubset:
s1={1,2,3}
s2={1,2,3,4,5,6}
print(s1.issubset(s2))
OUTPUT: True
3.12 issuperset:
s1={1,2,3}
s2={1,2,3,4,5,6}
print(s1.issuperset(s2))
OUTPUT: False
3.13 Union:
s1={1,2,3,7}
s2={1,2,3,4,5,6}
print(s1.union(s2))
OUTPUT: {1,2,3,4,5,6,7}
3.14 Intersection:
s1={1,2,3,7}
s2={1,2,3,4,5,6}
print(s1.intersection(s2))
OUTPUT:{1,2,3}
3.15 Difference:
s1={1,2,3,7}
s2={1,2,3,4,5,6}
print(s1.difference(s2))
OUTPUT:{7}
s1={1,2,3,7}
s2={1,2,3,4,5,6}
print(s2.difference(s1))
OUTPUT:{4,5,6}
3.16 Symmetric_difference:
s1={1,2,3,7}
s2={1,2,3,4,5,6}
print(s1.symmetric_difference(s2))
OUTPUT:{7,4,5,6}

4.Dictionary:

it is also one type of collection in python here stored in key value pair .
4.1 Creation:
d1={"name":"obul","company":"xyz","2":400}
print(d1)
OUTPUT: {"name":"obul","company":"xyz","2":400}
4.2 Accessing:
d1={"name":"obul","company":"xyz","2":400}
print(d1["name"])
OUTPUT: obul
4.3 Adding one more element:
d1={"name":"obul","company":"xyz","2":400}
d1["sal"]=10000
print(d1)
OUTPUT:{"name":"obul","company":"xyz","2":400,"sal":10000}
4.4 delete:
d1={"name":"obul","company":"xyz","2":400}
del d1["2"]
print(d1)
OUTPUT:{"name":"obul","company":"xyz"}
4.5 clear:
d1={"name":"obul","company":"xyz","2":400}
d1.clear()
print(d1)
OUTPUT:{ }


Comments