Skip to content

Instantly share code, notes, and snippets.

@asaskevich
Created April 28, 2014 15:33
Show Gist options
  • Save asaskevich/11375601 to your computer and use it in GitHub Desktop.
Save asaskevich/11375601 to your computer and use it in GitHub Desktop.
Samples for built-in Python functions
# abs(var)
print(abs(-23))
print(abs(3 + 5j))
# all(var)
print(all([1, 2, 3, 4, 5]))
print(all("string"))
print(all([0, 1, 1]))
# any(var)
print(any([]))
print(any([0, 0, 0, 1, 0, 0, ]))
# ascii(obj)
print(ascii([1, 2, 3, 5]))
print(ascii(10))
print(ascii("string"))
print(ascii({1: "one", 2: "two"}))
# bin(int)
print(bin(123))
print(bin(256))
# bool([x])
print(bool())
print(bool(0))
print(bool("True"))
print(bool(1))
# bytearray([source, [encoding, [errors]]])
print(bytearray())
print(bytearray("string", "UTF-8"))
print(bytearray(3))
# bytes([source, [encoding, [errors]]])
print(bytes())
print(bytes("string", "UTF-8"))
print(bytes(3))
# callable(obj)
print(callable(1))
print(callable("string"))
# chr(i)
print(chr(33))
print(chr(512))
# complex([real, [imagine]])
print(complex())
print(complex(1))
print(complex(2, 3))
print(complex('1+4j'))
print(complex('5j'))
# dir([obj])
print(dir())
print(dir(complex))
# divmod(a, b)
print(divmod(4, 2))
print(divmod(7, 3))
# enumerate(iterable, start = 0)
print(enumerate([1, 2, 3, 4, 5, 6]))
print(enumerate(["one", "two", "three"], 1))
# eval(expr)
print(eval("1+6/3"))
print(eval("1 & 2 ^ 3 - 4"))
print(eval("print('eval()')"))
# filter(function, iterable)
filtered = filter(lambda x: x % 2 == 0, [1, 2, 3, 4, 5])
for item in filtered:
print("[", item, "]")
# float([x])
print(float())
print(float('inf'))
print(float("-inf"))
print(float(" nan"))
print(float(" 213.434235235235235 "))
# format(obj, [spec])
print(format(1 + 2j))
print("{0} + {1} != {2} + {3}".format(1, 2, 3, 4))
# getattr(obj, attr, [default])
print(getattr([1, 2, 3, 4], "len", 0))
print(getattr([1, 2, 3, 4], "__init__"))
# globals()
print(globals())
# hasattr(obj, name)
print(hasattr("string", "__init__"))
print(hasattr("string", "size"))
# hash(obj)
print(hash("string"))
print(hash(10))
print(hash(12345678910111213141516171819))
# help([obj])
# help()
# help(complex)
# hex(int)
print(hex(100))
print(hex(256))
print(hex(12345678910111213141516171819))
# id(obj)
print(id("str"))
print(id(0))
# input([promt])
# print(input())
# print(input("What is your name: "))
# int([x, [base]])
print(int("10"))
print(int("abacaba", 13))
print(int("10000", 2))
# len(obj)
print(len({1: 2, 3: 4, 5: 6}))
print(len("str"))
print(len([1, 2, 3, 4]))
# locals()
print(locals())
# map(function, iterable)
mapped = map(lambda x: x ** 2 - x, [0, 1, 2, 3])
for var in mapped:
print("[", var, "]")
# max()
print(max([4, 3, 2, 1, 5, 6, 7]))
print(max(4, 3, 2, 1, 5, 6, 7))
# min()
print(min([4, 3, 2, 1, 5, 6, 7]))
print(min(4, 3, 2, 1, 5, 6, 7))
# oct(int)
print(oct(10))
print(oct(8))
print(oct(124124))
# ord(ch)
print(ord('a'))
print(ord('\u00ff'))
# pow(x, y, [z])
print(pow(2, 8))
print(pow(2, 8, 3))
# range(start, [stop, [step]])
print(range(3))
print(range(3, 7))
print(range(3, 7, 2))
# repr(obj)
print(repr(1 + 3j))
print(repr([1, 4, 6, 7, 8]))
print(repr({1: 2, 3: 4, 5: 6}))
# round(x, [num])
print(round(0.55))
print(round(0.535353535, 4))
print(round(0.535353535, 3))
# slice(start, [stop, [step]])
print(slice(3))
print(slice(3, 7))
print(slice(3, 7, 2))
# sorted(iterable, [key, [reverse]])
print(sorted([1, 4, 2, 6, 8, 3, 1]))
print(sorted([1, 4, 2, 6, 8, 3, 1], reverse=True))
# str()
print(str(1))
print(str(0o123))
print(str(0b100))
print(str([1, 2, 3, 4]))
# sum()
print(sum([1, 2, 3, 4, 5, 6]))
# type(obj)
print(type("str"))
print(type({1: 2}))
print(type(2))
print(type(1 + 3j))
print(type(range(1, 3)))
# zip(iterable)
zipped = zip([1, 2, 3], [4, 5, 6])
for pair in zipped:
print("{", pair, "}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment