Python Tuple Methods Flashcards

(26 cards)

1
Q

What does the count() method do in a tuple?

A

Returns the number of occurrences of a specified value in the tuple.

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

What is the purpose of the index() method?

A

Returns the first index of a specified value in the tuple.

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

True or false: Tuples are mutable in Python.

A

FALSE

Tuples are immutable, meaning their elements cannot be changed after creation.

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

Fill in the blank: The len() function returns the _______ of a tuple.

A

number of elements

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

What is a tuple?

A

An ordered collection of elements that is immutable.

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

What does tuple packing mean?

A

Assigning multiple values to a tuple in a single statement.

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

What does tuple unpacking allow you to do?

A

Assign elements of a tuple to multiple variables in one go.

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

True or false: Tuples can contain duplicate elements.

A

TRUE

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

What is the result of t = (1, 2, 3) and t.index(2)?

A

Returns 1, the index of the first occurrence of 2.

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

Fill in the blank: A tuple can be created using _______ parentheses.

A

round

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

What happens if you try to modify a tuple?

A

It raises a TypeError since tuples are immutable.

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

Define nested tuples.

A

Tuples that contain other tuples as their elements.

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

What does t.count(1) return if t = (1, 2, 1)?

A

Returns 2, as 1 appears twice.

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

True or false: You can concatenate two tuples.

A

TRUE

Use the + operator to concatenate tuples.

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

What is the output of (1, 2) + (3, 4)?

A

Returns (1, 2, 3, 4).

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

Fill in the blank: The slice operation can be used on tuples to get _______.

17
Q

What does t[::-1] do if t = (1, 2, 3)?

A

Reverses the tuple, returning (3, 2, 1).

18
Q

What is the difference between a list and a tuple?

A

Lists are mutable; tuples are immutable.

19
Q

What is the output of len((1, 2, 3))?

A

Returns 3, the number of elements in the tuple.

20
Q

True or false: Tuples can hold mixed data types.

21
Q

What does the * operator do with tuples?

A

Repeats the tuple elements a specified number of times.

22
Q

What is the result of (1, 2) * 3?

A

Returns (1, 2, 1, 2, 1, 2).

23
Q

Fill in the blank: A tuple with one element must include a _______ after the element.

24
Q

What does t = (1,) create?

A

A tuple with a single element, 1.

25
What is a **tuple comprehension**?
A concise way to create tuples using a generator expression.
26
True or false: Tuples can be used as **dictionary keys**.
TRUE ## Footnote Since tuples are immutable, they can serve as keys in dictionaries.