Conditionals & Tuples Flashcards
What is a ‘tuple’ in Python?
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)
What are the three main places where we will use tuples?
- As keys to dictionaries;
- Passing/returning multiple values to functions
- In assignments
Give an example of using tuples in assignments.
>>> a = 1; b = 2 >>> print(a, b) (1, 2) >>> (a, b) = (b, a) >>> print(a, b) (2, 1)
What is the function we use to cast an object to a Boolean?
bool(obj)
True or false: every type has a unique value for which bool() evaluates to False.
True
Which integer will evaluate to False when cast to bool(int)?
> > > bool(0)
False
Which string will evaluate to False when cast to bool(str)?
> > > bool(“”) # empty string
False
Which float will evaluate to False when cast to bool(float)?
> > > bool(0.0)
False
Which tuple will evaluate to False when cast to bool(tuple)?
> > > bool(()) # empty tuple
False
Which list will evaluate to False when cast to bool(list)?
> > > bool([]) # empty list
False
Which dictionary will evaluate to False when cast to bool(dict)?
> > > bool({}) # empty dict
False