--Originally published at Coding The Future
Picture by Ed.ward
Greetings everyone! Time to tuple-up! Or is it bucle-up? Who cares... Just make sure to fasten your seatbelt because we are about to begin a journey!
Today, we'll be discussing two very simple yet very powerful types in Python: Lists and Tuples. You may have heard of arrays in other programming languages, but in Python, we have lists and tuples instead, which are in fact, very similar to them.
Let's begging by quickly defining what a list or tuple is. A list is a variable that can store several values, just like it's name suggests. For example, a list of all of your friends' names. A tuple, on the other hand, is a constant that can hold several values, and obviously it remains unchanged throughout the program. For example, a tuple with the months of the year. Let us begin.
Lists
Declaring a new list is quite simple. All you have to do is type the name of the list, followed by the values you would like to assign inside of square brackets. Take a look:
myFriends = ['Daniela', 'Diego', 'Christian', 'Jillian']
myLuckyNumbers = [2, 3, 7, 13, 10, 15]
As you can see above, I declared two lists. The first one, a list that stores strings, and the second one, stores integers. As you may have intuited from the example, you cannot store different data types in the same list.
Each item is stored in an index number of the list, starting from 0. So if I had to chart the second list for example, it would look something like this:
Index | Value Stored |
0 | 2 |
1 | 3 |
2 | 7 |
3 | 13 |
4 | 10 |
5 | 15 |
Consequently, if you need to work with a specific value stored in n index, you would simply call Continue reading "TUPLE UP! – Diving deeper into Lists and Tuples" →