Tuples in Python || Python Tuples || Python Tutorial || Live Demo with Example

Опубликовано: 15 Сентябрь 2024
на канале: Topictrick
103
6


Python Tuples: Holla! Welcome back to another exciting Python Tutorial on Tuples in Python. A Python tuples is a Python sequence (i.e. Built-in Data type) which stores a group of items or elements. Tuples in Python are similar to lists. But there is a significant difference between Python Tuples and Python Lists.

Tuples in Python are immutable like strings whereas Lists are mutable. Python Tuples once modified created cannot be modified because they are immutable.

Tuples are generally used store constant values (i.e. values which do not change during the python program.)

Creating a Tuples in Python.

Python Tuples are created by using parentheses () and each elements should be delimited by comma ',' following are couple of examples:

1. a_tpl = () # Empty Python tuples
2. b_tpl = (1, 2, 3, 4, 5) # Tuple with integer values.
3. c_tpl = ('a', 'c', 'd', 'e', 'f') # Tuple with character values.
4. d_tpl = (1, 2, ('a', 'b')) # Nested Tuples in Python.
5. e_tpl = tuple(range(20)) # Range function to create tuple.
6. b_tpl[0] # Print 1 item from Tuples in Python.

Tuple Functions:

Since tuples in Python are immutable, so you cannot use append(), pop(), sort(), reverse() methods, but you can use basic built-in functions. Following are a list of functions that can be used with Python tuples:

1. Len() :: To get length of tuple.
2. Min() :: To get min value from the tuples in Python.
3. Max() :: To get max value from the tuples in Python.
4. Count() :: To get count of element in tuples in Python .
5. Sorted() :: To sort the tuple items.
6. Index() :: To get index of the element.

How to append an item to the tuples in Python:

1. Convert tuple into lists by using the list method.
2. Append item to list using append method.
3. Convert lists tuple by using tuple method.

Python Tuples Example:
a_tpl = (1, 2, 3, 4)
b_tmp = lists(a_tpl)
a_tmp.append('topictrick')
a_tpl = tuple(a_tmp)

For loop to print Tuples in Python.

for i in a_tpl:
print('Items of tuple :', i)

Output:
Items of Tuple: 1
Items of Tuple: 2
Items of Tuple : 3
Items of Tuple: 4
Items of Tuple: topic trick

Website : www.topictrick.com
Youtube : topictrick
Twitter :
Facebook :
Linkedin :
Redditt :
Pinterest :
Python Tutorial Link:

Tags

Topictrick: Thanks for watching the video. Please leave your feedback and share our video with friends if you like our video.

-~-~~-~~~-~~-~-
Please watch: "How to use pandas read_csv function || Python read_csv pandas || pd.read_csv In 5 Min."


-~-~~-~~~-~~-~-