Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Seeing More Immutable Built-in Data Types: Strings & Tuples

00:00 Two more of Python’s built-in immutable types are data types you’ve seen already. One of them is a string, for example, same string we’ve seen earlier greeting = "Hello, Pythonistas!".

00:11 They’re immutable because we cannot change, let’s say you want to change the first letter and change it to something else, for example, “A”. If you want to do that, you can’t.

00:22 Once you create a string object, it cannot be mutated, it cannot be changed. And this is the reason why the string methods such as .upper() you’ve seen earlier, they do return a copy of the string greeting.upper() and we’ll see this is the case for other string methods that make a change to the string.

00:45 They’re not making a change to the same string object. Instead, they’re creating a new object that’s similar to the original string, but with the appropriate change.

00:57 In this case, everything is uppercase, but greeting has not changed. Let’s look at the identity of greeting.

01:07 It’s a number that ends at 1440. The meaning of this number is not important except it’s unique. So what we’re looking for is that no other object has this same number.

01:17 Because if it does, it means it’s the same object.

01:20 And greeting.lower(),

01:25 we have 'hello pythonistas!'. Now let’s look at the id(). The underscore in Python’s standard REPL allows you to access the most recent output.

01:36 In this case, it’s the string, lowercase 'hello pythonistas!'. And you can see that’s a different id from greeting because greeting was the id that ends with 1440.

01:49 Therefore, when you call a string method, the method does not change the object itself. It cannot do that because the object, the string object, is immutable.

01:59 Instead, it creates a new object with the required string.

02:04 And the same applies for tuples such as the tuple we’ve seen before. Once you create a tuple, you cannot make any changes to this tuple. If we try to delete team, let’s say it’s Jason, you want to delete, you get a TypeError.

02:20 Tuple object does not support item deletion. Why? Because immutable objects cannot change. Once you’ve created the object, you cannot remove anything from it.

02:29 You cannot add a new item to this tuple. It is fixed.

02:35 And a few more things about tuples or immutable data types in general. Let’s redefine in a new REPL team, which is the same tuple Jason, Matt, Sarah, Caitlin, and Mark.

02:47 Let’s look at the id for team, if I use the correct parentheses especially.

02:53 So in this case, this is a new REPL session, so the identity has changed. The number you get from the identity is unique for every run of the program for the lifetime of your Python program.

03:06 If you run a new program, or as I’ve done in this case, run a new Python REPL, the object identity will be different.

03:14 And what if you try to use the += operator? Now you can only add a tuple to another tuple. So here you’d need to say, for example, Claire,

03:25 and for this to be a tuple, you need to put the comma. The parentheses are optional, but the comma is not because without a comma this would be a string, whereas this makes it a tuple.

03:35 Let’s have a look at team. And it seems as though team has changed, and that is true. The variable team is now pointing to the tuple, Jason, Matt, Sarah, Caitlyn, Mark, and Claire.

03:47 However, the identity of team is now different. It’s a number that ends with 5072, not 5136. So when you use the += operator, since a tuple is immutable, Python creates a new tuple with the old one and the new one.

04:07 And that’s a new tuple so it’s a new object. You can also see that if you try to explore the methods that tuple has, well there is no append() as lists do.

04:18 If you try to say team.append("John"), for example, or any name you want really, tuple object has no attribute append. And this should not be surprising because tuples are immutable.

04:30 So you cannot have a method that changes the tuple. If you want to add something to the tuple, you’ll have to create a new one. The only methods that tuples have are .index(), which allows you to find the index for let’s say Matt.

04:46 And this is fine because it’s saying Matt is at index 1. So the second position in the tuple, this does not make changes to the tuple. So this is perfectly fine.

04:57 And the other one is .count(), which allows you to count how many times Matt appears in the list. In this case, it’s also 1. And again, .count() is giving you a value based on how many times this item appears in the tuple.

05:09 It’s not making changes to the tuple.

05:12 .index() and .count() are methods you can have for tuples. All the other methods that you might be familiar with from lists such as append() or sort() cannot be valid methods for a tuple because they require changes to the object, which tuples do not allow.

05:30 So to summarize, Python’s built-in immutable data types include integers, floats, and complex numbers, Booleans, which are a subtype of integers, strings, and tuples.

05:42 There’s also another one, bytes. We’re not going to be discussing bytes in this video course, but if you want to read more about the bytes built-in data type, you can do so in the article, which accompanies this video course, “Python’s Immutable vs Mutable Types: What’s the Difference?”

Become a Member to join the conversation.