Python Data Science Tool Box -1 Flashcards
Good to Remember : Tuples
Note that the return statement return x, y has the same result as return (x, y): the former actually packs x and y into a tuple under the hood!
How would you write a lambda function add_bangs that adds three exclamation points ‘!!!’ to the end of a string a?
How would you call add_bangs with the argument ‘hello’?
The lambda function definition is:
add_bangs = (lambda a: a + ‘!!!’),
and the function call is: add_bangs(‘hello’).
nums = [2, 4, 6, 8, 10]
result = map(lambda a: a ** 2, nums)
What does this piece of lambda code do?
You can see here that a lambda function, which raises a value a to the power of 2, is passed to map() alongside a list of numbers, nums. The map object that results from the call to map() is stored in result.
Whar does the map() do?
map() applies a function over an object, such as a list.
What does the filter () do?
The function filter() offers a way to filter out elements from a list that don’t satisfy certain criteria.
What are the 2 ways of raising an exception?
try : except:
and using raise