logo

Операторске функције у Питхон-у | Сет 2

Операторске функције у Питхон-у | Сет 1

Више функција се разматра у овом чланку.



1. сетитем(об пос вал) :- Ова функција се користи за доделити вредност на а посебан положај у контејнеру. 
Операција - об[пос] = вал

2. делитем(об пос) :- Ова функција се користи за избрисати вредност на а посебан положај у контејнеру. 
Операција - дел об[пос]

поштар

3. гетитем(об пос) :- Ова функција се користи за приступ вредност на а посебан положај у контејнеру. 
Операција - об[пос]
 



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(0len(li)): print (li[i]end=' ') print ('r') # using setitem() to assign 3 at 4th position operator.setitem(li33) # printing modified list after setitem() print ('The modified list after setitem() is : 'end='') for i in range(0len(li)): print (li[i]end=' ') print ('r') # using delitem() to delete value at 2nd index operator.delitem(li1) # printing modified list after delitem() print ('The modified list after delitem() is : 'end='') for i in range(0len(li)): print (li[i]end=' ') print ('r') # using getitem() to access 4th element print ('The 4th element of list is : 'end='') print (operator.getitem(li3)) 

Излаз: 
 

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. сетитем(об слице(аб) валс) :- Ова функција се користи за поставите вредности у одређеном опсегу у контејнеру. 
Операција - обј[а:б] = валс

како затворити режим програмера

5. поделити (об слице(аб)) :- Ова функција се користи за избришите вредности из одређеног опсега у контејнеру. 
Операција - од обј[а:б]



6. гетитем(об слице(аб)) :- Ова функција се користи за приступити вредностима у одређеном опсегу у контејнеру. 
Операција - обј[а:б]
 

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(0len(li)): print (li[i]end=' ') print ('r') # using setitem() to assign 234 at 2nd3rd and 4th index operator.setitem(lislice(14)[234]) # printing modified list after setitem() print ('The modified list after setitem() is : 'end='') for i in range(0len(li)): print (li[i]end=' ') print ('r') # using delitem() to delete value at 3rd and 4th index operator.delitem(lislice(24)) # printing modified list after delitem() print ('The modified list after delitem() is : 'end='') for i in range(0len(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(lislice(02))) 

Излаз: 
 

разлика између тигра и лава
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. цонцат(обј1обј2) :- Ова функција се користи за спојити два контејнера. 
Операција - обј1 + обј2

8. садржи(обј1обј2) :- Ова функција се користи за проверите да ли је обј2 присутан у обј1
Операција - обј2 у обј1
 

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(s1s2)) # using contains() to check if s1 contains s2 if (operator.contains(s1s2)): print ('geeksfor contains geeks') else : print ('geeksfor does not contain geeks') 

Излаз: 
 

The concatenated string is : geeksforgeeks geeksfor contains geeks


  9. и_(аб) :- Ова функција се користи за израчунавање по битовима и наведених аргумената. 
Операција - а & б

је кат тимпф адвокат

10. или_(аб) :- Ова функција се користи за израчунавање по битовима или наведених аргумената. 
Операција - а | б

11. бесплатно (аб) :- Ова функција се користи за израчунавање битни кор наведених аргумената. 
Операција - а ^ б

12. инверт(а) :- Ова функција се користи за израчунавање битова инверзија поменутог аргумента. 
Операција - ~ а
 

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_(ab)) # using or_() to display bitwise or operation print ('The bitwise or of a and b is : 'end='') print (operator.or_(ab)) # using xor() to display bitwise exclusive or operation print ('The bitwise xor of a and b is : 'end='') print (operator.xor(ab)) # 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)) 

Излаз: 
 

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