Chapter 7 Flashcards

(39 cards)

1
Q

What is a sequence in Python?

A

An object that contains multiple items of data stored one after another (e.g., lists, tuples)

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

What is the difference between a list and a tuple?

A

Lists are mutable; tuples are immutable

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

What is a list?

A

An object containing multiple data items, called elements, defined with brackets: [item1, item2, …]

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

What is a repetition operator for lists?

A
  • operator, which repeats a list n times: list * n
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you iterate over a list?

A

With a for loop: for x in list:

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

What is an index in a list?

A

A number specifying the position of an element; first element is at index 0

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

What do negative indexes in lists represent?

A

Positions relative to the end of the list; -1 is the last element

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

What does the len() function return for a list?

A

The number of elements in the list

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

What happens if you use an invalid index?

A

An IndexError exception is raised

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

Are lists mutable?

A

Yes, list elements can be changed with assignment

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

How do you concatenate two lists?

A

Using + or +=

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

What is list slicing?

A

Extracting a span of items: list[start:end], up to but not including end

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

What does the in operator do with lists?

A

Tests whether an item is in a list; returns True or False

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

What does the not in operator do with lists?

A

Tests whether an item is not in a list

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

What does append(item) do?

A

Adds an item to the end of a list

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

What does count(item) do?

A

Returns the number of times an item appears in a list

17
Q

What does index(item) do?

A

Returns the index of the first occurrence of an item; raises ValueError if not found

18
Q

What does insert(index, item) do?

A

Inserts an item at a specific index

19
Q

What does sort() do for lists?

A

Sorts elements in ascending order

20
Q

What does remove(item) do?

A

Removes the first occurrence of an item

21
Q

What does reverse() do for lists?

A

Reverses the order of list elements

22
Q

What does the del statement do for lists?

A

Removes an element at a specified index: del list[i]

23
Q

What do sum(), min(), and max() do for lists?

A

Return the total, the lowest value, and the highest value in a numeric list

24
Q

How do you properly copy a list?

A

Create a new list and copy elements using a loop or concatenation

25
How do you calculate the average of numbers in a list?
Find the sum of values and divide by len(list)
26
What does writelines() do?
Saves the contents of a list to a file (does not add \n automatically)
27
What does readlines() do?
Reads data from a file into a list
28
What is a list comprehension?
A concise expression that creates a new list by iterating over an existing one
29
Example of list comprehension?
[item**2 for item in list1] creates a list of squared values
30
How do you add conditions to a list comprehension?
Include an if clause: [item for item in list1 if item < 10]
31
What is a two-dimensional list?
A list containing other lists, often thought of in rows and columns
32
How do you access elements in a two-dimensional list?
With two indexes, e.g., list[row][col]
33
What is a tuple?
An immutable sequence; once created, it cannot be changed
34
How do you create a tuple?
(item1, item2, …) or (item1,) for a single element
35
What operations do tuples support?
Indexing, slicing, len, min, max, in, +, *, and some methods like index()
36
What methods are not supported by tuples?
append, remove, insert, reverse, and sort
37
Can a tuple hold mutable objects?
Yes, and the mutable object’s contents can still change
38
Why use tuples?
Faster than lists, provide safety, and required in some operations
39
How do you convert between lists and tuples?
Use list() and tuple() functions