Operaattorin toiminnot Pythonissa | Sarja 1

Pythonilla on ennalta määritellyt funktiot monille matemaattisille loogisille bittikohtaisille relaatiooperaatioille moduulin "operaattori" alla. Joitakin perustoimintoja käsitellään tässä artikkelissa. 1. lisää(a b) :- Tämä funktio palauttaa lisäys annetuista argumenteista. Toiminta - a + b. 2. ala(a b) :- Tämä funktio palauttaa ero annetuista argumenteista. Toiminta - a - b. 3. mul(a b) :- Tämä funktio palauttaa tuote annetuista argumenteista. Toiminta - a * b. 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. truediv(ab) :- Tämä funktio palauttaa jako annetuista argumenteista. Toiminta - a / b. 5. floordiv(ab) :- Tämä funktio palauttaa myös annettujen argumenttien jaon. Mutta arvo on lattiaarvo, ts. palauttaa suurimman pienen kokonaisluvun . Toiminta - a // b. 6. pow(ab) :- Tämä funktio palauttaa eksponentio annetuista argumenteista. Toiminta - a ** b. 7. mod(ab) :- Tämä funktio palauttaa moduuli annetuista argumenteista. Toiminta - a % b. 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  (  a    b  ))   # using floordiv() to divide two numbers   print   (  'The floor division of numbers is : '    end  =  ''  );   print   (  operator  .  floordiv  (  a    b  ))   # using pow() to exponentiate two numbers   print   (  'The exponentiation of numbers is : '    end  =  ''  );   print   (  operator  .  pow  (  a    b  ))   # using mod() to take modulus of two numbers   print   (  'The modulus of numbers is : '    end  =  ''  );   print   (  operator  .  mod  (  a    b  ))   
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. lt(a b) :- Tätä toimintoa käytetään tarkista onko a pienempi kuin b vai ei . Palauttaa tosi, jos a on pienempi kuin b muuten palauttaa epätosi. Toiminta - a < b . 9. (a b) :- Tätä toimintoa käytetään tarkista, onko a pienempi tai yhtä suuri kuin b vai ei . Palauttaa tosi, jos a on pienempi tai yhtä suuri kuin b muuten palauttaa epätosi. Toiminta - a <= b . 10. eq(a b) :- Tätä toimintoa käytetään tarkista onko a yhtä suuri kuin b vai ei . Palauttaa tosi, jos a on yhtä kuin b muuten palauttaa epätosi. Toiminta - a == b . 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  (  a    b  )):   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  (  a    b  )):   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  (  a    b  )):   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. gt(ab) :- Tätä toimintoa käytetään tarkista onko a suurempi kuin b vai ei . Palauttaa tosi, jos a on suurempi kuin b muuten palauttaa epätosi. Toiminta - a > b . 12. ge(ab) :- Tätä toimintoa käytetään tarkista, onko a suurempi tai yhtä suuri kuin b vai ei . Palauttaa tosi, jos a on suurempi tai yhtä suuri kuin b muuten palauttaa epätosi. Toiminta - a >= b . 13.ne(ab) :- Tätä toimintoa käytetään tarkista, onko a erisuuri kuin b vai onko se yhtä suuri . Palauttaa tosi, jos a ei ole yhtä suuri kuin b muuten palauttaa epätosi. Toiminta - a != b . 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  (  a    b  )):   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  (  a    b  )):   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  (  a    b  )):   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