Python Tuples

In this Python tutorial we will learn about Python Tuples and we will also cover different examples related to Python Tuples.

Python Tuples

In Python the tuple are same as a list in term of indexing, nested objects and it is a group of objects that are separated by commas. The tuple are immutable but the lists are mutable.

Example:

In the following example we are making the tuple which are the collection of the objects which are separated by commas.

  • tup = (“Python”, “T”, “point”) is used to create a tuple.
  • print(tup) is used to print the tuple by using print() function.
# Python Tuple
tup = ("Python", "T", "point")
# Printing tuple
print(tup)

Output:

After running the above code we get the following output in which we can see that the tuples are creating that is separated by commas.

Python Tuple
Python Tuple

Github Link

Check this code in Repository from Github and you can also fork this code.

Github User Name: PythonT-Point

Creating Python Tuples

The python tuples are created with the help of () operator and it is the collection of objects and are separated by commas.

Example:

In the following example we are creating the python tuple with the help of tuple[] statement and then print the tuple with the help of print() function.


tup : tuple[int | str, ...] = (2,4,6,"Pythontpoint")
 
print(tup)

Output:

In the following output we can see that the python tuple is created, we are defining a variable called values that can holds the tuple.

Creating Python Tuple
Creating Python Tuple

Read: Python Dictionary

Accessing values in Python Tuples

We are accessing the values in python tuples and we creating the tuple by using the square brackets, we can get the values from the tuples.

Example:

In the following example we will access the values in tuples, using the square brackets, we can get the values from the tuples.

tup = ("Python", "t", "point")
 
print("Value in tup[0] = ", tup[0])
print("Value in tup[1] = ", tup[1])
print("Value in tup[2] = ", tup[2])

Output:

After running the above code we get the following output in which we can see that the values are accessing in the python tuple.

Accessing the values in python tuple
Accessing the values in python tuple

Read: Python Strings

Slicing Python Tuples

After accessing the values in the tuple here we are slicing the python tuple, using the square brackets, we can get the values from tuple.

Example:

In the following code we will creating the tuple and getting the values from the tuple by using the square bracket.

# slicing the tuple
 
tup = (1,2, 3, 4)
print(tup[2:])
print(tup[::-2])
print(tup[1:3])

Output:

After running the above code we get the following output in which we can see that the tuple can be sliced.

Slicing the python tuple
Slicing the python tuple

Read:Python Literals

Deleting Python Tuples

Here we are deleting the tuples simply by using the del statement and then printing this delete tuple by using the print() function.

Example:

In the following example we are deleting the tuple. The tuple can be deleted by using the del statement.

# deleting a tuple
 
tup = ( 1, 2)
del tup
print(tup)

Output: After running the above code we get the following output in which we can see that the error occur because after using del statement the tuple is deleted that’s why the error is occur.

Deleting the Python tuples
Deleting the Python tuples

Finding length of Python Tuple

Here we are finding the length of the Python tuple. For finding the length we are using the len() function.

Example:

In the following code we will finding the length of the python tuple by simply using the len() function and after finding the length print this length by using print() function.

# printing the length of a tuple
 
tup = ('python', 't', 'point')
print(len(tup))

Output:

After running the above code we get the following output in which we can see that the length of the tuple is printed on the screen.

Finding length of Python Tuple
Finding length of Python Tuple

So, in this tutorial, we have learned about the Python Tuples and we have covered all these topics.

  • Python Tuples
  • Creating Python Tuples
  • Accessing values in Python Tuples
  • Slicing Python Tuples
  • Deleting Python Tuples
  • Finding length of Python Tuple

Do follow the following tutorials also:

29 thoughts on “Python Tuples”

  1. hiI like your writing so much share we be in contact more approximately your article on AOL I need a specialist in this area to resolve my problem Maybe that is you Looking ahead to see you

    Reply
  2. I applaud your devotion. I’ve looked at your sketch, and the material you’ve created is excellent. Nevertheless, you appear to be apprehensive about the prospect of heading in a direction that could cause discomfort. I agree with you that you will be able to deal with this issue in a timely manner.

    Reply
  3. I loved as much as you will receive carried out right here The sketch is attractive your authored material stylish nonetheless you command get got an impatience over that you wish be delivering the following unwell unquestionably come more formerly again since exactly the same nearly a lot often inside case you shield this hike

    Reply
  4. Wonderful beat I wish to apprentice while you amend your web site how could i subscribe for a blog web site The account aided me a acceptable deal I had been a little bit acquainted of this your broadcast provided bright clear idea

    Reply
  5. Your work has captivated me just as much as it has captivated you. The visual display is elegant, and the written content is impressive. Nevertheless, you seem concerned about the possibility of delivering something that may be viewed as dubious. I agree that you’ll be able to address this issue promptly.

    Reply
  6. Program iz naturally like your web site however you need to take a look at the spelling on several of your posts. A number of them are rife with spelling problems and I find it very bothersome to tell the truth on the other hand I will surely come again again.

    Reply
  7. Real Estate You’re so awesome! I don’t believe I have read a single thing like that before. So great to find someone with some original thoughts on this topic. Really.. thank you for starting this up. This website is something that is needed on the internet, someone with a little originality!

    Reply
  8. you are in reality a good webmaster The website loading velocity is amazing It sort of feels that youre doing any distinctive trick Also The contents are masterwork you have done a fantastic job in this topic

    Reply
  9. helloI like your writing very so much proportion we keep up a correspondence extra approximately your post on AOL I need an expert in this space to unravel my problem May be that is you Taking a look forward to see you

    Reply
  10. Лучшие онлайн-казино предлагают своим пользователям широкий выбор игр, включая слоты, рулетку, блэкджек, покер и другие. Кроме того, они часто предлагают бонусы и фриспины новым игрокам, что позволяет им начать играть без необходимости вносить депозит. Регистрация в онлайн-казино обычно занимает несколько минут и требует минимальной информации о пользователе. рейтинг топ лушчее andnmmkxaf

    Reply
  11. What i do not realize is in fact how you are no longer actually much more wellfavored than you might be right now Youre very intelligent You recognize thus considerably in relation to this topic made me in my view believe it from numerous numerous angles Its like men and women are not fascinated until it is one thing to do with Lady gaga Your own stuffs excellent All the time handle it up

    Reply
  12. Blue Techker You’re so awesome! I don’t believe I have read a single thing like that before. So great to find someone with some original thoughts on this topic. Really.. thank you for starting this up. This website is something that is needed on the internet, someone with a little originality!

    Reply

Leave a Comment