--Originally published at Codebuster
Lists and tuples are two very powerful tools in python. Both of them are a variable in which you can store different values. This values can be from strings, characters or even numbers, but you can’t have two distint types in the same list.
Declaring new lists is pretty easy, you just have to chose a name of the list, open brackets and write the different values that you want to store, closing brackets afterwards, as such:
myHobbies=[“reading”, “writing”, “netflix”, “music”]
An important part to remember is that when you create a list each item gets assigned a place in the list, an index number. This starts from cero, so the first element will actually be the number 0. This index allows you to use specific values inside the list, since you can bring it out with
So if I want to remember which was the first hobby I wrote down, I would put:
Modifyin lists is actually quite simple, two easy things you can do with them is adding or substracting an item, you can do that like this:
To add: myHobbies.append(“eating”)
To delete: del myHobbies
Tuples are very similar to lists. The difference relies in that tuples can’t be altered, so once you write the values down you will not be able to modify them. Declaring tuples is quite similar to declaring lists, it goes as such:
familyMembers=(“Araceli”, “Ricardo”, “Hannia)
Indexes work the same way.