Now we are going to start a new topic, Creation Traversal of Tuples Class 11. In this article you will get Comprehensive Notes on Creation Traversal of Tuples Class 11. So here we begin!
Creation Traversal of Tuples Class 11
Let’ start the article with introduction to tuples. Tuple means that set of values of a specific sequences. It is considered as a standard data type that store any sequence of values of any type. As you know tuples are immutable i.e. you cannot change the elements of a tuple in place. Python creates a new tuple whenever any changes made to them.
To create tuple, put all the values inside parenthesis i. e. moon or rounded brackets. You can create tuples in following ways:
- Empty Tuple – Like empty lists, you can create empty tuples
- Tuple of integers – Tuple with integer values
- Tuple of mixed numbers – Tuple with integers, and float numbers
- Tuple of characters – Tuple using characters
- Tuple of mixed values – Tuple with numbers, characters and strings
- Nested Tuple – A tuple which has another tuple as an element
- Tuple from existing sequence – A tuple can be created using existing sequence
As we have discussed, tuple are created using parenthesis. You can create an empty tuple like this:
t = ()
Where t is a tuple object like variable, = assigns empty values with ().
Tuple of integers
The tuple which has integer values are considered as tuple of integers. The example is something like this:
t = (2,4,5,6)
You can create a tuple with single element without brackets also. For example,
t = 56, or t=(56,) are similar.
If you write t = (56) then it will consider as a single value, not tuple.
Tuple of mixed numbers
The tuple can have mixed numbers including integers and floating numbers. Consider the following example:
t = (90,87.50,85.25,3,7.0,45,65)
Tuple of characters
The tuple which contains sequence of characters is considered as tuple of characters. For example:
t = (‘a’,’b’,’c’,’d’,’e’)
Tuple of mixed values
A tuple can also consists of mixed values including integers, characters and strings. For example,
t = (1,’Amrita’,95.85,’A’)
A tuple can have another tuple as an element is known as nested tuple. Consider the following example:
t = (1,’Virat Kohli’,(102,67,23))
Tuple from existing sequence
As list() method we have used to convert any sequence into the list, similarly tuple() function allows creating any sequence into the tuple. Let’s have a look at the following examples:
t = tuple(‘TutorialAICSIP’)
t = tuple([10,20,30])
You can use eval() function to create a tuple of values entered by user.
Now in the next section of Creation Traversal of Tuples Class 11 we will see how to access the elements tuple and work with them. As like lists, you can access tuples in the following ways:
- Membership Operator
As like lists we can access tuples using forward (Positive) Indexing, backward (negative) indexing, and looping. Look at the following examples:
t = (11,34,56,78,90) print(t,t,t) print(t[-1],t[-2],t[-3]) for i in range(len(t)): print(i)
The tuple can be accessed through slicing as like as lists. For example
t = [10,20,30,40,50] slice1 = t[0:4] print(slice1) slice1=t[2:-1] print(slice1) slice1=t[2:33] print(slice1) print(t[0:10:2]) print(t[0:10:3]) print(t[:5:3]) print(t[::3]) print(t[4::2])
Now you know very well how to use membership operator in and not in for accessing values from the tuple. Consider following example:
t = [10,20,30,40,50] print(44 in t) print(44 not in t)
The first print statement will will display False where as second print statement will display True as 44 is not present in the tuple.
In the next section of Creation Traversal of Tuples Class 11 we will talk about tuple concatenation.
It is also known as joining the tuples. Have a look at the following example:
t1 = (4,5,6) t2 = (7,8,9) t3 = t1 + t2 print(t3)
You can replicate the entire tuple values using * operator. Look at this example:
t= (1,2,3) print(t * 4)
That’s all from Creation Traversal of Tuples Class 11. I hope you understands the concept very well. If you have any query or doubt, feel free to ask in the comment section.
Hit the like button if you enjoyed this article and learned something from it. You can share your valuable feedback/suggestion/views in the comment section.
Thank you very much for visiting our blog.