Lecture 6 Flashcards

dictionary with functions, built in lambda methods, type coercion, debugging, handling missing keys in dictionaries, more functions

1
Q

how do you pass a function thru a dictionary to return a value? (3)

A

1 define the function
2 define dictionary + key: value syntax –> d={key:add(1, 2)}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

filter() method

function, syntax, parameters (2), and what it returns

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

all() function

purpose
syntax
returns
use case
etc

A

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))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what values correspond to false? (6)

A

0

None

’’

[]

()

{}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

type coercion

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

any() function

A

Return True if any element of the iterable is truthy. If the iterable is empty, return False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

sorted() function

A

Returns a new sorted list from the items in iterable. works on any iterable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

reversed() function

A

Returns a reverse iterator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

max() function

A

Return the largest item in an iterable or the largest of two or more arguments. also works with dictionaries

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

min() function

A

Return the smallest item in an iterable or the smallest of two or more arguments. works with dictionaries

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

sum() function

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

round() fuction

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what is an alternative way to round a float to x decimal places?

A

print(““.format(2.478))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

zip() function

use
syntax
returns

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

bin() function

use
syntax
returns

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

iter() function

A

used to return an iterator object. It creates an object which can be iterated one element at a time.

Syntax : iter(obj, sentinel)

Parameters : 

    obj : Object which has to be converted to iterable ( usually an iterator ).
    sentinel : value used to represent end of sequence.

Returns : Iterator object
17
Q

what are the 3 properties of iterators?

A

The iteration object remembers the iteration count via the internal count variable.

Once the iteration is complete, it raises a StopIteration exception and the iteration count cannot be reassigned to 0.

Therefore, it can be used to traverse the container ONLY once. so if you try again it will throw error

18
Q

chr() method

A

The chr() function returns the character that represents the specified unicode. Convert back to unicode with the ord() function.

19
Q

enumerate()

A

adds a counter to an iterable and returns it in the form of an enumerating object. This enumerated object can then be used directly for loops or converted into a list of tuples using the list() function.

Syntax: enumerate(iterable, start=0)

Parameters:

Iterable: any object that supports iteration
Start: the index value from which the counter is to be started, by default it is 0

Return: Returns an iterator with index and element pairs from the original iterable, can be typecast into a list

20
Q

syntax error

A

Occurs when Python encounters incorrect syntax (something it doesn’t parse).

Usually due to typos or not knowing Python well enough

21
Q

NameError

A

This occurs when a variable is not defined, i.e. it hasn’t been assigned

22
Q

TypeError

A

Occurs when:
1. An operation or function is applied to the wrong type
2. Python cannot interpret an operation on two data types

23
Q

IndexError

A

Occurs when you try to access an element in a list using an invalid index (i.e. one that is outside the range of the list or string)

24
Q

ValueError

A

This occurs when a built-in operation or function receives an argument that has the right type but an inappropriate value:

int(“foo”)

25
Q

KeyError

A

This occurs when a dictionary does not have a specific key:

26
Q

get() method

A

syntax : Dict.get(key, default=None)

Parameters:

key: The key name of the item you want to return the value from
Value: (Optional) Value to be returned if the key is not found. The default value is None.

Returns: Returns the value of the item with the specified key or the default value.

27
Q
A