3. Introducing Tuples (13m) Flashcards

1
Q

what is the difference between lists and tuples in python?

A

In Python, both lists and tuples are used to store collections of items, but they have some fundamental differences in terms of mutability, syntax, and usage. Here are the main differences between lists and tuples:

  1. Mutability: Lists are mutable, which means their elements can be modified after creation. You can add, remove, or modify elements in a list. On the other hand, tuples are immutable, meaning that their elements cannot be changed once the tuple is created. You cannot add, remove, or modify elements in a tuple.
  2. Syntax: Lists are defined using square brackets ([]), while tuples are defined using parentheses (()). For example:
    ~~~
    my_list = [1, 2, 3] # This is a list
    my_tuple = (1, 2, 3) # This is a tuple
    ~~~
  3. Usage: Lists are typically used for collections of items where the order and mutability of elements matter. They are commonly used for sequences that can be modified, such as storing a list of names or a series of steps. Tuples, on the other hand, are often used for collections of items where immutability and the order of elements are important. Tuples are useful for representing fixed collections of values, such as coordinates or database records.
  4. Performance: Tuples are generally more lightweight and faster to access than lists because of their immutability. Lists, being mutable, require more memory and operations to manage changes. If you have a collection of items that won’t change, using tuples can be more efficient.

Here’s an example to illustrate the differences:
~~~
my_list = [1, 2, 3] # A list
my_tuple = (1, 2, 3) # A tuple

my_list[0] = 4 # Modifying the first element of the list
# my_list is now [4, 2, 3]

my_tuple[0] = 4 # This will raise a TypeError since tuples are immutable

my_list.append(4) # Adding an element to the list
# my_list is now [4, 2, 3, 4]

Tuples cannot be modified, so there’s no append() method available
~~~

In summary, use lists when you have a collection of items that needs to be modified, and use tuples when you have a collection of items that should remain fixed.

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

Exploring Tuple Syntax

A

Now that you know a little bit more about the difference between a tuple and a list, let’s take a look at one.

Here we have a python list:
~~~
# A list
groceries = [‘apples’, ‘oranges’, ‘lettuce’, ‘cheddar cheese’]
~~~

To convert this list to a tuple, we can do one of two things: remove the square brackets entirely, or replace them with parentheses.
~~~
# A tuple!
groceries = ‘apples’, ‘oranges’, ‘lettuce’, ‘cheddar cheese’
~~~

# A tuple!
groceries = ('apples', 'oranges', 'lettuce', 'cheddar cheese')

Both syntaxes are valid - the truth is, it is the comma that makes something a tuple, not the parens. The parens just add some helpful visibility and readability.

Unlike lists, however, the requirement of separating each item by a comma also applies to tuples with only one item, whether or not you add the parentheses.

If a tuple only has one element, that element must be followed by a comma, otherwise the python interpreter will assume that you’re simply referencing the item in question, and not creating a tuple.

Code like the following will not produce a one-item tuple. If this code were run in a python interpreter, it would only assume that I’m intending to assign the integer 1 to the my_tuple variable.

# Not a tuple
my_tuple = 1

This is also the case if I use parens.
~~~
# Also not a tuple
my_tuple = (1)
~~~

The comma is what makes a tuple:
~~~
# A tuple!
my_tuple = 1,

The same tuple!
my_tuple = (1,)
~~~

Tuple Ordering and Indexing
Just like lists, tuples are ordered. Ordered means that each element in a tuple will always fall in the same spot. This is handy! Having an ordered sequence means we can loop through it with ease. This is because all Python sequences, including tuples, are numerically indexed, beginning with 0, meaning each element in the sequence has a number indicating its position in the sequence.

That means that individual items in a tuple (or any sequence) are accessed the same way as items in a list:

Take the following tuple:
~~~
game_of_thrones_characters = (‘Sansa’, ‘Brienne’, ‘Tyrion’, ‘Jon’, ‘Cersei’)
~~~

To access the third item in this tuple, ‘Tyrion’, I would use the syntax:
~~~
game_of_thrones_characters[2]
~~~

Likewise, to access the first item, ‘Sansa’, I would use the syntax:
~~~
game_of_thrones_characters[0]
~~~

All of the same operations that work on lists will also work on tuples, with the exception of mutable operations - or ones that change the list, because as we know, tuples are immutable!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. Create a variable called my_tuple and assign to it a tuple with three values, all strings ‘I’, ‘love’, ‘python’.
  2. Create a variable called item3 and assign to it the third element in my_tuple.
A

Creating a tuple:
~~~
my_tuple = (“I”,”love”,”python”)
~~~

Creating a variable and assigning to tuple:
~~~
item3 = my_tuple[2]
~~~

So total code is:
~~~
my_tuple = (‘I’, ‘love’, ‘python’) # Creating a tuple with three string values

item3 = my_tuple[2] # Accessing the third element in my_tuple

print(item3) # Printing the value of item3
~~~

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

Is tuple is a python sequence?

A

Yes, a tuple is a sequence in Python.

In Python, a sequence is an ordered collection of items where each item is assigned a unique index. Tuples, along with lists and strings, are examples of sequences in Python.

As a sequence, tuples share common characteristics and behaviors with other sequences. For instance, you can access elements of a tuple using indexing and slicing, iterate over its elements using loops, check if an element exists in a tuple using the in operator, and apply various sequence operations and methods.

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

Is tuple is a python sequence?

A

Yes, a tuple is a sequence in Python.

In Python, a sequence is an ordered collection of items where each item is assigned a unique index. Tuples, along with lists and strings, are examples of sequences in Python.

As a sequence, tuples share common characteristics and behaviors with other sequences. For instance, you can access elements of a tuple using indexing and slicing, iterate over its elements using loops, check if an element exists in a tuple using the in operator, and apply various sequence operations and methods.

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