Python Tuple

« Previous Tutorial Next Tutorial »

A tuple is basically a sequence of immutable python objects. Tuples can not be changed unlike the lists and tuples uses parentheses. Here is an example to create tuples in python:

tuple1 = ("python", "tuple", 1952, 2323, 432);
tuple2 = (1, 2, 3, 4, 5);
tuple3 = ("a", "b", "c", "d", "e");

Python Tuple Example

Here is an example program, illustrating the tuple in Python:

# Python Tuple - Example Program

tuple1 = ("python", "tuple", 1952, 2323, 432);

print(tuple1)              # this will print the complete tuple
print(tuple1[1:4])         # this will print the elements starting from 2nd till 4th
print(tuple1[1:])          # this will print the elements starting from the 2nd element
print(tuple1[0])           # this wil print the first element of the tuple
print(tuple1 * 2)          # this will print the tuple two times

Here is the sample output of the above python program:

python tuple

A tuple containing single value also have a comma like this

tuple1 = (10,);

« Previous Tutorial Next Tutorial »


Quick Links
Signup - Login - Give Online Test