Working with Tuples in Python
≀Paulo Portela
Posted on February 18, 2024
In this post, we'll explore the basics of tuples in Python, using an astronomy theme to provide code examples. Tuples are similar to lists in that they are used to store collections of items in a single variable. However, tuples are immutable, hashable, more memory-efficient, and faster than lists, making them a good choice for fixed, unchanging data. Let's get started.
First, let's create an empty tuple. This can be done by assigning an empty pair of parentheses to a variable:
constellations = ()
print(constellations)
# Output: ()
Now, let's create a tuple with one element. To do this, we need to include a trailing comma after the element, to distinguish it from a regular variable assignment:
big_dipper = ("Ursa Major",)
print(big_dipper)
# Output: ('Ursa Major',)
We can also create a tuple with more than one element by separating the elements with commas:
orion = ("Betelgeuse", "Rigel", "Bellatrix", "Mintaka", "Alnilam", "Alnitak", "Saiph")
print(orion)
# Output: ('Betelgeuse', 'Rigel', 'Bellatrix', 'Mintaka', 'Alnilam', 'Alnitak', 'Saiph')
We can access the elements of a tuple in the same way as we do with lists, using their index. For example, to get the first element of the orion
tuple, we can use the following code:
first_star = orion
print(first_star)
# Output: Betelgeuse
We can also access a range of elements using slicing. For example, to get the second and third elements of the orion
tuple, we can use the following code:
second_and_third_stars = orion[1:3]
print(second_and_third_stars)
# Output: ('Rigel', 'Bellatrix')
We can reverse a tuple using slicing with a step of -1
:
reversed_orion = orion[::-1]
print(reversed_orion)
# Output: ('Saiph', 'Alnitak', 'Alnilam', 'Mintaka', 'Bellatrix', 'Rigel', 'Betelgeuse')
To get the number of elements in a tuple, we can use the len()
function:
num_stars = len(orion)
print(num_stars)
# Output: 7
Since tuples are immutable, we cannot add, remove, or change their elements. However, we can create a new tuple by concatenating two or more tuples using the +
operator:
big_and_little_dippers = big_dipper + ("Ursa Minor",)
print(big_and_little_dippers)
# Output: ('Ursa Major', 'Ursa Minor')
We can also create a new tuple by repeating a tuple a certain number of times using the *
operator:
three_bears = big_dipper * 3
print(three_bears)
# Output: ('Ursa Major', 'Ursa Major', 'Ursa Major')
We can check if an element is in a tuple using the in
keyword. For example, to check if "Betelgeuse" is in the orion
tuple, we can use the following code:
betelgeuse_in_orion = "Betelgeuse" in orion
print(betelgeuse_in_orion)
# Output: True
We can also use the count()
method to count the number of times an element appears in a tuple:
num_ursa_major = three_bears.count("Ursa Major")
print(num_ursa_major)
# Output: 3
We can use the index()
method to find the index of the first occurrence of an element in a tuple. For example, to find the index of "Rigel" in the orion
tuple, we can use the following code:
rigel_index = orion.index("Rigel")
print(rigel_index)
# Output: 1
That concludes our beginner's guide to tuples in Python. We've covered various operations, from creating and accessing elements to concatenating and repeating tuples. We hope this guide has helped get you started with tuples in Python. Happy coding!
Posted on February 18, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 12, 2024