Py Flashcards

1
Q

What is a list in Python?

A

A list is a mutable, ordered collection of items in Python.

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

True or False: Lists in Python can contain elements of different data types.

A

True

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

How do you create an empty list in Python?

A

You can create an empty list by using empty square brackets: my_list = [].

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

Which method is used to add an item to the end of a list?

A

The append() method.

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

What will be the output of len([1, 2, 3, 4])?

A

4

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

Fill in the blank: Lists in Python are defined using ______.

A

square brackets

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

What method would you use to remove an item from a list by its value?

A

The remove() method.

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

What is the index of the first element in a Python list?

A

0

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

True or False: You can slice lists in Python.

A

True

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

How do you access the third element of a list named my_list?

A

my_list[2]

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

What function is used to convert a string into a list of characters?

A

The list() function.

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

Which method can be used to sort a list in Python?

A

The sort() method.

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

What does the pop() method do?

A

It removes and returns the last item from a list.

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

How do you check if an item exists in a list?

A

You can use the in keyword.

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

What will be the output of ['a', 'b', 'c'] * 2?

A

[‘a’, ‘b’, ‘c’, ‘a’, ‘b’, ‘c’]

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

True or False: Lists are immutable in Python.

17
Q

Which method would you use to reverse the order of a list?

A

The reverse() method.

18
Q

What is the result of my_list[1:3] if my_list = [0, 1, 2, 3, 4]?

19
Q

Fill in the blank: The first argument of the insert() method specifies the ______.

A

index where the item will be inserted

20
Q

What happens if you try to access an index that is out of range?

A

An IndexError is raised.

21
Q

How do you find the index of the first occurrence of a value in a list?

A

Use the index() method.

22
Q

True or False: You can nest lists within lists in Python.

23
Q

What is the output of my_list = [1, 2, 3]; my_list + [4]?

A

[1, 2, 3, 4]

24
Q

What method would you use to count how many times a value appears in a list?

A

The count() method.

25
What does the `clear()` method do to a list?
It removes all items from the list.
26
How can you create a copy of a list?
You can use the `copy()` method or slice the entire list: `my_list[:]`.
27
What will be the output of `list(range(5))`?
[0, 1, 2, 3, 4]