Python #9: Tuples

A tuple is basically a list. The main difference from a Python list is it’s permanent, it can’t be appended, added to, or modified in any way. The formal term for this is immutable, meaning it doesn’t change over time. Lists can be changed, so they’re mutable, and tuples can’t be changed, they’re immutable. And tuples use parentheses compared to lists that use brackets.

You can change (or ‘cast’) a list to a tuple by just typing ‘tuple’ in front of it and parenthesizing around the list’s brackets. Or you can make it simpler and use the variable or parameter the list is casted from.

We can check for an element in a tuple with the ‘in’ keyword

colors = ("red", "green", "blue")
print("blue" in colors)

Output: True

The ‘in colors’ is what makes it a ‘True/False’ statement. If we took it away, it would just print ‘blue’.

You can assign a tuple of values to a tuple of variables

(a, b) = (1, 2)

After this line, ‘a’ will contain 1 and ‘b’ will contain 2. And only in this case of tuples, you can leave out the parentheses.

We can unpack a tuple which means extracting the items from it.

coordinate = (12, 33)

x = coordinate[0]
y = coordinate[1]

x, y = coordinate

If I wanted to extract that 12 and 23 from that ‘coordinate’, I could do those first 2 lines with the indexes, or just do that last line at the bottom. That’s simpler and easier to read.

Slicing

Slicing is a way to extract multiple items from a list or string with a specified range.

"abcdef"[0:2] == "ab"
"abcdef"[0:3] == "abc"
"abcdef"[1:4] == "bcd

Using a colon, we can set a range within the string that selects the characters we want. Just like with the regular range, the end number is the index it stops just before. The first one selects from index 0 to 2. Reminder that 0 is the first character, and 1 is the second. So even though the range stops at 2, we’re only selecting up to index number 1, which means we stop at the second character. Again, whichever index number you end it at, it will not show that ending index, it’ll show the one just before it.

If you’re slicing a string from a function, you return it with the string and the slice command

def first_three(grant):
    return grant[0:3] 

This is if I wanted the first 3 letters of the ‘grant’ string to be returned. There’s no output since I didn’t print it, but the system knows that I selected ‘gra’. Since the beginning is 0, we can also leave it out and just keep the 3 at the end and it’ll still output the same. So a beginning number is only needed if the index is past 0; make sure you keep the colon first.

If we want it to go all the way to end, but we just want it to cut off a part of the beginning, you can put a beginning index, type the colon, and leave it empty after the colon. And vice versa, if we just want to cut off the tail end index, leaving everything before that, we’d leave the space before the colon empty, and we’d put a minus with the number after the colon, which’ll then cut the index off from the end.

There’s also a way to use variables to slice a string.

string = "hello world"
begin = len(string) - 5
end = len(string) - 1
print(string[begin:end])

Output: worl

Using the ‘len’ function (which selects the length of the string), we first use a ‘begin’ variable whose minus 5 tells it to start from the sixth index, which is the ‘w’ in ‘hello world’. And the ‘end’ variable’s minus 1 subtracts 1 from the end of the string, which is the ‘d’ in ‘world’. So the final output would be ‘worl’ since those are the remaining characters from the boundaries we set. ‘Begin’ and ‘end’ aren’t built-in functions, we just named it those. In this case, we only used the minus to start from the end of the string and cut off to where we want it to begin.

Leave a comment