Followers

Saturday, September 11, 2021

Operators - Python

 Operators 

The Operations being carried out on data are represented by operators. The Symbols that trigger the operation/action on data are called operators. The operations(specific tasks) are represented by Operators and the objects of the operation(s) are referred to as Operands. 

Python's rich set of operators comprises of these types of operators : 

(i) Arithmetic operators 

(ii) Relational operators 

(iii) Identity operators 

(iv) Logical operators 

(v) Bitwise operators 

(vi) Membership operators 

Out of these, we shall talk about membership operators later when we talk about strings/text, lists, and dictionaries. 

In this post, we will discuss arithmetic operators and Unary operators in detail. 



Arithmetic Operators  

To do arithmetic, Python uses arithmetic operators. Python provides operators for basic calculations, as given below :

        Addition                               //        Floor Division            

         Subtraction                                Remainder        

        Multiplication                      **        Exponentiation

         Division 



Each of these operators is a binary operator i.e., it requires two values (operands) to calculate, a final answer. Apart from this binary operator, Python provides two unary arithmetic operators(that requires one operand) also, which are unary +, unary -.


Unary Operators

Unary +

The operator's unary '+' precedes an operand. The operand (the value on which the operators operate) of the unary + operators must have an arithmetic type and the result is the value of the argument. For example, 

      if a = 5       then + a means 5 .

      if a = 0       then + a means 0.

      if a = -4      then = a means -4.

Unary -

The operator's unary - precedes an operand. The operand of the unary - operator must have an arithmetic type and the result is the negation of its operand's value. For example,  

     if a = 5      then -a means -5. 

     if a = 0      then -a means 0 (there is no quantity known as -0)

     if a = -4     then -a means 4. 


Binary Operator

Operators that act upon two operands are referred to as Binary Operators. The operands of a binary operator are distinguished as the left or right operand. Together, the operator and its operands constitute an expression.


  • Addition Operator +
     The arithmetic binary operator + adds values of its operands and the result is the sum of the values of its two operands. 

     For Example, 

     4 + 20                                              results in   24

     a +5 (where a = 2)                           results in   7

     a + b ( where a = 4, b = 6)               results in 10

For Addition operator + operands may be of number types.


Python also offers + as a concatenation operator when used with strings, tuples, and lists. This functionality for strings will be covered in upcoming posts.


  • Subtraction Operator - 
     The - operator subtracts the second operand from the first. 

      For Example,  

        14 - 3                                                 evaluates to   11

        a - b (where a = 7, b = 5)                   evaluates to   2

        x - 3 (where x = -1)                            evaluates to  -4   

        The operand may be of number types. 


  • Multiplication Operator *
       The * operator multiplies the values of its operands. 

        For Example, 

          3 * 4                                              evaluates to  12

          b * 4 (where b = 6)                        evaluates to  24

          p * 2 (where p = -5)                       evaluates to  -10

          a * c (where a = 3, c = 5)                evaluates to  15

         The operands may be of integer or floating-point number types.

Python also offers * as to replication operator when used with strings. This functionality will be covered in upcoming posts. 


  • Division Operator / 
       The / operator in Python 3.x divides its first operand by the                 second operand and always returns the result as a float value,              e.g.,

         4/2                                               evaluates to   2.0

         100/10                                           evaluates to  10.0

          7/2.5                                             evaluates to   2.8

          100/32                                          evaluates to  3.125

          13.5/1.5                                         evaluates to  9.0


  • Floor Division Operator //
       Python also offers another division operator //, which performs            the floor division. The floor divisions is the division in which            only the whole part of the result is given in the output and the            fractional part is truncated. 
       To understand this, consider the third example of division given          in division operator /, i.e., 
         a  = 15.9, b = 3,
         a/b evaluates to 5.3 
      Now if you change the division operator /, with floor division //          in above expressions, i.e., 
         If a = 15.9, b = 3, 
         a//b will evaluate to 5.0     
       Consider some more examples : 
        100//32       evaluates to       3.0
        7//3             evaluates to        2
        6.5//2          evaluates to       3.0
       The operands may be of number types. 

  • Modulus Operator % 
      The % operator finds the modulus (i.e., remainder but                           pronounced as mo-du-lo) of its first operand relative to the                 second. That is, it produces the remainder by dividing the first           operand by the second operand.
       For Example, 
        19%6 evaluates to 1, since 6 goes into 19 three times with a 
        remainder 1.
       Similarly, 
                 7.2%3                will yield       1.2
                  6%2.5               will yield       1.0
       The operand may be of number types. 

  • Exponentiation Operator **
        The exponentiation operator ** performs exponentiation                     (Power) calculation, i.e., it returns the result of a number                     raised to a power (exponent). For Example,
                                                                            
        4**3                         evaluates to 64 (4^3)
        a**b (a = 7, b = 4)   evaluates to 2401 (a^b i.e., 7^4)
        x**0.5 (x = 49)       evaluates to 7.0 (X^0.5, i.e., √x, i.e.,√49)
        27.009**0.3            evaluates to 2.68814413570761(27.009^0.3)
         
        The operands may be of number types. 



So with this, we end with this Topic, don't forget to follow me so that you can get the latest updates of my new posts that I upload weekly, I will try my best to upload daily OR you can Sign up for Newsletters at the top of the Blog and that will give you E-mail updates of my New Post.
Jai Hind!!!!

Vande Mataram!!! 

No comments:

Post a Comment

If you have any doubts, please do let me Know.