WEEK 2, deel 2 Flashcards

1
Q

What is a list in Python?

A

A list is a data structure that stores multiple values in an ordered sequence.

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

What happens if you try to access an index that doesn’t exist?

A

Python raises an IndexError.

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

How do negative indexes work in lists?

A

-1 refers to the last item.
-2 refers to the second-last item.

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

spam = [[‘cat’, ‘bat’], [10, 20, 30, 40, 50]]

print(spam[0])
print(spam[1][4])

A

First inner list: cat bat
# 5th item in second list: 50

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

What kinds of values can be in a list?

A

Lists can contain any type of values

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

What is the difference between indexing and slicing?

A

Indexing (spam[0]) gets one item.

Slicing (spam[1:3]) gets a range of items.

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

Syntax of Slicing

A

list[start:stop]

start: The index where slicing begins (included).
stop: The index where slicing ends (not included).

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

How do you change a value in a list?

A

Assign a new value using indexing.

spam = [‘cat’, ‘bat’, ‘rat’, ‘elephant’]
spam[1] = ‘dog’ # Change ‘bat’ to ‘dog’
print(spam)

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

How do you add or remove items in a list?

A

Use .append(value) to add an item.
Use .remove(value) to delete an item.

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

How do you check if a value is in a list?

A

Use the in keyword.

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

How do you sort a list in Python?

A

Use .sort() for ascending order, .sort(reverse=True) for descending order.

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

What is the safest way to copy a list?

A

Use .copy() to avoid modifying the original list.

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

spam = [‘cat’, ‘bat’, ‘rat’, ‘elephant’]
print(spam[:2])
print(spam[1:])

A

First 2 items: cat, bat

From index 1 to the end: bat, rat, elephant

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

what happens if you concatenation and replication lists

A

concatenation: +, joints two lists

replication: *, repeats a list

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

what’s the relationship between range(len(supplies)) and enumerate()

A

enumerate() automatically gives us both: the index and the value.

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

hat are augmented assignment operators, and how do they work?

A

They are shorthand for modifying a variable.

spam += 1 spam = spam + 1
spam -= 1 spam = spam - 1
spam *= 1 spam = spam * 1
spam /= 1 spam = spam / 1
spam %= 1 spam = spam % 1

17
Q

What is the difference between mutable and immutable data types?

A

Mutable → Can be changed (e.g., lists).

Immutable → Cannot be changed (e.g., strings, tuples).

18
Q

name = “Zophie a cat”
new_name = name[0:7] + “the” + name[8:12]
print(new_name)

A

Zophie the cat

19
Q

How is a tuple different from a list?

A

Tuples use () instead of [].
Tuples are immutable (cannot change values).

20
Q

spam = [0, 1, 2, 3, 4, 5]
cheese = spam # Both refer to the same list
cheese[1] = “Hello!”
print(spam) # spam is also changed

A

[0, ‘Hello!’, 2, 3, 4, 5]