Showing posts with the label Tuples

Python Programming Language - Tuples

Just like a list, a tuple is a collection of elements. However, unlike lists, tuples are immutable. This means that you can't add or remove any elements from a tuple, and you can't change anything in a tuple. To create a tuple, you need to place comma-separated elements inside parentheses: carl = ( "Carl" , 19 ) print(carl)     # ("Carl", 19) Additionally, we don't need to use parentheses when defining a tuple—we may just list the values separated by commas: carl = "Carl" , 19 print(carl)     # ("Carl", 19) To only have one item in a tuple, you need to add a comma after it. Otherwise, Python will not accept it as a tuple. carl = ( "Carl" ,) print(carl)   To create a tuple from another set of elements, such as a list, you can pass the list to the tuple() function , which will return the tuple: twd = [ "Carl" , 37 , "Rick" ] data = tuple(twd) print(data)       # ('Carl', 37, 'Rick') Using th