Python Basics IV Functions, Map, and Lambda Flashcards
How do you create a function in Python
use def, a colon, then indent.
What is map used for?
A code convenience, it makes it easy to apply a function to all the elements of a list.
What is lamda for?
To streamline code, you basically need function, but it’s a one shot deal, so you woud like to avoid the overhead required to construct a proper function.
Can you write a function on a single line. (So there would be no indenting)?
Yes
Map applies a function (or lambda expression) to every element in a list. FILTER works similarly, but applies a filter to every element in a sequence. Using filter, write an expression that would only return the even number from a list of numbers.?
What does the split method do?
You can apply it to a string, it will create a list where each element between the blanks shows.
Suppose, for example we wanted to extract the tag in a tweet. It would be done as shown.
What are the 2 important dictionary methods?
.keys and .values
A dictionary is a collection of key/value pairs.
Does a Python list have a Push and Pop method?
No, it has Append and Pop methods. Those methods have an optional integer argument. The default value is 0.
What method returns a boolean to determine if an item is in a list.
The “in” method
What is tuple unpacking
It is a shortcut method for extracting items from tuples when you have a list of tuples.