- Get link
- X
- Other Apps
Tuples in Python
Hello Programmers! In this tutorial, I will tell you about python tuple. This is an intermediate concept of python. So let's get started.
Now a tuple is a type of data structure which basically means it's a container where we can store different values. Now if you're familiar with lists in Python a tuple is actually very similar to a list it's basically a structure where we can store multiple pieces of information. But, a tuple has a few key differences from lists that we're going to talk about in this tutorial. The first thing I want to do is I want to show you through how to create a tuple.
Example
coordinates = (4,5)
Print(coordinates[0])
So I can come down here and I can create a tuple and one of the most common examples of tuples is coordinates. So let's say I had a series of like XY coordinates I could create a variable called coordinates and I'm just gonna set this equal to an open and closed parenthesis and whenever we want to create a tuple inside of Python. We're gonna use these open and closed parenthesis and inside of here inside of these parentheses we're gonna put the values that we want to store so I could for example put a list of numbers so this could be like my coordinates I can have like 4 and 5 so this would be my with coordinates inside of it and one of the things about tuples that makes them unique is a tuple is immutable and you'll hear that word a lot when we're talking about tuples immutable it basically means that the tupple can't be changed or modified once we create our tupple up here you cannot modify it you cannot change it you can't add elements to it you can't erase elements from it you can't change any of the elements inside the tupple what-you-see-is-what-you-get once you create the tupple it's it's as it as you can't change so I can come down here and I can print out some of the attributes inside the tupple and the way I can access them is just by using an open and closed square bracket and in here I canpass in a index so tuples are indexed starting at 0 so technically we would say that 4 is that index position one in Ferb sorry for is it index position zero and five is that index position one so if I said coordinates zero now it should print out four and down here we're printing out four so if I wanted to print out that five we can just put a 1 inside of the square brackets and now we'll print out that 5 so I can access the elements inside of a toughie I can easily access each entry but if I was to come over here and try to change one of the elements so if I said coordinates and coordinates 1 and I tried to set it equal to something else like 10 Python is actually gonna give us an error when we run this program so I'm
coordinates = (4,5)
Print(coordinates[0])
gonna run the program you see we get this error it says type error tupple object does not support item assignment because it's immutable because we can't change it so that's basically, what tuples are how we can create them and you know a couple like I said it's very simple you just create it and then you have it and now I want to talk to you guys about a common topic in Python which is the difference between tuples and lists so remember a list is basically doing the same thing a list if we wanted to create a list we would just use square brackets instead of those parentheses and if I was using a list I could assign different values to it I can I could basically mutate any of the elements I could add delete modify change whatever I wanted with the list. But with the tupple, we can't do that and that's sort of the basic difference and really a Python doesn't say anything more than that it basically just says like one of them is immutable one of them is not but in practical use cases generally people will use tuples for data that's never gonna change so you'll store data inside of a tuple when that data doesn't need to be mutated that's why something like coordinates is such a good candidate for a tupple because once you have coordinates like we're probably not gonna modify them right we might add other coordinates so for example if I wanted I could create a list of tuples so I could say like 4 or 5 6:7 maybe we have some other coordinates like a t-34 so here I have you know I have a list and inside of it we have these tuples but remember these tuples can't be modified the values can't be changed so really it's just a case-by-case basis I would say by a huge majority you're gonna be using lists most of the time tuples are more of a niche you know they're using more special situations but there are plenty of situations where you want to use tuples so if you're in a situation where you want to store data that can't be changed or mutated then tuples are the way to go so yeah that's just some basics about tuples some basics about the difference between lists and tuples and so a tuple is a great thing to add into your Python repertoire.
Congratulations! Now you know what is tuple in python. If you have any confusion then you can comment below.
Happy Coding :)
tuples in python,python tuples,tuple,python,python tuples tutorial,python tutorial,python tuple,tuples,learn python,tuple in python,what are tuples in python?,what are tuples in python,tuple examples in python,tuple in python 3,python programming,python 3,python tutorial for beginners,python for beginners,data structures in python,python tuples explained,python lists tuples,tuples in python 3,learn tuples in python,python tuples vs lists,list and tuples in python,nested tuples in python
Comments
Very Informative and creative contents. Keep posting More Blogs
ReplyDelete