Python如何使用运算符函数?详细指南|S2

2021年3月18日15:01:23 发表评论 990 次浏览

Python中的运算符函数|S1

本文讨论了更多功能。

1. setitem(ob, pos, val):-此函数用于在容器的特定位置赋值。

操作- ob[pos] = val

2. delitem(ob, pos):—该函数用于删除容器中特定位置的值。

操作- del ob[pos]

3. getitem(ob, pos):—这个函数用于访问容器中特定位置的值。

操作- ob (pos)

# 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 ))

输出如下:

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):-该函数用于设置容器中特定范围内的值。

操作- obj[a:b] = vals

5. delitem(ob, slice(a,b)):-这个函数用于删除容器中特定范围内的值。

操作- del obj[a:b]

6. getitem(ob, slice(a,b)):-这个函数用于访问容器中特定范围内的值。

操作——obj [a, b]

# 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 )))

输出如下:

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):—这个函数用于连接两个容器。

操作- obj1 + obj2

8. contains(ob1,obj2):—这个函数用于检查obj1中是否有obj2。

操作- obj1中的obj2

# 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" )

输出如下:

The concatenated string is : lsbin
geeksfor contains geeks

9. and_(a,b):-该函数用于按位计算上述参数。

操作- a和b

10. or_(a,b):-该函数用于按位计算或计算上述参数。

操作- a | b

11. xor(a,b):-这个函数用于计算上述参数的按位异或值。

运算- a ^ b

12. invert(a):-这个函数用于计算上述参数的位反转。

操作- ~ a

# 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 (a)

输出如下:

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

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

注意怪胎!巩固你的基础Python编程基础课程和学习基础知识。

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: