Transcription
Hello! In this lesson, we will get acquainted with tuples in the Python language.
Before we begin, I would like to recommend the website kipr.com. On this site, you will find code, homework assignments, and a lot of other useful information. The link to this lesson on the site will be in the description of this video.
Tuples in Python are essentially the same as lists, with two exceptions. First, once you add elements to a tuple, you cannot change them later. In essence, a tuple is like a constant where you add certain values, and those values exist without any changes thereafter.
The second difference is that tuples take up less space than lists. In fact, the size of these tuples and lists does not differ dramatically, but when we are developing websites using Python and need to pass information to users, it makes more sense to use tuples because they occupy less memory. Even a slight memory saving is beneficial, whether it's a computer program or a website. Memory optimization is always a priority.
For this reason, tuples are often used for data transmission. First, we ensure that the user cannot change the values in the tuple, and second, we transmit a smaller amount of data, thus optimizing the program.
Now, let's look at how to create tuples. Tuples are created in the same way as lists. To create a list, we use square brackets, while for tuples, we use parentheses. The rest of the operations are the same as with lists.
Let's input some values. For now, I will use only numbers, but you can also include booleans, floating-point numbers, or strings. All of this will be processed correctly.
Let's create such a tuple. If I try to access any element, the method of accessing elements is the same as with lists, meaning we also use square brackets—no parentheses here. For example, if I want to output the element six, I need to access it by its index. I know that this element is in the second position, which corresponds to index one, so I write one here, and I get the element I need.
Also, returning to the previous topic of slicing, you can specify a slice here. For instance, I will indicate that starting from index one up to the fifth element, we will output all elements. All elements are displayed correctly.
Notice that when outputting elements, we use parentheses, not square brackets. The parentheses are used because this is indeed a tuple.
Now, let's try to change an element. I will attempt to access the zero element and assign something new to it. We can see that the entire line is highlighted, indicating that something is wrong. Let's try to run this, and we notice that an error is displayed. If we translate this error, it states that a tuple does not have the functionality to reassign values. Therefore, I cannot assign a new value here.
Let me add a comment for your future reference: tuples have their own set of functions. However, when comparing tuples to lists, lists have a vast number of functions for deleting, adding, and modifying elements. In contrast, tuples have very few functions. For example, there is the `count` function, which allows you to count the number of occurrences of a specified element.
Let's say I want to find elements with the value six. In our case, it should return two, which is correct because I have two such elements. If I specify a non-existent value, it will return zero since no such elements were found.
So, there is the `count` function for working with tuples, and besides `count`, there is essentially only one other function: `len`. This function allows us to get the length of the entire tuple. If we run this, we see that it returns eight, which is the number of elements in our tuple.
Of course, besides calling functions, you can also simply output the entire tuple to the screen. To do this, just refer to it by name, and the entire tuple is displayed. Again, we notice that parentheses are used in the output.
If you need to output several elements, remember that there are both regular indices and slices, where we can specify the start, end, and step, as we have already discussed.
Now, let's look at how to create different tuples. In fact, you can create a tuple without parentheses. I can simply start listing elements separated by commas. Again, the data types can be different. If I try to output this to the screen, we will see that the data type is indeed a tuple.
If I use parentheses here, nothing changes; it will still be a tuple. However, if I write parentheses but only include one element inside, it will not be considered a tuple. You see, the parentheses do not add anything, and it is treated as just a single element.
To indicate that this five should be treated as a tuple, I need to add a comma. I can even omit the second element and just place a comma, which indicates that this is indeed a tuple.
If you want, you can also specify a comma without parentheses, and this will also create a tuple. The presence of parentheses indicates that we have created a tuple.
Now, let's place the same elements here as we did last time and try to iterate through this tuple using a loop. You can use a `while` loop or a `for` loop; both will work correctly, but the `for` loop is much more convenient for iterating through tuples, lists, dictionaries, and other structures that we will study later.
Here, I will create a variable called `element` and indicate that I am iterating through this tuple. For outputting each individual element, we use `print`. You can see that the output of lists and tuples is the same; we can use the same loop for both.
All elements have been displayed on the screen, and everything has been processed correctly.
Additionally, if you have a list in your program, for example, let's create a list with certain numbers, and I need to convert this list into a tuple. How can I do this?
Of course, I could change the square brackets, but suppose I cannot do that during the program. I can simply redefine this list using a method, indicating that it will be a tuple.
Let's create a new variable, say `new_tuple`, and to convert this list into a tuple, I need to use the `tuple` keyword. In other words, I need to use the `tuple` function.
Again, remember that `tuple` in English means "tuple," and I just pass one parameter, which in my case is this list. If I run this now, we won't notice anything, but if we output it to the screen, we will see that it was previously a list, and now it is indeed a tuple because it has parentheses.
In a similar way, you can also convert a string into a tuple. Remember that a string is a list of characters, and you can easily convert such a list into a tuple.
Let's create a new string here, for example, "hello world." If I output this string, we will see that it is just a regular string. But if I call the `tuple` function and pass this string to it, we will see that it results in a large tuple where each element is a specific character from the string. This is quite convenient.
Thus, we conclude that tuples are essentially the same as lists, except that tuples cannot be modified, and they take up less space. Therefore, tuples are preferable for passing user data, for example, or for sending data to a server or receiving data from a server.
At the same time, it is impossible to modify elements within a tuple. So, if you need a list to store information that will change later, you should definitely use lists. But if you are just passing user data or data on a server, it is always better to use tuples since they occupy less space, optimizing your program.
Well, that's all for now. I hope you enjoyed the lesson. If so, don't forget to subscribe to the channel and join our social media. All links will be in the description of this video. That's all from me. See you soon! Goodbye!