Lecture 6 Flashcards
dictionary with functions, built in lambda methods, type coercion, debugging, handling missing keys in dictionaries, more functions
how do you pass a function thru a dictionary to return a value? (3)
1 define the function
2 define dictionary + key: value syntax –> d={key:add(1, 2)}
filter() method
function, syntax, parameters (2), and what it returns
filters a sequence using a function that tests each element to be true or false.
SYNTAX: filter(function, sequence)
PARAMETERS:
function: function (lambda?) that tests if each element of a sequence is true or not.
sequence: sequence which needs to be filtered, it can be sets, lists, tuples, or containers of any iterators.
Returns: an iterator that is already filtered.
all() function
purpose
syntax
returns
use case
etc
purpose - returns true if all the elements of a given iterable (List, Dictionary, Tuple, set, etc.) are True OR if the iterable is empty. Otherwise it returns False
syntax - all( iterable )
reutrns: boolean value (true/false)
use case: if we want to ensure that user has not entered a False value then we use the all() function
etc - with conditions using FOR keyword:
(all(ele % 2 == 0 for ele in l))
what values correspond to false? (6)
0
None
’’
[]
()
{}
type coercion
the process of automatic or implicit conversion of values from one data type to another. We can coerce a variable to another date type by passing it to a function whose name is identical to the desired data type.
any() function
Return True if any element of the iterable is truthy. If the iterable is empty, return False
sorted() function
Returns a new sorted list from the items in iterable. works on any iterable.
reversed() function
Returns a reverse iterator.
max() function
Return the largest item in an iterable or the largest of two or more arguments. also works with dictionaries
min() function
Return the smallest item in an iterable or the smallest of two or more arguments. works with dictionaries
sum() function
sum(iterable, start)
- Takes an iterable and an optional start.
- Returns the sum of start and the items of an iterable from left to right and returns the total.
- start defaults to 0
- ALL values in iterable must be numbers
round() fuction
returns a floating point number that is a rounded version of the specified number, with the specified number of decimals.
syntax: round(number, digits)
The default number of decimals is 0, meaning that the function will return the nearest integer.
what is an alternative way to round a float to x decimal places?
print(““.format(2.478))
zip() function
use
syntax
returns
use: allows you to aggregate elements from multiple iterables into a single iterable.
syntax: zip(iterator1, iterator2, iterator3 …)
NOTE: if 1 tuple contains more items than the other, the extra items will be ignored.
RETURNS a zip object, which is an iterator of tuples where the items in each passed iterator are paired together by corresponding index places (0 and 0, 1 and 1, etc)
bin() function
use
syntax
returns
used to return the binary representation of a specified integer. A result always starts with the prefix 0b.
bin(int)
returns: binary value
Exceptions : Raises TypeError when a float value is sent in arguments.