Python Tuple Methods Flashcards

1
Q

Returns the number of times a specified value occurs in a tuple

A

count()
.

Syntax
tuple.count(value)

Parameter Values
Parameter Description
value Required. The item to search for

Definition and Usage
The count() method returns the number of times a specified value appears in the tuple.

Example
* Return the number of times the value 5 appears in the tuple:

thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)

x = thistuple.count(5)
print(x)

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

Searches the tuple for a specified value and returns the position of where it was found

A

index()
.

Syntax
tuple.index(value)

Parameter Values
Parameter Description
value Required. The item to search for

Definition and Usage
* The index() method finds the first occurrence of the specified value.

  • The index() method raises an exception if the value is not found.

Example
Search for the first occurrence of the value 8, and return its position:

thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)

x = thistuple.index(8)
print(x)

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

How to create a tuple?

A

thistuple = (“apple”, “banana”, “cherry”, “apple”, “cherry”)

  • Tuples are written with round brackets.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a Tuple?

A
  • Tuples are used to store multiple items in a single variable.
  • Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage.
  • A tuple is a collection which is ordered and unchangeable.
  • Tuples are written with round brackets.
  • Allows Duplicates. Since tuples are indexed, they can have items with the same value.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly