what is python tuple code example
Example 1: what is a tuple in python
# A tuple is a sequence of immutable Python objects. Tuples are
# sequences, just like lists. The differences between tuples
# and lists are, the tuples cannot be changed unlike lists and
# tuples use parentheses, whereas lists use square brackets.
tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = "a", "b", "c", "d";
# To access values in tuple, use the square brackets for
# slicing along with the index or indices to obtain value
# available at that index.
tup1[0] # Output: 'physics'
Example 2: tuples in python
my_tuple = 3, 4.6, "dog"
print(my_tuple)
# tuple unpacking is also possible
a, b, c = my_tuple
print(a) # 3
print(b) # 4.6
print(c) # dog
Example 3: how to use tupels python
tupel = ('banana',10,True)
print(tupel[2])
Example 4: tuple in python
#a tuple is basically the same thing as a
#list, except that it can not be modified.
tup = ('a','b','c')
Example 5: what are tuples in python
#A tuple is essentailly a list with limited uses. They are popular when making variables
#or containers that you don't want changed, or when making temporary variables.
#A tuple is defined with parentheses.
Example 6: python tuple methods
count(x) : Returns the number of times 'x' occurs in a tuple
index(x) : Searches the tuple for 'x' and returns the position of where it was first found