numeric type operations in python

there are three numeric types in python

  • int
  • float
  • complex

python is dynamic typing language. so, we don't need to code the type explicitly.
but, we can identify the type using "type" built-in function.


x = 1
y = 3.6

print("x is {} type".format(type(x)))
print("y is {} type".format(type(y)))


there are operations for numeric types. I will introduce them in this page.

x + y        sum of x and y
x - y         difference of x and y
x * y         product of x and y
x / y         quotient of x and y
x // y        floored quotient of x and y
x % y        remainder of x / y
-x             x negated
+x            positive x
abs(x)       absolute value or magnitude of x
int(x)        x converted to integer
float(x)     x converted to floating point
pow(x,y)   x to the power y
x ** y       x to the power y

most of them is very similar to other programming language's operations and mathematical operators.

by the way, let's see them in example code.


x = 1
y = 2.5

print("x + y = {}".format(x + y))
print("x - y = {}".format(x - y))
print("x * y = {}".format(x * y))
print("x / y = {}".format(x / y))
print("x // y = {}".format(x // y))
print("x % y = {}".format(x % y))
print("-x = {}".format(-x))
print("+x = {}".format(+x))
print("abs(y) = {}".format(abs(y)))
print("int(y) = {}".format(int(y)))
print("float(x) = {}".format(float(x)))
print("pow(2,3) = {}".format(pow(2,3)))
print("3**3 = {}".format(3**3))






Comments

Popular posts from this blog

making menubar and menu on QML

let's start QML programming with PySide2

QSizePolicy Fixed example in pyside2