Conditionals & Tuples Flashcards

1
Q

What is a ‘tuple’ in Python?

A
An immutable sequence of comma-separated immutable objects enclosed in (parenthesis)
>>> a = ('a', 123, [4,5,6])
>>> a[0]
'a'
>>> a[0:2]
('a', 123)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the three main places where we will use tuples?

A
  1. As keys to dictionaries;
  2. Passing/returning multiple values to functions
  3. In assignments
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Give an example of using tuples in assignments.

A
>>> a = 1; b = 2
>>> print(a, b)
(1, 2)
>>> (a, b) = (b, a)
>>> print(a, b)
(2, 1)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the function we use to cast an object to a Boolean?

A

bool(obj)

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

True or false: every type has a unique value for which bool() evaluates to False.

A

True

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

Which integer will evaluate to False when cast to bool(int)?

A

> > > bool(0)

False

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

Which string will evaluate to False when cast to bool(str)?

A

> > > bool(“”) # empty string

False

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

Which float will evaluate to False when cast to bool(float)?

A

> > > bool(0.0)

False

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

Which tuple will evaluate to False when cast to bool(tuple)?

A

> > > bool(()) # empty tuple

False

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

Which list will evaluate to False when cast to bool(list)?

A

> > > bool([]) # empty list

False

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

Which dictionary will evaluate to False when cast to bool(dict)?

A

> > > bool({}) # empty dict

False

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