12/11/2022 0 Comments Tuple unpacking![]() ![]() ![]() Only the following two methods are available. Methods that add items or remove items are not available with tuple. NameError: name 'my_tuple' is not defined Output Traceback (most recent call last): # NameError: name 'my_tuple' is not defined # TypeError: 'tuple' object doesn't support item deletion It means that we cannot delete or remove items from a tuple.ĭeleting a tuple entirely, however, is possible using the keyword del. # ConcatenationĪs discussed above, we cannot change the elements in a tuple. We can also repeat the elements in a tuple for a given number of times using the * operator.īoth + and * operations result in a new tuple. We can use + operator to combine two tuples. # However, item of mutable element can be changed # TypeError: 'tuple' object does not support item assignment We can also assign a tuple to different values (reassignment). But, if the element is itself a mutable data type like a list, its nested items can be changed. This means that elements of a tuple cannot be changed once they have been assigned. So if we want to access a range, we need the index that will slice the portion from the tuple. Slicing can be best visualized by considering the index to be between the elements as shown below. We can access a range of items in a tuple by using the slicing operator colon. # Negative indexing for accessing tuple elements The index of -1 refers to the last item, -2 to the second last item and so on. Python allows negative indexing for its sequences. # TypeError: list indices must be integers, not float # Accessing tuple elements using indexing Likewise, nested tuples are accessed using nested indexing, as shown in the example below. The index must be an integer, so we cannot use float or other types. ![]() in this example) will raise an Inde圎rror. Trying to access an index outside of the tuple index range(6,7. So, a tuple having 6 elements will have indices from 0 to 5. We can use the index operator to access an item in a tuple, where the index starts from 0. There are various ways in which we can access the elements of a tuple. We will need a trailing comma to indicate that it is, in fact, a tuple. Having one element within parentheses is not enough. my_tuple = 3, 4.6, "dog"Ĭreating a tuple with one element is a bit tricky. My_tuple = ("mouse",, (1, 2, 3))Ī tuple can also be created without using parentheses. The parentheses are optional, however, it is a good practice to use them.Ī tuple can have any number of items and they may be of different types (integer, float, list, string, etc.). The difference between the two is that we cannot change the elements of a tuple once it is assigned whereas we can change the elements of a list.Ī tuple is created by placing all the items (elements) inside parentheses (), separated by commas. Python Dictionaries Access Items Change Items Add Items Remove Items Loop Dictionaries Copy Dictionaries Nested Dictionaries Dictionary Methods Dictionary Exercise Python If.Else Python While Loops Python For Loops Python Functions Python Lambda Python Arrays Python Classes/Objects Python Inheritance Python Iterators Python Scope Python Modules Python Dates Python Math Python JSON Python RegEx Python PIP Python Try.A tuple in Python is similar to a list. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |