Operator Functions in Python | Set 2
Last Updated :
15 Jul, 2022
Operator Functions in Python | Set 1
More functions are discussed in this article.
1. setitem(ob, pos, val) :- This function is used to assign the value at a particular position in the container.
Operation - ob[pos] = val
2. delitem(ob, pos) :- This function is used to delete the value at a particular position in the container.
Operation - del ob[pos]
3. getitem(ob, pos) :- This function is used to access the value at a particular position in the container.
Operation - ob[pos]
Python3
# Python code to demonstrate working of
# setitem(), delitem() and getitem()
# importing operator module
import operator
# Initializing list
li = [1, 5, 6, 7, 8]
# printing original list
print ("The original list is : ",end="")
for i in range(0,len(li)):
print (li[i],end=" ")
print ("\r")
# using setitem() to assign 3 at 4th position
operator.setitem(li,3,3)
# printing modified list after setitem()
print ("The modified list after setitem() is : ",end="")
for i in range(0,len(li)):
print (li[i],end=" ")
print ("\r")
# using delitem() to delete value at 2nd index
operator.delitem(li,1)
# printing modified list after delitem()
print ("The modified list after delitem() is : ",end="")
for i in range(0,len(li)):
print (li[i],end=" ")
print ("\r")
# using getitem() to access 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 the values in a particular range in the container.
Operation - obj[a:b] = vals
5. delitem(ob, slice(a,b)) :- This function is used to delete the values from a particular range in the container.
Operation - del obj[a:b]
6. getitem(ob, slice(a,b)) :- This function is used to access the values in a particular range in the container.
Operation - obj[a:b]
Python3
# Python code to demonstrate working of
# setitem(), delitem() and getitem()
# importing operator module
import operator
# Initializing list
li = [1, 5, 6, 7, 8]
# printing original list
print ("The original list is : ",end="")
for i in range(0,len(li)):
print (li[i],end=" ")
print ("\r")
# using setitem() to assign 2,3,4 at 2nd,3rd and 4th index
operator.setitem(li,slice(1,4),[2,3,4])
# printing modified list after setitem()
print ("The modified list after setitem() is : ",end="")
for i in range(0,len(li)):
print (li[i],end=" ")
print ("\r")
# using delitem() to delete value at 3rd and 4th index
operator.delitem(li,slice(2,4))
# printing modified list after delitem()
print ("The modified list after delitem() is : ",end="")
for i in range(0,len(li)):
print (li[i],end=" ")
print ("\r")
# using getitem() to access 1st and 2nd element
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(obj1,obj2) :- This function is used to concatenate two containers.
Operation - obj1 + obj2
8. contains(obj1,obj2) :- This function is used to check if obj2 in present in obj1.
Operation - obj2 in obj1
Python3
# Python code to demonstrate working of
# concat() and contains()
# importing operator module
import operator
# Initializing string 1
s1 = "geeksfor"
# Initializing string 2
s2 = "geeks"
# using concat() to concatenate two strings
print ("The concatenated string is : ",end="")
print (operator.concat(s1,s2))
# using contains() to check if s1 contains s2
if (operator.contains(s1,s2)):
print ("geeksfor contains geeks")
else : print ("geeksfor does not contain geeks")
Output:
The concatenated string is : geeksforgeeks
geeksfor contains geeks
9. and_(a,b) :- This function is used to compute bitwise and of the mentioned arguments.
Operation - a & b
10. or_(a,b) :- This function is used to compute bitwise or of the mentioned arguments.
Operation - a | b
11. xor(a,b) :- This function is used to compute bitwise xor of the mentioned arguments.
Operation - a ^ b
12. invert(a) :- This function is used to compute bitwise inversion of the mentioned argument.
Operation - ~ a
Python3
# Python code to demonstrate working of
# and_(), or_(), xor(), invert()
# importing operator module
import operator
# Initializing a and b
a = 1
b = 0
# using and_() to display bitwise and operation
print ("The bitwise and of a and b is : ",end="")
print (operator.and_(a,b))
# using or_() to display bitwise or operation
print ("The bitwise or of a and b is : ",end="")
print (operator.or_(a,b))
# using xor() to display bitwise exclusive or operation
print ("The bitwise xor of a and b is : ",end="")
print (operator.xor(a,b))
# using invert() to invert value of a
operator.invert(a)
# printing modified value
print ("The inverted value of a is : ",end="")
print (operator.invert(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 : -2
Similar Reads
Operator Functions in Python | Set 1 Python has predefined functions for many mathematical, logical, relational, bitwise etc operations under the module "operator". Some of the basic functions are covered in this article. 1. add(a, b) :- This function returns addition of the given arguments. Operation - a + b. 2. sub(a, b) :- This func
5 min read
set() Function in python set() function in Python is used to create a set, which is an unordered collection of unique elements. Sets are mutable, meaning elements can be added or removed after creation. However, all elements inside a set must be immutable, such as numbers, strings or tuples. The set() function can take an i
3 min read
Comparison Operators in Python Python operators can be used with various data types, including numbers, strings, boolean and more. In Python, comparison operators are used to compare the values of two operands (elements being compared). When comparing strings, the comparison is based on the alphabetical order of their characters
4 min read
Intersection() function Python Python set intersection() method returns a new set with an element that is common to all set The intersection of two given sets is the largest set, which contains all the elements that are common to both sets. The intersection of two given sets A and B is a set which consists of all the elements whi
2 min read
Iterate over a set in Python The goal is to iterate over a set in Python. Since sets are unordered, the order of elements may vary each time you iterate. There are several ways to access and process each element of a set in Python, but the sequence may change with each execution. Let's explore different ways to iterate over a s
2 min read