Recent Posts

python tutorial of numeric type operator



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

파이썬에서 산술 연산자는 아래와 같습니다.


operatoroperation
x + ysum of x and y
x - ydifference of x and y
x * yproduct of x and y
x / yquotient of x and y
x // yfloored quotient of x and y
x % yremainder of x / y
-xx negated
+xx unchanged
x ** yx 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)


python numeric operator division



** 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)


python numeric operator power




bitwise operators


they are like below.



operatoroperation
x | ybitwise or of x and y
x ^ ybitwise exclusive or of x and y
x & ybitwise and of x and y
x << nx shifted left by n bits
x >> nx shifted right by n bits
~xthe 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 numeric operator bitwise




python tutorial of numeric type operator python tutorial of numeric type operator Reviewed by Juny on 7월 21, 2019 Rating: 5

댓글 없음:

Powered by Blogger.