int1.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. print(int(False))
  2. print(int(True))
  3. print(int(0))
  4. print(int(1))
  5. print(int(+1))
  6. print(int(-1))
  7. print(int('0'))
  8. print(int('+0'))
  9. print(int('-0'))
  10. print(int('1'))
  11. print(int('+1'))
  12. print(int('-1'))
  13. print(int('01'))
  14. print(int('9'))
  15. print(int('10'))
  16. print(int('+10'))
  17. print(int('-10'))
  18. print(int('12'))
  19. print(int('-12'))
  20. print(int('99'))
  21. print(int('100'))
  22. print(int('314'))
  23. print(int(' 314'))
  24. print(int('314 '))
  25. print(int(' \t\t 314 \t\t '))
  26. print(int(' 1 '))
  27. print(int(' -3 '))
  28. print(int('0', 10))
  29. print(int('1', 10))
  30. print(int(' \t 1 \t ', 10))
  31. print(int('11', 10))
  32. print(int('11', 16))
  33. print(int('11', 8))
  34. print(int('11', 2))
  35. print(int('11', 36))
  36. print(int('xyz', 36))
  37. print(int('0o123', 0))
  38. print(int('8388607'))
  39. print(int('0x123', 16))
  40. print(int('0X123', 16))
  41. print(int('0A', 16))
  42. print(int('0o123', 8))
  43. print(int('0O123', 8))
  44. print(int('0123', 8))
  45. print(int('0b100', 2))
  46. print(int('0B100', 2))
  47. print(int('0100', 2))
  48. print(int(' \t 0o12', 8))
  49. print(int('0o12 \t ', 8))
  50. print(int(b"12", 10))
  51. print(int(b"12"))
  52. def test(value, base):
  53. try:
  54. print(int(value, base))
  55. except ValueError:
  56. print('ValueError')
  57. test('x', 0)
  58. test('1x', 0)
  59. test(' 1x', 0)
  60. test(' 1' + chr(2) + ' ', 0)
  61. test('', 0)
  62. test(' ', 0)
  63. test(' \t\t ', 0)
  64. test('0x', 16)
  65. test('0x', 0)
  66. test('0o', 8)
  67. test('0o', 0)
  68. test('0b', 2)
  69. test('0b', 0)
  70. test('0b2', 2)
  71. test('0o8', 8)
  72. test('0xg', 16)
  73. test('1 1', 16)
  74. test('123', 37)
  75. # check that we don't parse this as a floating point number
  76. print(0x1e+1)
  77. # can't convert list to int
  78. try:
  79. int([])
  80. except TypeError:
  81. print("TypeError")