This is the 12th part of this article series- ‘PYTHON’. In this article, we are going to learn about the concepts of Python Tuples and respective statements. In addition to that will also go through- Creating your first Python Tuple and accessing your first Python Tuples using several examples, flow charts, explanations etc.
So, let’s explore it-
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
A tuple can be defined as-
“A sequence of immutable python objects are called tuples.”
In more simple words Tuple can be defined as sequence and it can be of any type (either homogeneous or heterogeneous) based on functionality and requirement of the program just like lists in python.
As I said above in this article, tuples are similar to lists.
So is there any difference between both of them??
Yes there is a catch, let’s have a look:
- Tuples are not dynamic like List, here dynamic means- tuples can’t be changed unlike Lists.
- Tuples uses small brackets, whereas list uses square brackets.
(For more info regarding Lists, kindly visit my previous article)
Creating a Simple Tuple
You can represent Tuple as-
tup1 = (‘.NET’, ‘HTML5’, ‘Python’);
or tup1 = “.NET”, “HTML5”, “Python”;
or tup1 = (1, 2, 3, 5, 8);
or # An empty tuple tup1 = ();
Suppose, if you want to put only one element in a tuple. So be careful, because there is a catch. Let’s have a look-
Tup1 = (1, );
As you can see there is still a comma, in spite of being only one element in the list.
To access the values in the tuple, we use square brackets. That square bracket will access the index to obtain the value available at that index.
# Defining Tuples
tup1 = ('.NET', 'HTML5', 'Python'); tup2 = "C#", "CSS", "Django"; tup3 = (1, 2, 3);
# Accessing Tuples
print (tup1) print (tup2) print (tup3)
As you can see in that image, we got the output as per our access. We used the indexes for accessing these entries. You can always try some other different index values to get some different results.
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.
“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! 🙂