Arithmetic operation in Python

in #python7 years ago

Python provides different arithmetic operators for performing different operations on numerical data.

The list of arithmatic operation used in python is as follow.

x + y #Addition
x - y #Subtraction
x * y #Multiplication
x / y #Division
x // y #Truncating division
x ** y #Exponentiation. Sets x to the power y;
x % y #Modulo operator
–x #Unary minus
+x #Unary plus

Please note that x and y are variable name holding proper value. That means you can't do x/y if either of them is string. It will throw an error.

Division : It return the true integer value. e.g 2/3 = 0.67 will result in integer part of the result i.e 0. However if one of them is floating point it will give floating point result. e.g 2.0/3 = 0.67. But truncating division operator(//) will always return integer part and ignoring remainder no matter if one of them is floating or not.

Exponentiation : To apply exponentiation , you can use double asteriks . e.g a**b. There is in built python function called pow() to calculate the exponentiation. e.g pow(2,3)

Hope you get the idea about arithmetic operation in python. See you in the next tutorial till then "Happy Coding".

Please join the @labwork team and Up vote,follow and resteem.

Comments are always appreciated.

Sort:  

In Python 3.x, division result is always float.

Even in Python 2.7 you can actually set this functionality by importing "division" module from future.

from __future__ import division
1 / 2
# 0.5

:)

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by labwork from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.