Lists and Boolean Flashcards
What is the syntax for creating a list?
my_list = [1, 2, 3]
.
How to add an item to the end of a list?
Use .append(item)
(e.g., my_list.append(4)
→ [1, 2, 3, 4]
).
How to combine two lists?
Use .extend()
or +
(e.g., [1,2].extend([3,4])
→ [1,2,3,4]
).
How to insert an item at a specific index?
Use .insert(index, item)
(e.g., [1,3].insert(1,2)
→ [1,2,3]
).
How to remove the first occurrence of a value?
Use .remove(value)
(e.g., [1,2,2].remove(2)
→ [1,2]
).
How to remove and return an item by index?
Use .pop(index)
(defaults to last item if no index is provided).
How to sort a list in-place?
Use .sort()
(e.g., [3,1,2].sort()
→ [1,2,3]
).
How to reverse a list in-place?
Use .reverse()
(e.g., [1,2,3].reverse()
→ [3,2,1]
).
What is list slicing syntax?
list[start:end:step]
(e.g., [1,2,3,4][1:3]
→ [2,3]
).
How to create a list with list comprehension?
[x*2 for x in range(3)]
→ [0, 2, 4]
.
What is the difference between [1,2] + [3]
and .append(3)
?
+
creates a new list; .append()
modifies the original.
What is a boolean value?
True
or False
(capitalized).
What values are considered falsy in Python?
0
, ''
, None
, []
, {}
, ()
, False
.
What is the output of bool('Hello')
?
True
(non-empty strings are truthy).
What is the output of bool([])
?
False
(empty lists are falsy).
How to check if a list contains a value?
Use in
(e.g., 2 in [1,2,3]
→ True
).
What does all([True, 1, 'non-empty'])
return?
True
(checks if all elements are truthy).
What does any([False, 0, 'text'])
return?
True
(checks if any element is truthy).
What is short-circuit evaluation?
Stops evaluating expressions early (e.g., False and ...
→ False
).
How to negate a boolean?
Use not
(e.g., not True
→ False
).