Built-ins

Types and classes


In [ ]:
type(True), type(1), type (1.), type('1'), type([]), type(()), type({}), type({1})

In [ ]:
x = 1.
type(x)

In [ ]:
type(bool), type(int), type(float), type(str), type(list), type(type)

In [ ]:
isinstance(1, int), isinstance(1, float), isinstance({2}, set), isinstance({}, set)

In [ ]:
issubclass(bool, int), issubclass(int, float)

Literal representations


In [ ]:
bool(True), bool(-17.8), bool('17'), bool([]), bool({}), bool(None), bool('')

In [ ]:
int('17'), int('10001', 2), int('21', 8), int('15', 12), int('11', 16), int('h', 22)

In [ ]:
int(True), int(17), int(-17), int(0b10001), int(0o21), int(0x11), int(17.8)

In [ ]:
bin(True), bin(17), bin(-17), bin(0b10001), bin(0o21), bin(0x11)

In [ ]:
oct(True), oct(17), oct(-17), oct(0b10001), oct(0o21), oct(0x11)

In [ ]:
hex(True), hex(17), hex(-17), hex(0b10001), hex(0o21), hex(0x11)

In [ ]:
float(False), float(17), float(17.), float(17e-1), float(0.17E2), float('170E-1')

In [ ]:
print(complex(0), complex(1.5), complex(4.5, -7.5))
print(complex(0).conjugate(), complex(1.5).conjugate(), complex(4.5, -7.5).conjugate())
print(complex(0).real, complex(1.5).real, complex(4.5, -7.5).real)
print(complex(0).imag, complex(1.5).imag, complex(4.5, -7.5).imag)

Operations on numbers


In [ ]:
abs(-3.8), abs(-2), abs(3.8)

In [ ]:
print(round(-3.6), round(-3.5), round(-3.4), round(3.4), round(3.5), round(3.6))
print(round(-3.1235, 3), round(-3.123456, 4), round(3.123456, 4), round(3.1235, 3))

In [ ]:
divmod(13, 5), divmod(-13., 5), divmod(13., -5.), divmod(-13, -5.), divmod(3.5, 2)

In [ ]:
print(pow(2, 3), pow(-2., -1), pow(4, -0.5), pow(-1, 0.5), pow(-3.8, 0), pow(1j, 1j))
print(pow(-2, 3, 3), pow(-2, 3, 5), pow(2, 3, 3), pow(2, 3, 5))

Strings


In [ ]:
print(ord('c'), ord('\xf7'), ord('•'), ord('\u2603'))
print(chr(99), chr(247), chr(8226), chr(9731))

In [ ]:
ascii('Ça me tient ∞ment à cœur\t')

In [ ]:
repr('A string'), str('A string')

In [ ]:
a = 2; b = 4.5
eval('(a + 3.5) * (b + 5.)')

In [ ]:
message = input('Input your message: ')
print('Your message is:', message)

Creating and processing iteratables


In [ ]:
print(tuple(range(4)), tuple(range(4, 8)), tuple(range(4, 16, 3)))
print(tuple(range(-8)), tuple(range(-8, -4)), tuple(range(-16, -8, -3)))
print()

print(range(2, 8, 2).count(4), range(2, 8, 2).count(5), range(2, 8, 2).index(4))

In [ ]:
print(list(enumerate({10, 15, 25, 40})))
print(dict(enumerate((10, 15, 25, 40), 3)))

In [ ]:
print(list(zip([1, 2, 3, 4], [11, 12, 13], [21, 22, 23, 24, 25], [31, 32, 33])))
print(list(zip(*zip([1, 2, 3, 4], [11, 12, 13], [21, 22, 23, 24, 25], [31, 32, 33]))))

In [ ]:
print(list(map(sorted,
               [(1, 2, 3), (7, 5, 4, 6, 8), (10, 9), (11,)])))
print(set(map(int.__add__,
              [1, 2, 3, 4, 5], [11, 12, 13])))
print(dict(map(lambda x: (x, 2 * x),
               (0, 1, 2, 3, 4, 5))))
print(tuple(map(lambda x, y, z: len({x, y, z}) == 2,
                [1, 20, 30, -4, 5, 60], [-1, 20, 31, 4, 5], [1, 20, 32, 4, -5, 61, 70])))

In [ ]:
print(list(filter(str.isupper,
                 {'A': 1, 'b': 2, 'c': 3, 'D': 4, 'E': 5})))
print(tuple(filter(lambda x: x < 10,
                [-1, 20, -3, -4, 50, 60])))

In [ ]:
print(sum([3, 1, 7, 5]))
print(sum({3: 1, 1: 1, 7: 2, 5: 3}))

In [ ]:
print(min((3, 1, 7, 5)))
# min() accepts also an arbitrary number of arguments
print(min(3, 1, 7, 5))

In [ ]:
print(max({3 : 10, 1: 10, 7: 10, 5: 10}))
# max() accepts also an arbitrary number of arguments
print(max(3, 1, 7, 5))

In [ ]:
sorted([2, 1, 3, 4, 0])

In [ ]:
list(reversed((2, 1, 3, 4, 0)))

In [ ]:
print(any([0, 0, 0, 0, 0]))
print(any((1, 0, 0, 1, 0))

In [ ]:
print(all({5, 2, 3, 1, 4}))
print(all({5: 1, 2: 1, 3: 1, 0: 1, 4: 4}))