Python Tuple Operations

This is the 13th part of this article series- ‘PYTHON’. This is the extended part of the previous article the Python Tuples. In this particular article am going to discuss Python Tuple operations like update, delete, length, repetition, membership using several examples and explanations. I just want you guys to understand these concepts as we are going to use them in upcoming articles.
So, let’s explore it-
Getting Theme
For getting the theme of Python, kindly go through my previous articles:
Chapter 1 | Python Properties
Chapter 2 | Python VS Other Languages
Chapter 3 | Environment and Setups
Chapter 4 | Python Basics 1.1
Chapter 5 | Python Basics 1.2
Chapter 6 | Python Strings
Chapter 7 | Python Lists
Chapter 8 | Python Operators
Chapter 9 | Python Decision Making
Chapter 10 | Python Looping
Chapter 11 | Python Loop Control Statements
Chapter 12 | Python Tuples
Chapter 13 | Python Tuple Operations…
Chapter 14 | Python Built-in Tuple Functions
Chapter 15 | Python Dictionary
Chapter 16 | Python Dictionary Operations
Chapter 17 | Python Function
Chapter 18 | Python Function Calling Arguments
Chapter 19 | Python Anonymous Function
Chapter 20 | Python Built-in Anonymous Function
Python Tuple Operations
Now am going to discuss some of the basic python tuple operations.
-
Tuples | Update
As I mentioned earlier, that tupies are not dynamic (we can’t change tuple values). But there is always a alternate or different way to perform a certain operation in any programming language, so as in python.
In python one can take portions of existing tuples to create a new tuples. In simple you can call it concatenation of two tuples. Let’s have a look how.
Example:
tup1 = ('.NET', 'HTML5', 'Python'); tup2 = "C#", "CSS", "Django";
# Creating a new Tuple tup3 = (tup1 + tup2); print (tup3)
Output:
( '.NET', 'HTML5', 'Python', "C#", "CSS", "Django") >>>
-
Tuple | Delete
In python you can never delete a single tuple, so you have to always delete a complete tuple. To delete a tuple in python we use del statement.
Example:
tup = ('.NET', 'HTML5', 'Python');
del (tup) print (tup)
Output:
You will get an error message, like this:
Explanation:
Did you get that – Why such error occur??
If not then let me explain. Actually we defined a tuple named tup and provided it some set of values then we used tuple operation del to delete the same and the only created tuple. And in the very next satetement we tried to print the same tuple, we just deleted. Thus, we tried to access already deleted tuple, that’s why ae are getting this error.
I hope it’s clear.
-
Tuple | Length
Tuple length can be defined by using len, same as we use to do in strings and in several other programming languages.
Example:
a = len ('LetUsTweak') print (a)
Output:
10 >>>
-
Tuple | Repetition
In order to repeat a whole tuple or a tuple value any number of time, we do use tuple repetition. This can be done by using
Example:
1. Repetition of the whole tuple:
a = ("Goodbye World!")*2 print (a)
Output:
Goodbye World!Goodbye World! >>>
2. Repetition of a certain value of tuple:
a = ("Goodbye World!", "Hello World!" *2) print (a)
Output:
('Goodbye World!', 'Hello World!Hello World!') >>>
-
Tuple | Membership
This operations works somewhat like Boolean. It checks or verify the values in ceratin tuple then it result true otherwise false.
Example:
-
Condition- ‘True’
a = (1, 2, 3, 4) b = 3 in a
print (b)
Output:
True >>>
2. Condition- ‘False’
a = (1, 2, 3, 4) b = 5 in a
print (b)
Output:
False >>>
Do it Yourself
“Try creating a bunch of tuples of different-different values and types as described in the article above and afterwards try to access them using different index values.”
Guidelines from our Side
- Do as much as code you can.
- Code anything you want, the best way to learn
- Don’t just study things, try to learn them.
- Work on your Concepts
- Work on the fundamentals of any technology or stuff you want to learn.
Moto-
“Keep calm and code python”.
I tried to make it interesting and interactive article and wish you guys going to like that, meanwhile. if you have any suggestions then your welcome.
Till the next part, Keep Sharing & Tweaking! 🙂
Discussion