Python Operators & Collections

#Python Arithmetic Opertaors(+ - * / // % **)
x=5
y=3
x+y

Output:
8


x-y

Output:
2


x*y

Output:
15


x/y

Output:
1.6666666666666667


x//y

Output:
1


x%y

Output:
2


x**y

Output:
125


#Python Comparison Operators(< <= > >= == !=)
#In this context the output value must be a boolean value(True / False)
x<=y

Output:
False


x>=y

Output:
True


x==y

Output:
False


x!=y

Output:
True


#Python Logical Operators(and or not)
x+y>y and x!=y

Output:
True


x+y<y or x==y

Output:
False


x and y

Output:
3


x or y

Output:
5


not x+y<y

Output:
True


#Python Assignment Operator(=)
x1=200
print(x1)

Output:
200


x1+=50
print(x1)

Output:
250


#Python Bitwise Operators
x & y

Output:
1


x | y

Output:
7


x ^ y

Output:
6


~x

Output:
-6


x<<3

Output:
40


x>>2

Output:
1


#Python Identity Operators(is is not)
x1=100
x2=100
y1=[10,20,30,40,50]
y2=[10,20,30,40,50]
x1 is x2

Output:
True


y1 is y2

Output:
False


y1 is not y2

Output:
True


#Python Membership Operators(in not in)
s="Hello World!"
'h' in s

Output:
False


'h' not in s

Output:
True


'H' in s

Output:
True


#Python Empty List
x=[]
print(x)

[]

x=[100,200,12.34,'python',True,200,500]
print(x)

[100,200,12.34,'python',True,200,500]


#To get the length of List
len(x)

Output:
7


#To get individual data item
x[0]

Output:
100


x[1]

Output:
200


x[-1]

Output:
500


#To retrieve all data items from a List
for k in x:
print(k)

100
200
12.34
python
True
200
500

import sys
print(sys.getsizeof(x))

112

#To get all builtin functions
dir(x)

Output:
['__add__',
'__class__',
'__contains__',
'__delattr__',
'__delitem__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__gt__',
'__hash__',
'__iadd__',
'__imul__',
'__init__',
'__init_subclass__',
'__iter__',
'__le__',
'__len__',
'__lt__',
'__mul__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__reversed__',
'__rmul__',
'__setattr__',
'__setitem__',
'__sizeof__',
'__str__',
'__subclasshook__',
'append',
'clear',
'copy',
'count',
'extend',
'index',
'insert',
'pop',
'remove',
'reverse',
'sort']

help(x.append)

Help on built-in function append:

append(object, /) method of builtins.list instance
Append object to the end of the list.

x.append(600)
print(x)

[100, 200, 12.34, 'python', True, 200, 500, 600]

#To insert a data item at a specified position
x.insert(2,300)
print(x)

[100, 200, 300, 12.34, 'python', True, 200, 500, 600]

y=x.copy()
print(y)

[100, 200, 300, 12.34, 'python', True, 200, 500, 600]

y.clear()
print(y)

[]

x.count(200)

Output:
2


x.index(12.34)

Output:
3


#Remove a data item from a List
x.remove(200)
print(x)

[100, 300, 12.34, 'python', True, 200, 500, 600]


x.pop()
print(x)

[100, 300, 12.34, 'python', True, 200, 500]


x.reverse()
print(x)

[500, 200, True, 'python', 12.34, 300, 100]

x1=[30,20,10,50]
x1.sort()
print(x1)

[10, 20, 30, 50]

x2=[50,60,70]
x1.extend(x2)      #merging two List
print(x1)

[10, 20, 30, 50, 50, 60, 70]

#Slicing
print(x)

[500, 200, True, 'python', 12.34, 300, 100]

x[:4]

[500, 200, True, 'python']

x[3:]

['python', 12.34, 300, 100]

x[2:5]

[True, 'python', 12.34]

x[-5:-2]

[True, 'python', 12.34]

#Python Set
s1={100,200,12.34,True,'python',200,300,400}
print(s1)

{'python', True, 100, 200, 12.34, 300, 400}

#To get the length
len(s1)

Output:
7


#To get the size of s1
import sys
print(sys.getsizeof(s1))

728

#To retrieve all data items from Set
for k in s1:
print(k)

python
True
100
200
12.34
300
400

s2=set()     #s2:empty set
print(s2)

set()


#To get all builtin functions in set
dir(s2)

Output:
['__and__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__iand__', '__init__', '__init_subclass__', '__ior__', '__isub__', '__iter__', '__ixor__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update']


s2.add(200)
s2.add(300)
s2.add(4.5)
s2.add(300)
s2.add(False)
s2.add('python')
s2.add(500)

print(s2)

{False, 'python', 4.5, 200, 300, 500}

#Another approach to represent a set
s3=set([100,200,300,23.45,True,'Machine Learning',500,300])
print(s3)

{True, 100, 200, 300, 'Machine Learning', 500, 23.45}

s4=s3.copy()
print(s4)

{True, 'Machine Learning', 100, 500, 23.45, 200, 300}

s4.clear()
print(s4)

set()

#To remove a data item from a set
s3.remove('Machine Learning')
print(s3)

{True, 100, 200, 300, 500, 23.45}

s3.discard(23.45)
print(s3)

{True, 100, 200, 300, 500}

s3.remove(900)
print(s3)

-------------------------------------------------------------------------------------
KeyError                        Traceback (most recent call last)
<ipython-input-14-f24fb844f5e9> in <module>
----> 1 s3.remove(900)
        2 print(s3)

KeyError: 900

s3.discard(900)
print(s3)

{True, 100, 200, 300, 500}

s3.pop()
print(s3)

{100, 200, 300, 500}

s4=set([20,30,10,40])
s5=set([40,50,60])
s4.union(s5)

Output:
{10, 20, 30, 40, 50, 60}

s4.intersection(s5)

Output:
{40}


#Python Tuple
t1=(100,200,5.6,False,'Deep Learning',200,500)
print(t1)

(100, 200, 5.6, False, 'Deep Learning', 200, 500)

#To get the length of tuple
len(t1)

Output:
7


import sys
print(sys.getsizeof(t1))

96

t1[0]

Output:
100


t1[6]

Output:
500


t1[-1]

Output:
500


t1[-5]

Output:
5.6


#Retrieve all data items from tuple
for x in t1:
print(x)

100
200
5.6
False
Deep Learning
200
500


#Another approach to represent a tuple
t2=('a',)
t3=('a','aa')
t4=('a','aa','aaa')
print(t2)
print(t3)
print(t4)

('a',)
('a', 'aa')
('a', 'aa', 'aaa')


type(t2)

Output:
tuple


type(t4)

Output:
tuple


#Another approach to represent a tuple
t5=1,
t6=1,2
t7=1,2,3
print(t5)
print(t6)
print(t7)

(1,)
(1, 2)
(1, 2, 3)


#Tuple Assignment
#(regdno,name,bname)
s=(101,'Akshay','MCA')
regdno=s[0]
name=s[1]
bname=s[2]
print(regdno)
print(name)
print(bname)

101
Akshay
MCA


#To get all builtin functions in Tuple
dir(t1)

Output:
['__add__',
'__class__',
'__contains__',
'__delattr__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__getnewargs__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__iter__',
'__le__',
'__len__',
'__lt__',
'__mul__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__rmul__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'count',
'index']


t1.count(200)

Output:
2


t1.index(5.6)

Output:
2


#Python Dictionary
car_detail={'brand':'Maruti','model':'Breeza','year':2022}
print(car_detail)

{'brand': 'Maruti', 'model': 'Breeza', 'year': 2022}

#To get the length of dictionary
len(car_detail)

Output:
3

type(car_detail)

Output:
dict

#To access a data item
car_detail['model']

Output:
'Breeza'


car_detail.get('model')

Output:
'Breeza'


#To get all keys
car_detail.keys()

Output:
dict_keys(['brand', 'model', 'year'])


#To get all values
car_detail.values()

Output:
dict_values(['Maruti', 'Breeza', 2022])


#To get all pairs
car_detail.items()

Output:
dict_items([('brand', 'Maruti'), ('model', 'Breeza'), ('year', 2022)])


#Retrieve all pairs from a dictionary
for x,y in car_detail.items():
print(x,y)

brand Maruti
model Breeza
year 2022


#To update data item
car_detail['year']=2021
print(car_detail)

{'brand': 'Maruti', 'model': 'Breeza', 'year': 2021}


car_detail.update({'year':2020})
print(car_detail)

{'brand': 'Maruti', 'model': 'Breeza', 'year': 2020}


#Add a new pair
car_detail['color']='Red'
print(car_detail)

{'brand': 'Maruti', 'model': 'Breeza', 'year': 2020, 'color': 'Red'}


#To copy all pairs from one dictionary to another
car_info=car_detail.copy()
print(car_info)

{'brand': 'Maruti', 'model': 'Breeza', 'year': 2020, 'color': 'Red'}


#To release all pairs from a dictionary
car_info.clear()
print(car_info)

{}


#To remove a pair from a dictionary
car_detail.pop('year')
print(car_detail)

{'brand': 'Maruti', 'model': 'Breeza', 'color': 'Red'}


car_detail.popitem()
print(car_detail)

{'brand': 'Maruti', 'model': 'Breeza'}


#using del keyword we can remove a pair from a dictionary
del car_detail['model']
print(car_detail)

{'brand': 'Maruti'}


#to delete entire dictionary
del car_detail
print(car_detail)   #output : NameError: name 'car_detail' is not defined

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext