Course
Tuples
Python Tutorial
This Python tutorial has been written for the beginners to help them understand the basic to advanced concepts of Python Programming Language. After completing this tutorial, you will find yourself at a great level of expertise in Python, from where you can take yourself to the next levels to become a world class Software Engineer.
Tuples
Tuple is one of the built-in data types in Python. A Python tuple is a sequence of comma separated items, enclosed in
parentheses ()
. The items in a Python tuple need not be of same data type.Following are some examples of Python tuples
tup1 = ("Rohan", "Physics", 21, 69.75)tup2 = (1, 2, 3, 4, 5)tup3 = ("a", "b", "c", "d")tup4 = (25.50, True, -55, 1+2j)
In Python, tuple is a sequence data type. It is an ordered collection of items. Each item in the tuple has a unique position index, starting from 0.
In C/C++/Java array, the array elements must be of same type. On the other hand, Python tuple may have objects of different data types.
Python tuple and list both are sequences. One major difference between the two is, Python list is mutable, whereas tuple is immutable. Although any item from the tuple can be accessed using its index, and cannot be modified, removed or added.
Python Tuple Operations
In Python, Tuple is a sequence. Hence, we can concatenate two tuples with
+
operator and concatenate multiple copies of a tuple with "*
" operator. The membership operators "in
" and "not in
" work with tuple object.Note that even if there is only one object in a tuple, you must give a comma after it. Otherwise, it is treated as a string.
Practice with Online Editor
Note: This Python online Editor is a Python interpreter written in Rust, RustPython may not fully support all Python standard libraries and third-party libraries yet.
Remember to save code(Ctrl
+S
Or
Command
+S
) before run it.