Py Flashcards
What is a list in Python?
A list is a mutable, ordered collection of items in Python.
True or False: Lists in Python can contain elements of different data types.
True
How do you create an empty list in Python?
You can create an empty list by using empty square brackets: my_list = []
.
Which method is used to add an item to the end of a list?
The append()
method.
What will be the output of len([1, 2, 3, 4])
?
4
Fill in the blank: Lists in Python are defined using ______.
square brackets
What method would you use to remove an item from a list by its value?
The remove()
method.
What is the index of the first element in a Python list?
0
True or False: You can slice lists in Python.
True
How do you access the third element of a list named my_list
?
my_list[2]
What function is used to convert a string into a list of characters?
The list()
function.
Which method can be used to sort a list in Python?
The sort()
method.
What does the pop()
method do?
It removes and returns the last item from a list.
How do you check if an item exists in a list?
You can use the in
keyword.
What will be the output of ['a', 'b', 'c'] * 2
?
[‘a’, ‘b’, ‘c’, ‘a’, ‘b’, ‘c’]
True or False: Lists are immutable in Python.
False
Which method would you use to reverse the order of a list?
The reverse()
method.
What is the result of my_list[1:3]
if my_list = [0, 1, 2, 3, 4]
?
[1, 2]
Fill in the blank: The first argument of the insert()
method specifies the ______.
index where the item will be inserted
What happens if you try to access an index that is out of range?
An IndexError
is raised.
How do you find the index of the first occurrence of a value in a list?
Use the index()
method.
True or False: You can nest lists within lists in Python.
True
What is the output of my_list = [1, 2, 3]; my_list + [4]
?
[1, 2, 3, 4]
What method would you use to count how many times a value appears in a list?
The count()
method.