Python Tutorial 5 – Python Operators

Python Tutorial 5 – Python Operators

Let’s begin our another session use of Python Operators and their priority while using them. Below is the list of commonly used mathematical operators mentioned in the priority in which they will be executed:

OperatorUsagePriority
+(Plus) and (-)minusUsed as unary operator1
**(exponential) 2
*, /, //, % 3
+ and –Used as binary operator4

1) Using plus and minus operator in print function

print(3+4) = 7
print(3+4.0) = 7.0
print(5-4) = 1
print(10.0-2) = 8.0

2) Using integer and string

print(3+’4’) 
This will give error because integer and string cannot be added

3) Using two strings in addition

print(‘3’+’4’) = 34

This command just concatenates the wo strings.
Remember that we cannot use –(minus) sign with strings because that is not a valid operator on strings unlike + which concatenates the strings.

4) ** operator – This is the exponential operator where left argument is base and right one is exponent. X ** Y means X raised to the power of Y. If any one of the arguments is a float number, then the output is float. Only when both arguments are integer, then output is integer.

print(5 ** 2) = 25
print(3 ** 2) = 9
print(4 ** 3.0) = 64.0

5) * – multiplication on integers

print(3 *4 ) = 12
print(3 * 4.) = 12.0

(Remember from our last session that 3. and 3.0 are same thing i.e. float number 3)

6) * – multiplication operation on strings. Such operation just prints the string n number of times as below:

print(4 * ‘3’ ) = 3333

This command prints the string ‘3’ four times. Remember that we cannot use float number is this command

print(‘5’ * 3) = 555

Order of the string and number can be changed.

print(‘2’ * ‘6’)

This command will give error because both operators are strings which cannot be multiplied

7) / – Division operator. This operator divides the two number, but the output is always a float number

print(8/4) = 2.0
print(10/5.0) = 2.0
print(15.0/3) = 5.0
print(20./4.0) = 5.0

Now try some number that will give some decimal portion also:

print(9/4) = 2.25
print(9/2) = 4.5
print(9.0/2.55) = 3.5294117647058827
print(1/2) = 0.5

So, two points to remember for /:
–This operator always gives a float output irrespective of whether the two arguments are integer or float
–This operator divides and gives the decimal portion of the output if the arguments are like that

8) // – Division operator. This is another division operator but with below features:

–If both the arguments are integer, then output is integer. If any one argument is float, then output is float number with decimal portion having value 0
And
–This operator omits the decimal/fractional part and rounds off the output to the nearest smallest integer if there is any decimal part in the output

Please see below examples:

print(4//2) = 2
print(10//2) = 5
print(15.0//3)= 5.0
print(20./4.0) = 5.0

Now try below examples:

print(9//4) =2
print(9//4.0) = 2.0
print(-10//3) = -4 ( actual result is -3.33 but rounded to nearest smaller integer is -4)

In these 2 examples, the actual result is 2.25(using ‘/’) but this operator will round off the result to nearest smallest integer. Float value gave the float output with decimal part being 0.
Try out more examples on these operators for more clarity.

9) % – Reminder operator

print(10 % 2) = 0
print(10.0 % 5) = 0.0
print(10%2.5) = 0
print(10%3) = 1
print(10%3.0) = 1.0
print(10%3.3) = 0.1
print(15%.3.5) = 1.0
print(15%3.6) = 0.6

10) Try below examples
Binary operator, operating on two arguments:

print(5+3.4) = 8.0 
print(4.0 – 8) = -4.0

Unary operator, operating on one argument

print(+3) = 3
print(-1) = -1

Direction of calculation in Python Operators: 

Most of the operators follow calculation from left hand side. The only exception to this rule is **(exponential) operator which follows calculation from right hand side

11) When using multiple arguments:

print(2 *3 *4) = 24
print(3 -5 + 6.0) = 4.0

Now in above two examples, it does not matter whether you start your calculation from left side of the command or right side of the command because result is same. But for some operators, the direction will matter:

print(10 % 6 % 3) 

If we start the calculation from left side, then output will be 1
10%6 =4 and 4%3 =1
but if we start the calculation from right side, then we will get error:
6%3=0 and 10%0 = error

12) ** operator – This follows right hand side calculation

print(2 ** 2 ** 3) = 256
2**3 = 8 and 2**8 = 256

Left hand side calculation will give result as 64 which is not correct

Operator Priorities in Python Operators:

When in the expression to evaluate, there are more than one operator involved, then calculation is done considering the operator priorities as present in the table above:

13) Try out below examples:

print(10 * 3 % 4) = 2
print(10 % 3 * 5) = 5
print(10 / 3 * 5) = 16.66
print(10 * 3 / 5) = 6.0
print(10 * 3 / 5 +2) = 8.0
print(10 * 3 + 5 / 5 +2) = 30(10*3) + 1(5/5) + 2 = 33.0
print(2 * 3 ** 2) = 9(3 **2) * 2 = 18

Use of Parenthesis in expressions:

If we have used parenthesis in our expression, then expression in parenthesis will calculate first. Use of parenthesis sometimes between Python operators may change the result of expression because priority might change for calculation. It may be possible that there is no change in the result even after using parenthesis which is expected.

14) Below example:

print((3+2) / (6 * 4)//5 + (3 + (10/4)//2))

The answer is 4.0 Try it your self and check the calculation

Previous Tutorial: https://mytechdevice.com/python-tutorial-4-python-literals/

 

Leave a Reply

Your email address will not be published.