#2 – Data Types Flashcards

(17 cards)

1
Q

When should you use a tuple in Python?

A

Use a tuple when your data is fixed and should not change.
coordinates = (10, 20)

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

When should you use a list in Python?

A

Use a list when you need an ordered, mutable collection.
fruits = ['apple', 'banana']

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

When should you use a set in Python?

A

Use a set when you need a collection of unique, unordered items.
unique_items = set([1, 2, 2, 3])

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

When should you use a dictionary in Python?

A

Use a dictionary when you want to associate keys with values.
person = {'name': 'Alice', 'age': 30}

Example:

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

What is a namedtuple and when should you use it?

A

A namedtuple is like a tuple, but with named fields. It makes code more readable.

from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])
p = Point(1, 2)
print(p.x)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a defaultdict and when should you use it?

A

A defaultdict is like a normal dictionary, but it automatically creates default values for missing keys.

from collections import defaultdict
d = defaultdict(int)
d['a'] += 1
print(d['a'])  
# 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you get the length of a list?

A

Use the len() function.
len(my_list)

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

How do you get the last element of a list?

A

Use negative indexing.

my_list = [10, 20, 30] print(my_list[-1])  # 30
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you get the length of a list?

A

Use the len() function.
len(my_list)

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

How do you add an element to the end of a list?

A

Use .append().

my_list.append(100)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you add multiple elements to a list?

A

Use .extend().

my_list.extend([4, 5])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you insert an element at a specific index?

A

Use .insert(index, value).

my_list.insert(1, 'x')
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you remove an element by value?

A

Use .remove(value).

my_list.remove('x')
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you remove an element by index?

A

Use del or .pop().

del my_list[2]
or
my_list.pop(2)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you get a sublist (slice)?

A

Use slicing syntax.

my_list[1:3]  # from index 1 to 2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you reverse a list (non-destructive)?

A

Use slicing with step -1.

reversed_list = my_list[::-1]
17
Q

How do you check if an element is in a list?

A

Use the ‘in’ keyword. 'if 10 in my_list:'