12. Python Arithmetic Operators & Relational operators

 


Operator:

Operator means, one symbol to perform a certain activity, the symbol is considered as an operator.

Example:

‘+’ is a symbol perform 10+20. It’s performing, adding two numbers or addition perform. in the same way subtraction (-), multiplication (*) also doing some arithmetic operations. And we have more operators in python.

Some of the operators in python.

1.       Arithmetic operator.

2.      Relational operator.

3.      Equality operator.

4.      Logical operator.

5.      Bitwise operator.

6.      Shift operator.

7.      Assignment operator.

8.     Ternary operator

9.      Special operator.

a.      Identity operator

b.       Membership operator

 

10.  Operator precedence operator.

11.   Mathematical function by using math module.

Arithmetic Operators

Arithmetic operators are used to performing arithmetic operations such as addition, subtraction, division, multiplication, etc. check all the arithmetic operators.

Operator

Operator Name

Description

Example

'+'

Addition operator

It’s Produces, Adds two values, providing their sum.

10 + 20 = 30

'−'

Subtraction operator

It’s Produces, Subtracts or providing their Difference.

20 - 10 = 10

'*'

Multiplication operator

It’s Produces, product the two values.

10*20 = 200

'/'

Division operator

It’s Produces, divided into two values.

20/10 = 2

'%'

Modulus operator

It’s Produces, divided into two values and returns a remainder.

105/10 = 5

'**'

Exponent operator

It’s Produces, exponential (power) calculation on Values.

10**2 = 100

'//'

Floor division operator

It’s Produces, values of the quotient.

10//2 = 5

 

We have seven athematic operators are there. Five (+,-,*,%,/)are common. But in python, we have two more special operators (**,//).

Examples:

a = 10

b = 2

c,d,e,f,g,h,i = a + b, a - b, a * b, a / b, a % b, a ** b, a // b

print(c),print(d),print(e) ,print(f) ,print(g) ,print(h) ,print(i)

Result.

12 ,8 ,20 ,5.0 ,0 ,100 ,5

In python division always given result as a floating point value only.

Ex:

a = 10

b = 2

c = (a/b)

print(c)                       Result is 5.0

if we want int value as a result,  then we need to use Floor division operator (//).

a = 10             (int type)

b = 2               (int type)      

c = (a//b)

print(c)                      Result is 5   (Result also int type)

if we use floating point of any one, then result also floating point only.

a = 10.0          (float type)

b = 2               (int type)      

c = (a//b)

print(c)                      Result is 5.0   (Result also float type)

One more example:

a ,b,c = 10 , 10.2, 3

print(d)  = (a/c),(b/c),(a//c),(b//c)

(3.3333333333333335, 3.4, 3, 3.0)     Result

One more example:

20/2=10.0                 # The result always float point only. Because division operator always taking the float value.

20.5/2= 10.25           # Result always float point only. Because the division operator has one float value that’s why the result also float value.

20//2 =10                 # Result always int value only. Because Floor division operator both values having int only. that’s why the result also int value.

20.5//2=10.0            # The result always float point only. Because the Floor division operator has one float value that’s why the result also floats value.

 

Some important points in Arithmetic operators

 

Addition operator means, add two values, providing their sum.

Ex: 10+20 = 30    #  Addition operator

Now we applying addition operator to all fundamental data types (int, float, complex, bool, str), through some examples

10+20 = 30               # adding two int values.

>>> 10.5+20.5          # adding two float values.

31.0                 #result

>>> (10+20J)+(25+38J)                # adding two complex values.

(35+58j)         #result

>>> 'rama'+'krishna'           # adding two str values .

'ramakrishna'           #result

>>> 'rama'+10          # adding str  and int values . Result is error.

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: can only concatenate str (not "int") to str

>>> 'rama'+'10'        # adding str  and str values .

'rama10'                     Result

>>> 'rama'+str(10)  # adding str  and str values .

'rama10'         Result

Now in string

>>> 'rama' * 3    #multiply str and int values.

'ramaramarama'      Result

>>> 3 * 'rama'            #multiply int and str values.

'ramaramarama'      Result

>>> 'rama' * 'krishna'         #multiply str and str values.

Traceback (most recent call last):                        Result

  File "<stdin>", line 1, in <module>

TypeError: can't multiply sequence by non-int of type 'str'

>>> 'rama'*'3'           #multiply str and str values.

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: can't multiply sequence by non-int of type 'str'

>>> 'rama' * int(3)  #multiply str and int values.

'ramaramarama'

 

Now we need to know about zero division error.

 

10/0

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

ZeroDivisionError: division by zero

10.0/0

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

ZeroDivisionError: division by zero

10//0

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

ZeroDivisionError: division by zero

10.0//0

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

ZeroDivisionError: division by zero

10%0

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

ZeroDivisionError: integer division or modulo by zero

 

 Now we need to know about multiply with bool values.

 

‘>>> 'rama'* True  #multiply str and bool values.

'rama'     #result

>>> 'Rama*true #multiply str and bool values. But we are not entering the proper bool name.

  File "<stdin>", line 1

    'Rama*true

             ^

SyntaxError: EOL while scanning string literal

 >>> 'rama' * False #multiply str and bool values.

''                       #result

 

Relational operator:

 

When the values of two identifiers are to be compared then Relational operators are used. The output of these comparison operators is always a Boolean value, either True or False. The identifiers can be Numbers or Strings or Boolean values. Strings are compared letter by letter using their ASCII values.

Operator

Operator Name

Description

Example

'=='

Equal to

Given values equal or not, if equal, it’s true otherwise it’s false.

10 == 20 #False

'!='

Not Equal

Given values not equal or not, if not equal, it’s true otherwise it’s false.

10!=20 #False

'>'

Greater than

Given values are Greater than, if Greater than, it’s true otherwise it’s false.

10>20 #False

'<'

Lesser than

Given values are Lesser than, if Lesser than, it’s true otherwise it’s false.

10<20 #True

'>='

Greater than or equal to

Given values are Greater than or equal to, if Greater than or equal to, then it’s true otherwise it’s false.

10>=20 #False

'<='

Lesser than or equal to

Given values are Lesser than or equal to, if Lesser than or equal to, then it’s true otherwise, it’s false.

10>=20 #False

 

Example:

1. >>>10 == 20

False

2. >>>10 != 20

True

3. >>>10 < 20

True

4. >>>10 > 20

False

5. >>>10 <= 20

True

 6. >>>10 >= 20

False

7. >>> "F" < "G"                   # Strings are compared letter by letter using their ASCII values

True

8.>>> "Aston" > "Asher"

True

9.>>> True == True

True

If we don’t know the ASCII values of strings then we can check, support them with ord() function. Ex: print(ord('d')), result will be (100).

If we have an idea of ASCII values but we don’t know the string, then we can find out string or symbol support with chr() function. Ex: print(chr(121)), result will be (y).

One More example:

a ='rama'

b = 'krishna'

c = (a==b),(a!=b),(a>b),(a<b),(a>=b),(a<=b)

print(c)

(False, True, True, False, True, False)    # Result.

Relational operators chaining is possible check some examples

10 <20                                  # True

10<20<30                           # True

10<20<30<40                   # True

10<20<30<40>50           # False

it’s comparing two values one by one, if one result is false then its total is false. Otherwise, it’s true.

Some examples of ‘==’ and ‘!=’

10==20  # False

10!=20  # True

1==True # True

10==10.0 # True

‘rama’ ==’rama’ # True

10 == ‘rama’  # False

10 == ‘10’     # False

10 ==20 ==30 ==40  # False

10 ==10 ==10 ==10  # True

What is the difference between ‘==’ and ‘is’ operator

‘==’ operator meant for contain comparison

‘is’ operator meant for address reference comparison

Ex:

list_1= [10,20,30]

list_2= [10,20,30]

print (id(list_1),id(list_2))

21687976,  26596680   #result

print(list_1==list_2)

True                                    #result

print(list_1 is list_2)

False                                 #result

Both are the same values, so ‘==’ result is True.

Both are not the same address, so ‘is’ the result is False.

 

0 Comments

Post a Comment

Post a Comment (0)

Previous Post Next Post