Python Tuples are the primary Python data structure which are ordered and immutable. Immutable means that the values of the different indices can not be reassigned once their values have been defined.
An empty Tuple is just a pair of parenthesis, while a one-element Tuple has the value with a comma at end, inside the parenthesis.
Tuples do not have to have parenthesis around them. Usually whenever you see a list of numbers, it is treated as a Tuple. This will be seen to be important in both sending values to external functions as well as getting values from the functions, as we will later see.
If you have the same number of elements before and after an assignment, the values will be unpacked to individual variables. Thus here we define the variable a to be 1, and the variable b to be 2.
Unlike Lists, we can not use the append() function to add values. However we can use the plus operator to join tuples.
This is the immutable property of a Tuple. The individual indices can't be changed once we have given them values. We can see that we again use the square brackets to extract an element, which is common in Python.
Even though a Tuple can not be changed element-wise, we can make global changes. This is due to the fact that in Python, variable names are only pointers to the actual data. If they lose reference, Python automatically deletes them.
We can use the list() function to change a Tuple to a List. A List is Mutable and individual elements can be changed.
Once an element has been changed, we can turn it back into a Tuple with the tuple() function.
Likewise, if we use, the function tuple() on a string, its characters are placed in individual indices.
As before, we can test membership of an element with the in command. Tuples have count() and index() functions with similar functionality as their List counterparts.
We can use a for statement to loop the elements in a Tuple. For example, we can find out how many vowels are in a Tuple with this example.
Unlike List Comprehensions, there are no Tuple Comprehensions. However if we use parenthesis with a comprehension loop, it generates a Python generator object. Basically a generator object knows how to generate itself, on demand, but does not store all values saving memory. We can use the tuple() function to force it to generate all values at once and store the result as a Tuple. These two steps can be considered as constructing a Tuple.
Tuples will return the number of elements with the len() function.
You may go to pythonaudio.blogspot.com to see the slides. To see a larger image of the slide, you can click on them at that page, which provides easy navigation controls. The text for the audio of the slides as well as any relevant source code is also on that page.
This is the video of Tutorial 9:
nice article for beginners.thank you.
ReplyDeletejavacodegeeks
welookups python
very good post, i actually love this web site, carry on it
ReplyDeletepython online training
python training