Data Structures Used In Python- Tuple - MyPythonGuru

Jobs Search Portal and Learning point for Python,Data Science,AI,ML, Cloud and latest technologies.

Follow us on Facebook

Post Top Ad

Your Ad Spot

Thursday, July 11, 2019

Data Structures Used In Python- Tuple

Tuples--


  • In Python, there are different data types: string, integer and float. These data type contained in a tuple.
  • Tuples are combination of integer,string,float grouped under () Parentheses.
  • Tuples are immutable i.e they cant be change or edited.
  • we can extend the length of tuples by adding more element into it..


For Example:

1.SimleTuple = (0, 9, 6, 5, 10, 8, 9, 6, 2)

In above example tuple in collection of similat type of data type( integer)

2.NestedTuple=(1, 2, ("physics,chemistry"), "rock") ,(2,4),("book",(1,2)))

In the above example tuple is collection of heterogeneous data type.

3.explanation of  NestedTuple

a. 1,2 --- data type is integer
b. ("pop", "rock")-- data type tuple( collection of similar data type String)
c. (3,4) -- data type tuple ( combination of integer data type which when close into parantheses then also name as tuple)

In Python, there are different data types: string, integer and float. These data types can all be contained in a tuple as follows:

Now, let us create your first tuple with string, integer and float.

================================================================================

# Create your first tuple

tuple1 = ("AWS",”Microsoft”,10,1.2,”azure”,”IBM” )
tuple1
The type of variable is a tuple.

# Print the type of the tuple you created

type(tuple1)
Indexing:

Each element of a tuple can be accessed via an index. The following table represents the relationship between the index and the items in the tuple. Each element can be obtained by the name of the tuple followed by a square bracket with the index number:

We can print out each value in the tuple:

# Print the variable on each index

print(tuple1[0])
print(tuple1[1])
print(tuple1[2])
We can print out the type of each value in the tuple:

# Print the type of value on each index

print(type(tuple1[0]))
print(type(tuple1[1]))
print(type(tuple1[2]))
We can also use negative indexing.

We can obtain the last element as follows (this time we will not use the print statement to display the values):

# Use negative index to get the value of the last element of tuple1

tuple1[-1]

We can display the next two elements as follows:

# Use negative index to get the value of the second last element

tuple1[-2]

# Use negative index to get the value of the third last element

tuple1[-3]
Concatenate Tuples:

We can concatenate or combine tuples by using the + sign:

# Concatenate two tuples

tuple2 = tuple1 + ("hard rock", 10)
tuple2

We can slice tuples obtaining multiple values as demonstrated by the figure below:

Slicing of Tuples:

We can slice tuples, obtaining new tuples with the corresponding elements:

# Slice from index 0 to index 2

tuple2[0:3]
We can obtain the last two elements of the tuple:

# Slice from index 3 to index 4

tuple2[3:5]
We can obtain the length of a tuple using the length command:

# Get the length of tuple

len(tuple2)
This figure shows the number of elements:

Sorting of Tuple:

Consider the following tuple:

# sort the value of tuple Ranking

Rankings = (0, 9, 6, 5, 10, 8, 9, 6, 2)
We can sort the values in a tuple and save it to a new tuple:

# Sort the tuple

RankingSorted = sorted(Ranking)
RankingSorted
Nested Tuple:

Nested tuple is conmbination of string,integet,tuple etc.where tuple can contain another tuple as well as other more complex data types. This process is called 'nesting'. Consider the following tuple with several elements:

# Create a nest tuple

NestedT =(1, 2, ("aws", "azure") ,(3,4),("ibm",(1,2)))
Each element in the tuple including other tuples can be obtained via an index as shown in the figure:


# Print element on each index

print("Element 0 of Tuple: ", NestedT[0])
print("Element 1 of Tuple: ", NestedT[1])
print("Element 2 of Tuple: ", NestedT[2])
print("Element 3 of Tuple: ", NestedT[3])
print("Element 4 of Tuple: ", NestedT[4])
We can use the second index to access other tuples as demonstrated in the figure:

We can access the nested tuples :

# Print element on each index, including nest indexes

print("Element 2, 0 of Tuple: ",   NestedT[2][0])
print("Element 2, 1 of Tuple: ",   NestedT[2][1])
print("Element 3, 0 of Tuple: ",   NestedT[3][0])
print("Element 3, 1 of Tuple: ",   NestedT[3][1])
print("Element 4, 0 of Tuple: ",   NestedT[4][0])
print("Element 4, 1 of Tuple: ",   NestedT[4][1])
We can access strings in the second nested tuples using a third index:

# Print the first element in the second nested tuples

NestedT[2][1][0]

# Print the second element in the second nested tuples

NestedT[2][1][1]
We can use a tree to visualise the process. Each new index corresponds to a deeper level in the tree:

Similarly, we can access elements nested deeper in the tree with a fourth index:

# Print the first element in the second nested tuples

NestedT[4][1][0]

# Print the second element in the second nested tuples

NestedT[4][1][1]
The following figure shows the relationship of the tree and the element NestedT[4][1][1]:

Quiz on Tuples:
Execute the below python program and share the result in comment section:

# sample tuple

cloud_tuple = ("aws", "azure", "ibm", "Microsoft azure", "aws iot",
                "alicloud", "oracle cloud", "cisco")
cloud_tuple


Q 1:  Find the length of the tuple cloud_tuple?

# Write the code and share the result in comment section.

Q 2: Access the element, with respect to index 4?

# Write the code and share the result in comment section.

Q 3:Use slicing to obtain indexes 3, 4 and 5:

# Write the code and share the result in comment section.
.
Q 4: Find the first two elements of the tuple cloud_tuple

# Write the code and share the result in comment section.

Q 5:Find the first index of “azure”

# Write the code and share the result in comment section.
Q 6: Generate a sorted List from the Tuple my_tuple(-1,-3,7,0)

# Write the code and share the result in comment section.


Read More : Python Doc

No comments:

Post Top Ad

Your Ad Spot