Built In Functions

Lambda

1. Describe lambda() function.
Lambda functions creates unnamed functions. Hence, they are called anonymous functions because these functions do not have a name.
2. Explain the syntax components lambda <args>: <expression>.
  • args are the arguments to put in the expression.
  • expression is evaluated and returned when lambda function is called.
3. List the limitations of lambda().
  • Limited to a single expression
  • No assignments
  • No annotations
4. Convert lambda x: x*2 into a normal function.
def func(x):
	return x*2
5. Provide a general syntax to perform if-else statement.
lambda <args> : <return value> if <condition> else <return value>
6. Provide a general syntax to perform if-elif-else statement.
lambda <args> : <return value> if <condition > else ( <return value > if <condition> else <return value>)

Range

1. Describe range() function.
The range function returns a sequence of numbers.
2. Explain the range function parameters range(start, stop, step).

start: The sequence of numbers will start from the specified value.

end: The sequence will stop before the specified value.

step: The sequence generates the number based on the step-size.

3. What does range(0,5) returns when we iterate over it?
0,1,2,3,4
4. What does range(5,8) returns when we iterate over it?
5,6,7
5. What does range(0,10,2) returns when we iterate over it?
0,2,4,6,8

Map

1. Describe map() function.
The map function is used to apply a specified function to all items in an iterable (such as a list) and return an iterator containing the modification.
2. Explain the parameters map(func, *iterable).
  • *iterable: A variable number of iterable objects.
  • func: A function that will apply to the items in the iterable.
3. Use map() with a lambda function to square each number in the list numbers.
map(lambda x: x**2, numbers)

Filter

1. Describe filter() function.
The filter function returns an iterator that contains all the elements that satisfy a given condition by the function.
2. Explain the parameters filter(func, iterable).
  • func: A function that takes a single argument
  • iterable: A single iterable
3. Given lis = [1,2,3,4,5,6,7,8,9], filter the even numbers using a lambda function and store into a list.
list(filter(lambda x: x%2 == 0, lis))

List Comprehension

1. Describe list comprehensions.
List comprehensions provide a concise and expressive way to create lists.
2. Explain the syntax components in [<expression> for <item> in <iterable> if <condition>].
  • expression: The expression to be evaluated for each item in the iterable.
  • item: The variable representing each element in the iterable.
  • iterable: The sequence of elements (e.g., a list, tuple, or string) you are iterating over.
  • condition: (optional): An optional condition that determines whether the item should be included in the new list.
3. What should we see list comprehensions as?
We should see list comprehension as functions, with its own local scope.
4. Use a list comprehension to filter out even numbers in the number list numbers.
numbers = [1, 2, 3, 4, 5]
evens = [num for num in numbers if num % 2 == 0]
print(evens)
# Output: [2, 4]
5. Use a list comprehension to square all the numbers in the number list numbers.
numbers = [1, 2, 3, 4, 5]
squares = [num**2 for num in numbers]
print(squares)
# Output: [1, 4, 9, 16, 25]

Zip

1. Describe zip() function.
The function returns an iterator that combines elements from multiple iterables into tuples.
2. Explain the parameters zip(*iterable).
*iterable: A variable number of iterable objects.
3. Given names = [’bob’, ’alice’] and ages = [10, 15] combine both iterables together and store into a list.
list(zip(names, ages))
4. What are zip()’s common use cases?
  • Creating dictionaries.
  • Combining data from different sources.
Last updated on 23 Aug 2024