Tuples Flashcards

1
Q

What is a Tuple

A
  • A tuple is an immutable sequence of Python objects
  • Tuples are also comparable and hashable

ex: t= (‘a’,’b’,’c’,’d’)

time complexity: 0(1)
space complexity O(n)

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

how do we access elements in tuples

A

tuples can be accessed with brackets: []
ex: myTuple[2] or myTuple[-2]
The - int looks from the end of the tuple.

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

How are tuples saved in memory

A

Tuples are saved in memory continuously

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

How do you access a portion of a tuple

A

you can access a portion with slicing the data
ex: myTuple[1:3]
the index three is not included. 1 and 2 are.

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

How to traverse tuple

A

Use a for loop
ex: for i in myTuple:
print(i)
or

for i in range(len(newTuple)):
print(newTuple[i)

time complexity is O(n)

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

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

A

you can use the in operator to test if a value is in the.
ex: ‘x’ in myTuple

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

Different ways to search for a element in tuple

A

You can search with a looping function.

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

how do you concatenate tuples together.

A

you can use the + button to combine tuple
newTuple = tuple1 + tuple2

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

How to repeat the values in a tuple?

A
  • opreator
    ex myTuple * 2: this will take the values and repeat them once.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

how to count how many times a value is in a tuple

A

count method
myTuple.count(‘a’)

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

How to find the index of a value in a tuple

A

find method
ex myTuple.index(4) this will find the first index in the tuple.

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

what function turns a list into a tuple

A

tuple()
ex: tuple([1,2,3,4)

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

When to use tuples and not lists

A
  1. tuples are not multiple
  2. Iterating rhrough a tuple is faster than with list
    3 Tuples that contain immutable elements can be used as a key for a distonary
  3. if you have data that doesn’t change, implementing it as tuple will guarantee that it remains write-protected
  4. Generaly use tuples for heterogeneous (different) data types and lists for homogeneous (similar) data types.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly