TUPLE
Hello guys, we are going to study about, tuple. Tuple is almost similar to list.
The only difference is that we cannot change the elements of a tuple when
assigned.
Creating
a tuple
Initializing a tuple is as simple as initializing a
list, instead we keep our variables between () than [].
Note: - tuple can be created with the parentheses. This is known as tuple packing.
Creating a single value stored tuple is bit tricky, check out the code and its output to find out.
Output
Accessing and indexing of elements of tuple
Accessing tuple is easy. We can use
indexing to access elements of the tuple.
Negative indexing is also covered in the example
below.
Check out the example
Output
Changing of elements of tuple
You can’t change the elements of the tuple. Tuple is immutable.
If a nested tuple is given including a list, we can change the element of the
list.
Else we can use + and * operators to add two or more tuple. This is called concatenation.
The + operator will consequently add the tuples.
To repeat any
element we can use * operator.
Deleting a
tuple
We can delete the whole tuple with del. We cannot delete or remove a
specific element from the tuple.
Tuple method
Methods that add or delete
items of a tuple aren’t available. We have only index and count method to use.
Hello Python people, for this blog we have taken help from the book Python : The Complete Reference.
If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via Instagram "@coding.winds".
Do subscribe to our daily blog update by clicking here.
Thank You!