tuples functions python 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: changing tuple values

x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)

print(x)

Example 3: tuples in python

my_tuple = ("hello")
print(type(my_tuple))  # <class 'str'>

# Creating a tuple having one element
my_tuple = ("hello",)
print(type(my_tuple))  # <class 'tuple'>

# Parentheses is optional
my_tuple = "hello",
print(type(my_tuple))  # <class 'tuple'>

Example 4: 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 5: tuples in python

# the tuples are like the Read-Only they are not to be changed or modified they're constant

letters = "a", "b", "c", "d"    # you can use () as they are optional
print(letters)
"""
    tuples are immutable but it's important because they don't returns the bugs
    
    these are sequence types means you can iterate over them by it's index numbers
    
    tuples data can't be changed but list's data can be changed
"""

Tags:

C Example