logo

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

Питхон има унапред дефинисане функције за многе математичке логичке релационе побитне итд операције под модулом 'оператор'. Неке од основних функција су покривене у овом чланку. 1. додај (а б) :- Ова функција се враћа додатак датих аргумената. Операција - а + б. 2. под(а б) :- Ова функција се враћа разлика датих аргумената. Операција - а - б. 3. мул(а б) :- Ова функција се враћа производ датих аргумената. Операција - а * б. Python
# Python code to demonstrate working of  # add() sub() mul() # importing operator module  import operator # Initializing variables a = 4 b = 3 # using add() to add two numbers print ('The addition of numbers is :'end=''); print (operator.add(a b)) # using sub() to subtract two numbers print ('The difference of numbers is :'end=''); print (operator.sub(a b)) # using mul() to multiply two numbers print ('The product of numbers is :'end=''); print (operator.mul(a b)) 
Output:
The addition of numbers is:7 The difference of numbers is :1 The product of numbers is:12 

4. труедив(аб) :- Ова функција се враћа дивизије датих аргумената. Операција - а / б. 5. флоордив(аб) :- Ова функција такође враћа поделу датих аргумената. Али вредност је минимална вредност, тј. враћа највећи мали цео број . Операција - а // б. 6. пов(аб) :- Ова функција се враћа степеновање датих аргумената. Операција - а ** б. 7. мод(аб) :- Ова функција се враћа модул датих аргумената. Операција - а % б. Python
# Python code to demonstrate working of  # truediv() floordiv() pow() mod() # importing operator module  import operator # Initializing variables a = 5 b = 2 # using truediv() to divide two numbers print ('The true division of numbers is : 'end=''); print (operator.truediv(ab)) # using floordiv() to divide two numbers print ('The floor division of numbers is : 'end=''); print (operator.floordiv(ab)) # using pow() to exponentiate two numbers print ('The exponentiation of numbers is : 'end=''); print (operator.pow(ab)) # using mod() to take modulus of two numbers print ('The modulus of numbers is : 'end=''); print (operator.mod(ab)) 
Output:
The true division of numbers is: 2.5 The floor division of numbers is: 2 The exponentiation of numbers is: 25 The modulus of numbers is: 1 

8. лт(а б) :- Ова функција се користи за провери да ли је а мање од б или не . Враћа тачно ако је а мање од б, иначе враћа нетачно. Операција - а< b . 9. (а б) :- Ова функција се користи за провери да ли је а мање или једнако б или није . Враћа тачно ако је а мање или једнако б, иначе враћа нетачно. Операција - а<= b . 10. ек(а б) :- Ова функција се користи за провери да ли је а једнако б или није . Враћа тачно ако је а једнако б, иначе враћа нетачно. Операција - а == б . Python
# Python code to demonstrate working of  # lt() le() and eq() # importing operator module  import operator # Initializing variables a = 3 b = 3 # using lt() to check if a is less than b if(operator.lt(ab)): print ('3 is less than 3') else : print ('3 is not less than 3') # using le() to check if a is less than or equal to b if(operator.le(ab)): print ('3 is less than or equal to 3') else : print ('3 is not less than or equal to 3') # using eq() to check if a is equal to b if (operator.eq(ab)): print ('3 is equal to 3') else : print ('3 is not equal to 3') 
Output:
3 is not less than 3 3 is less than or equal to 3 3 is equal to 3 

11. гт(аб) :- Ова функција се користи за провери да ли је а веће од б или не . Враћа тачно ако је а веће од б, иначе враћа нетачно. Операција - а > б . 12. ге(аб) :- Ова функција се користи за провери да ли је а веће или једнако б или не . Враћа тачно ако је а веће или једнако б, иначе враћа нетачно. Операција - а >= б . 13.не(аб) :- Ова функција се користи за провери да ли а није једнако б или је једнако . Враћа тачно ако а није једнако б, иначе враћа нетачно. Операција - а != б . Python
# Python code to demonstrate working of  # gt() ge() and ne() # importing operator module  import operator # Initializing variables a = 4 b = 3 # using gt() to check if a is greater than b if (operator.gt(ab)): print ('4 is greater than 3') else : print ('4 is not greater than 3') # using ge() to check if a is greater than or equal to b if (operator.ge(ab)): print ('4 is greater than or equal to 3') else : print ('4 is not greater than or equal to 3') # using ne() to check if a is not equal to b if (operator.ne(ab)): print ('4 is not equal to 3') else : print ('4 is equal to 3') 
Output:
4 is greater than 3 4 is greater than or equal to 3 4 is not equal to 3