Python Builtinfunctions
Python Builtinfunctions
print(abs(-5)) # Output: 5
all(iterable): Returns True if all elements of the iterable are true.
print(all([1, 2, 3])) # Output: True
any(iterable): Returns True if any element of the iterable is true.
print(any([0, False, 5])) # Output: True
ascii(object): Returns a string containing a printable
representation of an object.
print(ascii('µ')) # Output: '\\xb5'
bin(x): Converts an integer number to a binary string.
print(bin(3)) # Output: '0b11'
bool(x): Converts a value to a Boolean, returning either True or
False.
print(bool(0)) # Output: False
bytearray(source): Returns a mutable array of bytes.
print(bytearray('hello', 'utf-8')) # Output:
bytearray(b'hello')
callable(object): Checks if the object appears callable.
print(callable(len)) # Output: True
chr(x): Returns a string representing a character whose Unicode code
point is the integer x.
print(chr(97)) # Output: 'a'
dict(): Creates a new dictionary.
print(dict(a=1, b=2)) # Output: {'a': 1, 'b': 2}
enumerate(iterable): Returns an enumerate object.
print(list(enumerate(['a', 'b']))) # Output: [(0,
'a'), (1, 'b')]
filter(function, iterable): Constructs an iterator from
elements of iterable for which function returns true.
print(list(filter(lambda x: x > 0, [-1, 0, 1]))) #
Output: [1]
hex(x): Converts an integer to a lowercase hexadecimal string.
print(hex(255)) # Output: '0xff'
input(prompt): Reads a line from input, converts it to a string.
# name = input('Enter your name: ')
int(x): Converts a number or string to an integer.
print(int('10')) # Output: 10
len(s): Returns the length of an object.
print(len('hello')) # Output: 5
map(function, iterable): Applies function to every item of
iterable and returns a list of the results.
print(list(map(lambda x: x * 2, [1, 2, 3]))) #
Output: [2, 4, 6]
max(iterable): Returns the largest item in an iterable.
print(max([1, 2, 3])) # Output: 3
min(iterable): Returns the smallest item in an iterable.
print(min([1, 2, 3])) # Output: 1
next(iterator): Retrieves the next item from the iterator.
it = iter([1, 2, 3])
print(next(it)) # Output: 1
ord(c): Converts a character to its integer representation.
print(ord('a')) # Output: 97
pow(x, y): Returns x to the power of y.
print(pow(2, 3)) # Output: 8
range(start, stop, step): Generates a sequence of numbers.
print(list(range(0, 5, 1))) # Output: [0, 1, 2, 3,
4]
reversed(seq): Returns a reversed iterator.
print(list(reversed([1, 2, 3]))) # Output: [3, 2,
1]
set(iterable): Creates a new set object.
print(set([1, 2, 2, 3])) # Output: {1, 2, 3}
sorted(iterable): Returns a sorted list from the items in
iterable.
print(sorted([3, 1, 2])) # Output: [1, 2, 3]
str(object): Converts an object to a string.
print(str(123)) # Output: '123'
sum(iterable): Sums the items of an iterable.
print(sum([1, 2, 3])) # Output: 6
type(object): Returns the type of an object.
print(type(123)) # Output: <class 'int'>
zip(*iterables): Returns an iterator of tuples.
print(list(zip([1, 2], ['a', 'b']))) # Output:
[(1, 'a'), (2, 'b')]