C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Operator 1: The "/" operator leaves the fractional part of the result intact. It does not matter if the two operands are fractional or not.
Operator 2: The "//" operator divides in the same way. But it also rounds the result down to the nearest integer.
Python program that divides numbers
a = 100
b = 7
# Divide 100 by 7.
print(a / b)
# Discard fractional part of result.
print(a // b)
Output
14.285714285714286
14
Python common line division
>>> 5//3
1
>>> 5/3
1.6666666666666667
Conversion: Float is similar to other built-ins like int or str. Python simplifies common conversions.
ConvertBuilt-insPython program that uses float
# Float converts a string into a float number.
value = "3.14"
number = float(value)
print(number)
print(number == 3.14)
print(value == "3.14")
print()
# Float also converts an integer into a float number.
integer = 100
floating = float(integer)
print(floating)
print(integer)
Output
3.14
True
True
100.0
100
Note: Int will cause an error if we try to convert a floating-point number within a string (like "123.4").
Python program that uses int
# Convert a string to int.
input = "123"
result = int(input)
print(result)
# Use int to convert from floating to integral.
input = 456.9
result = int(input)
print(result)
Output
123
456
Python program that uses hex
# Convert this value to a hex.
value = 10
result = hex(value)
print(result)
# Convert another value.
value = 100
result = hex(value)
print(result)
Output
0xa
0x64
Python program that uses oct
# Convert 74 into octal which is 112.
number = 74
octal = oct(number)
print(octal)
Output
0o112
Negative: The sign bit is represented by the 0 before the lowercase "b." A -1 has a leading minus sign.
Python program that uses bin
number = 100
# Convert this number to a binary string.
result = bin(number)
print(result)
Output
0b1100100
More bin examples
bin(-1) -0b1
bin(0) 0b0
Imaginary unit: Complex numbers include an "imaginary unit" that is separate from the real parts. These numbers never become bored.
Info: In Python we have the complex() built-in function. These numbers can be added, subtracted, and manipulated in other ways.
Python program that uses complex numbers
# Create two complex numbers.
complexA = complex(3, 10)
complexB = complex(5, 15)
# Add the two together.
# ... The result is also complex.
complexC = complexA + complexB
print(complexC)
Output
(8+25j)
Version 1: This version of the code uses the single-slash operator to perform a division.
Version 2: Here we use the double-slash operator to perform division and get an integer-only result.
Result: We find that the "//" operator is slower than the "/" operator. It adds steps beyond the regular operator.
Python program that times division
import time
a = 1000
b = 223
c = 0
print (time.time())
# Version 1: normal division
i = 0
while i < 10000000:
c = a / b
i += 1
print (time.time())
# Version 2: integer result division
i = 0
while i < 10000000:
c = a // b
i += 1
print (time.time())
Output
1345843075.764
1345843077.922 (/ = 2.158 s)
1345843080.448 (// = 2.526 s)