int1.py 1.54 KB
Newer Older
1
2
3
print(int(False))
print(int(True))

xyb's avatar
xyb committed
4
5
6
7
8
9
10
11
12
13
14
print(int(0))
print(int(1))
print(int(+1))
print(int(-1))

print(int('0'))
print(int('+0'))
print(int('-0'))
print(int('1'))
print(int('+1'))
print(int('-1'))
xyb's avatar
xyb committed
15
print(int('01'))
xyb's avatar
xyb committed
16
17
18
19
print(int('9'))
print(int('10'))
print(int('+10'))
print(int('-10'))
xyb's avatar
xyb committed
20
21
print(int('12'))
print(int('-12'))
xyb's avatar
xyb committed
22
23
24
25
26
27
28
29
30
print(int('99'))
print(int('100'))
print(int('314'))
print(int(' 314'))
print(int('314 '))
print(int('  \t\t  314  \t\t  '))
print(int('  1  '))
print(int(' -3 '))

xyb's avatar
xyb committed
31
32
33
34
35
36
37
38
print(int('0', 10))
print(int('1', 10))
print(int(' \t 1 \t ', 10))
print(int('11', 10))
print(int('11', 16))
print(int('11', 8))
print(int('11', 2))
print(int('11', 36))
xyb's avatar
xyb committed
39
print(int('0o123', 0))
xyb's avatar
xyb committed
40
print(int('8388607'))
xyb's avatar
xyb committed
41
42
print(int('0x123', 16))
print(int('0X123', 16))
43
print(int('0A', 16))
xyb's avatar
xyb committed
44
print(int('0o123', 8))
xyb's avatar
xyb committed
45
print(int('0O123', 8))
xyb's avatar
xyb committed
46
47
print(int('0123', 8))
print(int('0b100', 2))
xyb's avatar
xyb committed
48
print(int('0B100', 2))
xyb's avatar
xyb committed
49
50
51
print(int('0100', 2))
print(int(' \t 0o12', 8))
print(int('0o12  \t  ', 8))
52
print(int(b"12", 10))
Dave Hylands's avatar
Dave Hylands committed
53
print(int(b"12"))
xyb's avatar
xyb committed
54
55
56
57
58
59
60
61
62


def test(value, base):
    try:
        print(int(value, base))
    except ValueError:
        print('ValueError')


xyb's avatar
xyb committed
63
64
test('x', 0)
test('1x', 0)
xyb's avatar
xyb committed
65
test('  1x', 0)
xyb's avatar
xyb committed
66
test('  1' + chr(2) + '  ', 0)
xyb's avatar
xyb committed
67
68
69
70
71
72
73
74
75
76
77
78
79
test('', 0)
test(' ', 0)
test('  \t\t  ', 0)
test("\u0200", 0)
test('0x', 16)
test('0x', 0)
test('0o', 8)
test('0o', 0)
test('0b', 2)
test('0b', 0)
test('0b2', 2)
test('0o8', 8)
test('0xg', 16)
xyb's avatar
xyb committed
80
test('1 1', 16)
81
test('123', 37)
82
83
84

# check that we don't parse this as a floating point number
print(0x1e+1)
85
86
87
88
89
90

# can't convert list to int
try:
    int([])
except TypeError:
    print("TypeError")