
Python division operator - And what does double slash work in Python?
In Python, slash operator works division.
x = 6 / 2
print(x) # 3.0
print(type(x)) # <class 'float'>
x is not 3 but 3.0 (float). To get the integer, use int
.
x = 6 / 2
y = int(x)
print(y) # 3
print(type(y)) # <class 'int'>
Double slash
Double slash divides and returns a floor value.
x = 7 // 2
y = 20 // 3
print(x) # 3
print(y) # 6
print(type(x)) # <class 'int'>
print(type(y)) # <class 'int'>
The remainder is removed and only integer part is returned. If the value of equation is float, double slash returns a floor and float value.
x = 7.1 // 2
y = 20 // 3.2
print(x) # 3.0
print(y) # 6.0
print(type(x)) # <class 'float'>
print(type(y)) # <class 'float'>
Comments
Powered by Markdown