Python provide following builtins numeric data types:
The builtin function int()
can be used to convert other types to integer, including base changes.
Example:
In [9]:
# Converting real to integer
print ('int(3.14) =', int(3.14))
print ('int(3.64) =', int(3.64))
print('int("22") =', int("22"))
In [10]:
print('int("22.0") !=', int("22.0"))
In [11]:
print("int(3+4j) =", int(3+4j))
In [13]:
# Converting integer to real
print ('float(5) =', float(5))
In [15]:
print('int("22.0") ==', float("22.0"))
In [17]:
print('int(float("22.0")) ==', int(float("22.0")))
In [12]:
# Calculation between integer and real results in real
print ('5.0 / 2 + 3 = ', 5 / 2 + 3)
In [18]:
x = 3.5
y = 2.5
z = x + y
print(x, y, z)
print(type(x), type(y), type(z))
z = int(z)
print(x, y, z)
print(type(x), y, type(z))
In [9]:
# Integers in other base
print ("int('20', 8) =", int('20', 8)) # base 8
print ("int('20', 16) =", int('20', 16)) # base 16
In [ ]:
In [11]:
# Operations with complex numbers
c = 3 + 4j
print ('c =', c)
print ('Real Part:', c.real)
print ('Imaginary Part:', c.imag)
print ('Conjugate:', c.conjugate())
NOTE: The real numbers can also be represented in scientific notation, for example: 1.2e22.
Python has a number of defined operators for handling numbers through arithmetic calculations, logic operations (that test whether a condition is true or false) or bitwise processing (where the numbers are processed in binary form).
In [33]:
x = 22
y = 4
if(x < y):
print("X wins")
else:
print("Y wins")
In [34]:
x = 2
y = 4
if(x < y):
print("X wins")
else:
print("Y wins")
In [32]:
x = 2
y = 4
if(x > y):
print("X wins")
else:
print("Y wins")
In [31]:
x = 14
y = 4
if(x > y):
print("X wins")
else:
print("Y wins")
In [ ]:
x = 2
y = 4
if(x <= y):
print("X wins")
else:
print("Y wins")
In [ ]:
x = 2
y = 4
if(x <= y):
print("X wins")
else:
print("Y wins")
In [ ]:
x = 21
y = 4
if(x <= y):
print("X wins")
else:
print("Y wins")
In [ ]:
x = 4
y = 4
if(x <= y):
print("X wins")
else:
print("Y wins")
In [ ]:
x = 8
y = 4
if(x >= y):
print("X wins")
else:
print("Y wins")
In [ ]:
x = 4
y = 14
if(x <= y):
print("X wins")
else:
print("Y wins")
In [ ]:
x = 4
y = 4
if(x <= y):
print("X wins")
else:
print("Y wins")
In [17]:
x = 4
y = 4
if(x == y):
print("X & Y are equal")
else:
print("X & Y are different")
In [19]:
x = 41
y = 4
if(x == y):
print("X & Y are equal")
else:
print("X & Y are different")
In [21]:
x = 2+1j
y = 3+1j
if(x == y):
print("X & Y are equal")
else:
print("X & Y are different")
In [22]:
x = 21+1j
y = 21+1j
if(x == y):
print("X & Y are equal")
else:
print("X & Y are different")
In [22]:
x = 21+1j
y = 21+1j
if(x == y):
print("X & Y are equal")
else:
print("X & Y are different")
In [23]:
x = 4
y = 4
if(x != y):
print("X & Y are different")
else:
print("X & Y are equal")
In [24]:
x = 41
y = 4
if(x != y):
print("X & Y are different")
else:
print("X & Y are equal")
In [25]:
x = 2+1j
y = 3+1j
if(x != y):
print("X & Y are different")
else:
print("X & Y are equal")
In [26]:
x = 21+1j
y = 21+1j
if(x != y):
print("X & Y are different")
else:
print("X & Y are equal")
During the operations, numbers are converted appropriately (eg. (1.5+4j) + 3
gives 4.5+4j
).
Besides operators, there are also some builtin features to handle numeric types: abs()
, which returns the absolute value of the number, oct()
, which converts to octal, hex()
, which converts for hexadecimal, pow()
, which raises a number by another and round()
, which returns a real number with the specified rounding.
In [36]:
x = 10 #-> 1010
y = 11 #-> 1011
1011
"""
OR
0 0 | 0
0 1 | 1
1 0 | 1
1 1 | 1
AND
0 0 | 0
0 1 | 0
1 0 | 0
1 1 | 1
"""
In [37]:
print("x<<2 = ", x<<2)
print("x =", x)
print("x>>2 = ", x>>2)
print("x&y = ", x&y)
print("x|y = ", x|y)
print("x^y = ", x^y)
print("x =", x)
print("~x = ", ~x)
print("~y = ", ~y)
In [ ]:
In [ ]:
In [ ]: