create a tuple in python

A tuple is a collection of ordered and immutable elements enclosed in parentheses () and separated by commas ,. To create a tuple in python, you can simply enclose the elements within parentheses as follows:

main.py
my_tuple = (1, 2, 3, 'apple', 'banana')
40 chars
2 lines

In the code above, we create a tuple my_tuple that contains integers and strings. To access the elements of a tuple, you can use indexing where the first element is at index 0. For example:

main.py
print(my_tuple[0])   # Output: 1
print(my_tuple[-1])  # Output: 'banana'
73 chars
3 lines

gistlibby LogSnag