Hello I am Juny.
there are operations in number such as plus, minus and multiplication.
likewise, programming also has something like that.
those are numeric operators.
basically,
there are the numeric types of int, float and complex in python.
they all can be operated by numeric operators.
the computer handles number as binary number.
so, there is also bitwise operators.
we will learn mathematic operators and bitwise operators in this post.
mathematic operators
they are there below
파이썬에서 산술 연산자는 아래와 같습니다.
operator | operation | |||
---|---|---|---|---|
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 | x unchanged | |||
x ** y | x to the power y |
sum, difference, product and quotient are simple.
but,
// and ** would be unfamiliar.
if you do //, you will get the quotient without remainder.
for example,
5/2 have the quotient and the remainder.
the result will be 2.5
but, if you do 5//2, you will get only the quotient.
the result will be 2.
a = 5 / 2 b = 5 // 2 print("5 / 2 = ",a) print("5 // 2 = ",b)
** is like pow function in C language.
for example,
if you do 2**3, you will get 8 as a result.
a = 2 ** 3 b = pow(2,3) print("2 ** 3 = ", a) print("pow(2,3) = ",b)
bitwise operators
they are like below.
operator | operation | |||
---|---|---|---|---|
x | y | bitwise or of x and y | |||
x ^ y | bitwise exclusive or of x and y | |||
x & y | bitwise and of x and y | |||
x << n | x shifted left by n bits | |||
x >> n | x shifted right by n bits | |||
~x | the bits of x inverted |
| is for "or" operation.
either x bit is 1 or y bit is 1, it will be 1.
for example,
2 is 10 in binary number. and 3 is 11 in binary number.
if 2 and 3 are operated by or, it will be 11 in binary number or 3 in decimal number.
^ is for "xor" operation.
only one of two bits is 1, it will be 1.
for example,
2 is 10 in binary number. and 3 is 11 in binary number.
if 2 and 3 are operated by xor, it will be 01 in binary number or 1 in decimal number.
& is for "and" operation.
both two bits are 1, it will be 1.
for example,
2 is 10 in binary number. and 3 is 11 in binary number.
if 2 and 3 are operated by and, it will be 10 in binary number or 2 in decimal number.
<< is for left shift operation.
it makes the bits shift to left for n times.
and it is like product of 2.
for example,
2 is 10 in binary number. and 3 is 11 in binary number.
if 2 and 3 are operated by <<, it will be 100 in binary number or 4 in decimal number.
>> is for right shift operation.
it makes the bits shift to right for n times.
and it is like division of 2.
for example,
2 is 10 in binary number. and 3 is 11 in binary number.
if 2 and 3 are operated by >>, it will be 01 in binary number or 1 in decimal number.
if negative value is used in shift operation, ValueError exception raises.
let's check it in code.
a = 2 b = 3 print("2 | 3 = ", 2|3) print("2 ^ 3 = ", 2^3) print("2 & 3 = ", 2&3) print("2 << 1 = ", 2<<1) print("2 >> 1 = ", 2>>1) print("2 << -1 = ", 2<<-1)
python tutorial of numeric type operator
Reviewed by Juny
on
7월 21, 2019
Rating:
댓글 없음: