Operator Functions in Python | Set 2

File handling | Python Methods and Functions

Additional features are discussed in this article.

1. setitem (ob, pos, val) : — This function is used to assign a value at a specific position in a container.
Operation — ob [pos] = val

2. delitem (ob, pos) : — This function is used to remove a value at a specific position in a container.
Operation — del ob [pos]

3. getitem (ob, pos) : — This function is used to access to a value at a specific position in a container.
Operation — Ob [pos]

# Python code to demonstrate how it works
# setitem (), delitem () and getitem ()

  
# import operator module

import operator

 
# Initializing list

li = [ 1 , 5 , 6 , 7 , 8 ]

 
# print the original list

print ( "The original list is:" , end = "")

for i in range ( 0 , len (li)):

print (li [i], end = " " )

  

print ( "" )

  
# using setitem () to assign 3 in 4th position

operator.setitem (li, 3 , 3 )

 
# print the modified list after setitem ()

print ( "The modified list after setitem () is: " , end = " ")

for i in range ( 0 , len (li)):

print (li [i], end = "" )

  

print ( "" )

 
# using delitem () to remove the value in the second index

operator .delitem (li, 1 )

  
# print the modified list after delitem ()

print ( "The modified list after delitem () is:" , end = "")

for i in range ( 0 , len (li)):

print (li [i], end = "" )

 

print ( "" )

 
# using getitem () to access to 4th element

print ( "The 4th element of list is:" , end = " ")

print (operator.getitem (li, 3 ))

Output:

 The original list is: 1 5 6 7 8 The modified list after setitem () is: 1 5 6 3 8 The modified list after delitem () is: 1 6 3 8 The 4th element of list is: 8 

4. setitem (ob, slice (a, b), vals) : — This function is used to set values ​​within a specific range in a container.
Operation — obj [a: b] = vals

5. delitem (ob, slice (a, b)) : — this function is used to remove values ​​from a specific range in a container.
Operation — del obj [a: b]

6. getitem (ob, slice (a, b)) : — this function is used to access values ​​within a specific range in a container.
Operation — object [a: b]

# Python code to demonstrate how it works
# setitem (), delitem () and getitem ()

 
# import operator module

import operator

 
# Initializing list

li = [ 1 , 5 , 6 , 7 , 8 ]

  
# print original list

print ( "The original list is:" , end = "")

for i in range ( 0 , len (li)):

print (li [i], end = "" )

 

print ( " " )

 
# using setitem () to assign 2,3,4 to the 2nd, 3rd and 4th index

operator.setitem (li, slice ( 1 , 4 ), [ 2 , 3 , 4 ])

  
# print the modified list after setitem ()

print ( " The modified list after setitem () is: " , end = " ")

for i in range ( 0 , len (li)):

print (li [i], end = "" )

 

print ( "" )

 
# using delitem () to remove a value in 3rd and 4th indices

operator.delitem (li, slice ( 2 , 4 ))

 
# prints the modified list after delitem ()

print ( "The modified list after delitem () is:" , end = "")

for i in range ( 0 , len (li)):

print (li [i], end = "" )

 

print ( " " )

  
# using getitem () to access 1st and 2nd elements

print ( "The 1st and 2nd element of list is:" , end = "")

print (operator.getitem (li, slice ( 0 , 2 )))

Output:

 The original list is: 1 5 6 7 8 The modified list after setitem () is: 1 2 3 4 8 The modified list after delitem () is: 1 2 8 The 1st and 2nd element of list is: [1, 2] 

7. concat (ob1, obj2) : — This function is used to merge two containers.
Operation — obj1 + obj2

8. contains (ob1, obj2) : — This function is used to check for obj2 in obj1 .
Operation — obj2 in obj1

# Python code to demonstrate how it works
# concat () and contains ()

 
# import operator module

import operator

 
# Initializing line 1

s1 = "geeksfor"

  
# Initializing line 2

s2 = "geeks"

 
# using concat () to concatenate two lines

print ( "The concatenated string is: " , end = " ")

print (operator.concat (s1, s2))

 
# using contains () to check if s1 s2 contains

if (operator.contains (s1, s2)):

  print ( "geeksfor contains geeks" )

else : print ( " geeksfor does not contain geeks " )

Output:

 The concatenated string is: pythonengineering geeksfor contains geeks 

9. and_ (a, b) : — This function is used to evaluate the bitwise and referenced arguments.
Operation — a & amp; b

10. or_ (a, b) : — This function is used to evaluate the bitwise or of the mentioned arguments.
Operation — a | b

11. xor (a, b) : — This function is used to compute the bitwise xor of the arguments mentioned.
Operation — a ^ b

12. invert (s) : — This function is used to calculate the bitwise inversion of the mentioned argument.
Operation — ~ a

# Python code to demonstrate how it works
# and_ (), or_ (), xor (), invert ()

 
# import operator module

import operator

 
# Initializing a and b

 

a = 1

 

b = 0

 
# using and_ () to display bitmap and operation

print ( "The bitwise and of a and b is: " , end = " ")

print (operator.and_ (a, b))

 
# using or_ () to display a bitmap or operation

print ( "The bitwise or of a and b is:" , end = "")

print (operator.or_ (a, b))

  
# using xor () to display a bitwise exception or operation

print ( "The bitwise xor of a and b is:" , end = "")

print (operator.xor (a, b))

 
# using invert () to invert the value
operator.invert (a)

  
# print modified value

print ( " The inverted value of a is: " , end = "")

print (a)

Output:

 The bitwise and of a and b is: 0 The bitwise or of a and b is: 1 The bitwise xor of a and b is: 1 The inverted value of a is: 1 

This article courtesy of Manjit Singh . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.





Get Solution for free from DataCamp guru