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.
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.
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.
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.
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.
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.
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:
Wow amazing blog layout How long have you been blogging for you made blogging look easy The overall look of your web site is magnificent as well as the content
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.
Temp Mail Great information shared.. really enjoyed reading this post thank you author for sharing this post .. appreciated
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
Wow wonderful blog layout How long have you been blogging for you make blogging look easy The overall look of your site is great as well as the content
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.
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
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
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.
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.
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!
Live Coin Watch I really like reading through a post that can make men and women think. Also, thank you for allowing me to comment!
Internet Chicks Good post! We will be linking to this particularly great post on our site. Keep up the great writing
Simplywall I appreciate you sharing this blog post. Thanks Again. Cool.
Techarp This was beautiful Admin. Thank you for your reflections.
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
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
I was suggested this web site by my cousin Im not sure whether this post is written by him as no one else know such detailed about my trouble You are incredible Thanks
Лучшие онлайн-казино предлагают своим пользователям широкий выбор игр, включая слоты, рулетку, блэкджек, покер и другие. Кроме того, они часто предлагают бонусы и фриспины новым игрокам, что позволяет им начать играть без необходимости вносить депозит. Регистрация в онлайн-казино обычно занимает несколько минут и требует минимальной информации о пользователе. рейтинг топ лушчее andnmmkxaf
Your article helped me a lot, is there any more related content? Thanks!
Bu soba, içindeki yakıtın yanmasıyla oluşan ısıyı doğrudan çevresine yayar ve aynı zamanda suyun ısınmasını sağlar.
Henof very informative articles or reviews at this time.
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
SocialMediaGirls This is my first time pay a quick visit at here and i am really happy to read everthing at one place
Your article helped me a lot, is there any more related content? Thanks!
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!
Tech Learner There is definately a lot to find out about this subject. I like all the points you made
I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?