Coding in Python: a comprehensive Beginners Guide to Learn the Realms of Coding in Python
Download 1.25 Mb. Pdf ko'rish
|
Coding in Python A Comprehensive Beginners Guide to Learn the Realms
Python Tuples
While lists can be modified and are flexible, Python tuples are the opposite. Once you have created a tuple, you cannot change it. A tuple is similar to a list in the sense that you can fill it up with millions of items but at the same time, it is different from a list in the sense that you cannot add, remove or change the items in a tuple. This is helpful if you want to create a list that you do not want to be changed. The values in a tuple that cannot be changed are labeled as immutable in Python. So, you can label a rigid list or a tuple as an immutable list. In appearance, a tuple looks mostly like a list except that you have to enclose the tuple items inside square brackets. Once you have created a tuple, you can easily access the tuples' items by using the index number. I will add a new feature to the game by creating a list of home items that the same player needs to sell. The difference is that the player cannot change the items of the house except in exceptional circumstances. He has to sell the house the way it is at the moment. He cannot add more items or remove the existing ones to tune the house's value as per the expectations and demands of buyers. homeitems = ('dining table', 'cooking range', 'washing machine', 'refrigerator', 'air conditioner') print(homeitems) = RESTART: C:/Users/saifia computers/Desktop/sample.py ('dining table', 'cooking range', 'washing machine', 'refrigerator', 'air conditioner') >>> Let us see what happens when we try to change the value of an item in the tuple. homeitems = ('dining table', 'cooking range', 'washing machine', 'refrigerator', 'air conditioner') homeitems[0] = ('bed') print(homeitems) = RESTART: C:/Users/saifia computers/Desktop/sample.py Traceback (most recent call last): File "C:/Users/saifia computers/Desktop/sample.py", line 2, in homeitems[0] = ('bed') TypeError: 'tuple' object does not support item assignment >>> You see an error because Python does not allow you to modify tuples. However, there is a way out by which you modify a tuple. You can do the modification by assigning new values to the same variable that carries the tuple. In the following code snippet, I will redefine the tuple. homeitems = ('dining table', 'cooking range', 'washing machine', 'refrigerator', 'air conditioner') print("Original items in the tuple:") for homeitem in homeitems: print(homeitem) homeitems = ('chairs', 'carpets', 'plates', 'oven') print("Modified items in the tuple:") for homeitem in homeitems: print(homeitem) = RESTART: C:/Users/saifia computers/Desktop/sample.py Original items in the tuple: dining table cooking range washing machine refrigerator air conditioner Modified items in the tuple: chairs carpets plates oven >>> Tuples are data structures that can be used to store values that cannot be changed through a program. Looping Just like lists, you can create a loop through your tuple. homeitems = ('dining table', 'cooking range', 'washing machine', 'refrigerator', 'air conditioner') for homeitem in homeitems: print(homeitem) = RESTART: C:/Users/saifia computers/Desktop/sample.py dining table cooking range washing machine refrigerator air conditioner >>> |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling