Learning Python : List, Dictionary & Tuple

seen from United States

seen from Netherlands

seen from Sweden

seen from Türkiye
seen from United States
seen from Netherlands
seen from Vietnam
seen from United States
seen from Hong Kong SAR China
seen from Taiwan
seen from United Kingdom

seen from Germany

seen from Malaysia

seen from Türkiye
seen from China

seen from United States
seen from Russia

seen from Türkiye
seen from United Kingdom

seen from United States
Learning Python : List, Dictionary & Tuple
https://tpoittech.stck.me/post/932098/Understanding-Python-Tuples-Immutable-Data-Made-Simple
Get A Better Career, High Pay & Promotion With India’s Most Advanced AI, ML Courses. Industry Oriented Curriculum, Designed By IITians. 100% Career Support.
Python Tuple
In this article, you'll learn everything about Python tuples. All the more explicitly, what are tuples, how to make them, when to utilize them and different strategies you ought to be comfortable with.
A tuple in Python is like a rundown. The distinction between the two is that we can't change the components of a tuple whenever it is allocated though we can change the components of a rundown.
Making a Tuple
A tuple is made by setting every one of the things (components) inside enclosures (), isolated by commas. The brackets are discretionary, nonetheless, it is a decent practice to utilize them.
A tuple can have quite a few things and they might be of various types (whole number, glide, list, string, and so forth)
# Different types of tuples
# Empty tuple
my_tuple = ()
print(my_tuple)
# Tuple having whole numbers
my_tuple = (1, 2, 3)
print(my_tuple)
# tuple with blended datatypes
my_tuple = (1, "Hi", 3.4)
print(my_tuple)
# settled tuple
my_tuple = ("mouse", [8, 4, 6], (1, 2, 3))
print(my_tuple)
Yield
()
(1, 2, 3)
(1, 'Hi', 3.4)
('mouse', [8, 4, 6], (1, 2, 3))
A tuple can likewise be made without utilizing brackets. This is known as tuple pressing.
my_tuple = 3, 4.6, "canine"
print(my_tuple)
# tuple unloading is likewise conceivable
a, b, c = my_tuple
print(a) # 3
print(b) # 4.6
print(c) # canine
Yield
(3, 4.6, 'canine')
3
4.6
canine
Making a tuple with one component is somewhat interesting. Including one component inside enclosures isn't sufficient. We will require a following comma to demonstrate that it is, truth be told, a tuple.
my_tuple = ("hi")
print(type(my_tuple)) # <class 'str'>
# Creating a tuple having one component
my_tuple = ("hi",)
print(type(my_tuple)) # <class 'tuple'>
# Parentheses is discretionary
my_tuple = "hi",
print(type(my_tuple)) # <class 'tuple'>
Yield
<class 'str'>
<class 'tuple'>
<class 'tuple'>
Access Tuple Elements
There are different manners by which we can get to the components of a tuple.
1. Ordering
We can utilize the list administrator [] to get to a thing in a tuple, where the file begins from 0.
In this way, a tuple having 6 components will have records from 0 to 5. Attempting to get to a file outside of the tuple record range(6,7,... in this model) will raise an IndexError.
The record should be a whole number, so we can't utilize drift or different types. This will bring about TypeError.
In like manner, settled tuples are gotten to utilizing settled ordering, as displayed in the model underneath.
# Accessing tuple components utilizing ordering
my_tuple = ('p','e','r','m','i','t')
print(my_tuple[0]) # 'p'
print(my_tuple[5]) # 't'
# IndexError: list record out of reach
# print(my_tuple[6])
# Index should be a number
# TypeError: list files should be numbers, not skim
# my_tuple[2.0]
# settled tuple
n_tuple = ("mouse", [8, 4, 6], (1, 2, 3))
# settled list
print(n_tuple[0][3]) # 's'
print(n_tuple[1][1]) # 4
Yield
p
t
s
4
2. Negative Indexing
Python permits negative ordering for its groupings.
The list of - 1 alludes to the last thing, - 2 to the second keep going thing, etc.
# Negative ordering for getting to tuple components
my_tuple = ('p', 'e', 'r', 'm', 'I', 't')
# Output: 't'
print(my_tuple[-1])
# Output: 'p'
print(my_tuple[-6])
Yield
t
p
3. Cutting
We can get to a scope of things in a tuple by utilizing the cutting administrator colon :.
# Accessing tuple components utilizing cutting
my_tuple = ('p','r','o','g','r','a','m','i','z')
# components second to fourth
# Output: ('r', 'o', 'g')
print(my_tuple[1:4])
# components starting to second
# Output: ('p', 'r')
print(my_tuple[:- 7])
# components eighth to end
# Output: ('I', 'z')
print(my_tuple[7:])
# components start to finish
# Output: ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'I', 'z')
print(my_tuple[:])
Yield
('r', 'o', 'g')
('p', 'r')
('I', 'z')
('p', 'r', 'o', 'g', 'r', 'a', 'm', 'I', 'z')
Cutting can be best pictured by believing the file to be between the components as displayed underneath. So assuming we need to get to a reach, we need the list that will cut the segment from the tuple.
Component Slicing in Python
Component Slicing in Python
Changing a Tuple
In contrast to records, tuples are unchanging.
This implies that components of a tuple can't be changed whenever they have been allocated. In any case, if the component is itself an alterable information type like a rundown, its settled things can be changed.
We can likewise dole out a tuple to various qualities (reassignment).
# Changing tuple values
my_tuple = (4, 2, 3, [6, 5])
# TypeError: 'tuple' object doesn't uphold thing task
# my_tuple[1] = 9
# However, thing of alterable component can be changed
my_tuple[3][0] = 9 # Output: (4, 2, 3, [9, 5])
print(my_tuple)
# Tuples can be reassigned
my_tuple = ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'I', 'z')
# Output: ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'I', 'z')
print(my_tuple)
Yield
(4, 2, 3, [9, 5])
('p', 'r', 'o', 'g', 'r', 'a', 'm', 'I', 'z')
We can utilize + administrator to consolidate two tuples. This is called connection.
We can likewise rehash the components in a tuple for a given number of times utilizing the * administrator.
Both + and * tasks result in another tuple.
# Concatenation
# Output: (1, 2, 3, 4, 5, 6)
print((1, 2, 3) + (4, 5, 6))
# Repeat
# Output: ('Repeat', 'Rehash', 'Rehash')
print(("Repeat",) * 3)
Yield
(1, 2, 3, 4, 5, 6)
('Rehash', 'Rehash', 'Rehash')
Read Our Latest Blog: Scope of Variable in Python
Erasing a Tuple
As examined above, we can't change the components in a tuple. It implies that we can't erase or eliminate things from a tuple.
Erasing a tuple completely, notwithstanding, is conceivable utilizing the catchphrase del.
# Deleting tuples
my_tuple = ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'I', 'z')
# can't erase things
# TypeError: 'tuple' object doesn't uphold thing cancellation
# del my_tuple[3]
# Can erase a whole tuple
del my_tuple
# NameError: name 'my_tuple' isn't characterized
print(my_tuple)
Yield
Traceback (latest call last):
Document "<string>", line 12, in <module>
NameError: name 'my_tuple' isn't characterized
Tuple Methods
Techniques that add things or eliminate things are not accessible with tuple. Just the accompanying two techniques are accessible.
A few instances of Python tuple techniques:
my_tuple = ('a', 'p', 'p', 'l', 'e',)
print(my_tuple.count('p')) # Output: 2
print(my_tuple.index('l')) # Output: 3
Yield
2
3
Other Tuple Operations
1. Tuple Membership Test
We can test if a thing exists in a tuple or not, utilizing the watchword in.
# Membership test in tuple
my_tuple = ('a', 'p', 'p', 'l', 'e',)
# In activity
print('a' in my_tuple)
print('b' in my_tuple)
# Not in activity
print('g' not in my_tuple)
Yield
Valid
Bogus
Valid
2. Emphasizing Through a Tuple
We can utilize a for circle to emphasize through every thing in a tuple.
# Using a for circle to emphasize through a tuple
for name in ('John', 'Kate'):
print("Hello", name)
Yield
Hi John
Hi Kate
Benefits of Tuple over List
Since tuples are very like records, the two of them are utilized in comparable circumstances. Nonetheless, there are sure benefits of carrying out a tuple over a rundown. Beneath recorded are a portion of the primary benefits:
We by and large use tuples for heterogeneous (unique) information types and records for homogeneous (comparable) information types.
Since tuples are unchanging, emphasizing through a tuple is quicker than with list. So there is a slight presentation help.
Tuples that contain unchanging components can be utilized as a key for a word reference. With records, this is unimaginable.
In the event that you have information that doesn't change, executing it as tuple will ensure that it remains compose secured.
In this blog, we'll learn about Python Tuples, list of tuples, create a tuple in python, difference between list and tuple, tuple operations in python, etc.
Tuples in Python | Insideaiml Article
Python Tuples | Python Tuples Tutorial | Python Tutorial | Python Programming