Topples are another type of sequences in Python you can consider topples as being immutable lists, meaning their contents cannot be changed by adding removing or replacing elements. topples may prove to be useful when you want to store some data in the form of a sequence and keep that data untouchable. However, unlike sets, topples are ordered collection of non unique elements, meaning indexes and duplicates are allowed. Okay, enough with the theory, let's start to practice and create our first topple. tuples are enclosed by parentheses, and their elements are separated by commas. So let's create our first empty topple.
Let's say my topple Okay, let's check type of my topple. And indeed, this is an empty topple and Python confirms this as well. If you want to create a topple with a single element, you have to use a trick That, although you have only one element inside the tupple, you have to write a comma after it. Otherwise it will not be regarded as a topple. Let's see this in practice, let's say, my topple having a single element, let's say nine. And now let's check type of my topple.
And this time we get class int, meaning this is an integer. As I said, in order to define a topple with a single element, you have to add a comma after that element. And this time, if we check type of my topple, you have a topple setup. You will remember this when creating topples that have a single element. Next, let's populate our topple with more elements. So let's see my topple.
Okay, just like strings and lists, topples support indexing, so if you want to access an element within a topple the indexing rules we've seen before are still applicable. So my topple of zero returns the first element in the topple which is one One multiple of one returns true, of course, multiple of minus one returns the last element in the tupple, which should be five. Multiple of minus two should return four. And it did it does. Since tuples are immutable, you cannot add or modify an element of A topple. Let's check this.
Let's say we want to modify the element at index one. So my topple of one equals 10. And we get the Type error topple object does not support item assignment. Also, removing elements is not permitted when working with tuples. So they'll have my tupple of one, we get the same type of exception type error, this time tupple object doesn't support item deletion. Another interesting thing you can do with topos is topple assignment.
This means you assign a topple of variables to a tuple of values and map each variable in the first topple to the corresponding value in the second topple. Let's See this as well, we will define topple one having the following elements. And now let's assign a tupple of variables to topple one. So let's say vendor, comma model, comma iOS equals topple one. Finally, let's check if the assignment and variable to value mapping have been properly performed. This means vendors should have the value of Cisco, so let's check this vendor.
And let's check the other two variables as well. And the assignment has been properly done. This is also called topple packing and unpacking. And you can see it as a kind of mapping between elements of two different topples. An important thing to remember here is that both tables should have the same number of elements. Otherwise, if you have different number of elements of value error will be returned.
So let's check this. Let's say we have topple two equals 123 and four And a tupple of variables, let's say X, Y, and Z equals topple two. So you can notice that tupple two has four elements, four values, and this couple of variables has only three values. And this should return an error. As I said, Indeed, we have a value error too many values to unpack expected three. You can also assign values in a table to variables in another table within a single statement, which is even more convenient.
So let's try a B and C equals 1020 and 30. Knowledge Check A, B, and C. Okay, so the result is correct. The mapping has been done successfully.