Tuples may contain multiple variables, nested Tuples, Lists or other objects. Tuples can be used for coordinates (x,y) or any other time you need to store various elements. # This returns (12345, 54321, 'hello!') - all of the values within the Tuple Print(t) # print the first value of the Tuple t # This returns 12345 - the first value inside the Tuple T = 12345, 5421, 'hello!' # Creating a Tuple with a variable name t I know this all sounds strange, but zero-based counting systems habitually confuse even the most die-hard programmer.Ī tuple consists of a number of values separated by commas, for instance: So when we talk about the ‘first element’ of a tuple we actually mean ‘the element with index 0’. It helps to refer to numbers as ‘indices’ when you use the zero-based counting system just to avoid confusion. Code would disagree with you, it would only have counted to four: Chances are you are someone who has just counted to five. Try it by counting the number of fingers on your right hand. Remember, Tuples, Lists and Dictionaries always start counting at zero, while us humans are normally used to start counting at one. You could of course declare 15,000 different variables by hand but that is generally considered to be bad practice. Sometimes -in fact quite often- you want to store a large or unknown amount of things. Tuples, Lists and Dictionaries are just a collection of things! Perhaps it’s best if we just get the obvious stuff out of the way first: We’ve already been using tuples and lists in examples and I’ve always told you not to worry about it. (Last updated: Wednesday, December 12, 2018)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |