Let us go to the next tutorial in the Python Tutorials for Beginners. This Python beginner tutorial explains the tuple sequence data type. Please view Python video tutorial 17 or read on... What are tuples? In Python programming, a tuple is a sequence of items. The tuple items may be of the same data type or different data types like integers, floats, strings, lists or other tuples. The tuple items are commonly of different data types. Tuples are immutable, meaning that after a tuple is defined, no tuple item can be updated. Also, a tuple that is an item in another tuple is called a nested tuple. The tuple syntax is:
item0, item1, item2,..., itemn
The tuple syntax can also be the items within parentheses.
(item0, item1, item2,..., itemn)
In the above tuple format, the items are separated by commas. The item indexes start from 0. Now, let us see Python tuple examples and tuple operations with Python examples.
# Python code
my_tuple = 1, 2, 10.23, 'Hello', 100
# print the Python tuple i.e. (1, 2, 10.23, 'Hello', 100)
print (my_tuple)
# Indexing tuple operation means getting a single item from a Python tuple
# print the first and second items of the tuple i.e. 1 and 2
item0, item1, item2,..., itemn
The tuple syntax can also be the items within parentheses.
(item0, item1, item2,..., itemn)
In the above tuple format, the items are separated by commas. The item indexes start from 0. Now, let us see Python tuple examples and tuple operations with Python examples.
# Python code
my_tuple = 1, 2, 10.23, 'Hello', 100
# print the Python tuple i.e. (1, 2, 10.23, 'Hello', 100)
print (my_tuple)
# Indexing tuple operation means getting a single item from a Python tuple
# print the first and second items of the tuple i.e. 1 and 2
print (my_tuple[0], my_tuple[1])
# Slicing tuple operation means getting a continuous section of a Python tuple
# print all items of the tuple from index 2
print (my_tuple[2:])
# print all items of the tuple from index 1 up to but excluding index 3
print (my_tuple[1:3])
# Unpacking tuple operation means getting multiple variables assigned from a Python tuple
a, b, c, d, e = my_tuple
# print the values of variables a, b, c, d and e i.e 1, 2, 10.23, 'Hello' and 100
print (a, b, c, d, e)
# A nested tuple is a tuple within another tuple
# ('b', 'c') is the nested tuple with index 1 in nt tuple
nt = 'a', ('b', 'c'), 1
# print the nested tuple i.e. print ('b', 'c')
print (nt[1])
# tuple operations are in, not in and +
# in tuple operation prints True because 1 is an item in nt tuple
print (1 in nt)
# not in tuple operation prints False because 1 is an item in nt tuple
print (1 not in nt)
# + tuple operation concatenates two tuples
my_tuple1 = 10, 20
my_tuple2 = 30, 40, 50, 60
print (my_tuple1 + my_tuple2)
# tuple functions
# len tuple function gives the number of items in the tuple
# print the number of items in my_tuple1 i.e print 2
print (len(my_tuple1))
# min tuple function gives the smallest item in the tuple i.e. print 10
print (min(my_tuple1))
# max tuple function gives the largest item in the tuple i.e print 20
print (max(my_tuple1))
# Slicing tuple operation means getting a continuous section of a Python tuple
# print all items of the tuple from index 2
print (my_tuple[2:])
# print all items of the tuple from index 1 up to but excluding index 3
print (my_tuple[1:3])
# Unpacking tuple operation means getting multiple variables assigned from a Python tuple
a, b, c, d, e = my_tuple
# print the values of variables a, b, c, d and e i.e 1, 2, 10.23, 'Hello' and 100
print (a, b, c, d, e)
# A nested tuple is a tuple within another tuple
# ('b', 'c') is the nested tuple with index 1 in nt tuple
nt = 'a', ('b', 'c'), 1
# print the nested tuple i.e. print ('b', 'c')
print (nt[1])
# tuple operations are in, not in and +
# in tuple operation prints True because 1 is an item in nt tuple
print (1 in nt)
# not in tuple operation prints False because 1 is an item in nt tuple
print (1 not in nt)
# + tuple operation concatenates two tuples
my_tuple1 = 10, 20
my_tuple2 = 30, 40, 50, 60
print (my_tuple1 + my_tuple2)
# tuple functions
# len tuple function gives the number of items in the tuple
# print the number of items in my_tuple1 i.e print 2
print (len(my_tuple1))
# min tuple function gives the smallest item in the tuple i.e. print 10
print (min(my_tuple1))
# max tuple function gives the largest item in the tuple i.e print 20
print (max(my_tuple1))
Want to learn more about tuples like more tuple functions like index, count, tuple with Python samples? Please view my Python video tutorial 17. Thank you.
Very Nice Tutorial. Thank you for explaining Python very well. All your tutorials are very good. You should make similar posts which will greatly benefit the freshers.
ReplyDelete