Skip to content

Python literals and basic conversions [ref]

Problem

  • Convert "5" to an integer, 5 to a string, and "2.5" to a float.
  • Evaluate the binary literal 0b101 and format 5 as eight binary digits.
  • Create positive and negative infinity, then test whether 10 lies between them.
  • Compute the minimum and maximum signed 32-bit integers.

Python solution

int("5")      # 5
str(5)        # '5'
float("2.5")  # 2.5

0b101             # 5
format(5, "08b")  # '00000101'

lo, hi = float("-inf"), float("inf")
lo < 10 < hi  # True

(-(2 ** 31), 2 ** 31 - 1)  # (-2147483648, 2147483647)