Tuples In Python

Python Collections (Arrays)

If you haven’t check out the last blog Go and read: Declare variable in python

Install Python on windows

There are four types of collection datatype in python.

 

In this post, we will learn about Tuple in python.

  • Tuple is similar to List only the difference is that  List values are changeable and tuple values are nonchargeable (static values).
  • Tuple is a collection of multiple data that is ordered and unchangeable.in python list is written in square brackets.
  • Tuples are declared in circular brackets
  • Example :
Articles = ("python", "C#", "Angular","asp.net","asp.net core")
print(Articles)

Output : (‘python’, ‘C#’, ‘Angular’, ‘asp.net’, ‘asp.net core’)

 

Access Tuple From Index

You can access the Tuple items by the index number:

Articles = ("python", "C#", "Angular","asp.net","asp.net core")
print(Articles[1])
#If you want to access the Tuple from reverse order you can use the negative index
print(Articles[-1])

Output :

C#

asp.net core

Range of index

You can give a specific range to you are Tuple, by giving  the starting point end the ending point

When You give range to a list a new list is been created and displayed to us.

Articles = ("python", "C#", "Angular","asp.net","asp.net core")
print(Articles[2:4])
#By leaving out the start value, the range will start at the first item
print(Articles[:4])
#By leaving out the end value, the range will go on to the end of the list:
print(Articles[1:])
#Specify negative indexes if you want to start the search from the end of the list
print(Articles[-4:-2])

Output :

(‘Angular’, ‘asp.net’)

(‘python’, ‘C#’, ‘Angular’, ‘asp.net’)

(‘C#’, ‘Angular’, ‘asp.net’, ‘asp.net core’)

(‘C#’, ‘Angular’)

 

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories