Built-in Functions Flashcards
what does cmp() do?
cmp() is a n-built function in Python, it is used to compare two objects and returns value according to the given values. It does not return ‘true’ or ‘false’ instead of ‘true’ / ‘false’, it returns negative, zero or positive value based on the given input.
Syntax: cmp(obj1, obj2)
Here, cmp() will return negative (-1) if obj1obj2.
What are the different outputs of cmp()?
Syntax: cmp(obj1, obj2)
Here, cmp() will return negative (-1) if obj1obj2.
what does bit_length() do?
Return the number of bits necessary to represent an integer in binary, excluding the sign and leading zeros
what would bit_length(37) return?
>>> n = -37 >>> bin(n) '-0b100101' >>> n.bit_length() 6
what does enumerate() do?
enumerate(iterable, start=0)
Enumerate() method adds a counter to an iterable and returns it in a form of enumerate object. This enumerate object can then be used directly in for loops or be converted into a list of tuples using list() method.
why is enumerate() helpful?
because it allows you to grab indexes and/or have a counter while going through an iterable.
What is the map() function do?
map(fun, iter)
map() function returns a map object(which is an iterator) of the results after applying the given function to each item of a given iterable (list, tuple etc.)
When should you use the map() function?
Whenever you want to get an output of a function being run on an iterable.
What date function gives you a date object formatted with yyyy-mm-dd?
datetime.date.today()