Albert
0
Q:

Python Tuples

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
2
# 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
"""
1
#a tuple is basically the same thing as a
#list, except that it can not be modified.
tup = ('a','b','c')
4
#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.
3
# A tuple is a collection which is ordered and unchangeable. 
# In Python tuples are written with round brackets.
thistuple = ("apple", "banana", "cherry")
print (thistuple)
> ('apple', 'banana', 'cherry')
# Access an element
print(thistuple[1])
'banana'
# Once a tuple is created, you cannot change its values.
1
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'>
1

thistuple = ("apple", "banana", "cherry")

print(thistuple[-1])
 
0

New to Communities?

Join the community