define tuples code example

Example 1: what are tuples used for

Tuples in Python

An immutable data value that contains related elements.
Tuples are used to group together related data,
such as a person’s name, their age, and their gender.

A tuple is the same as a list except uses parenthisies instead of square brackets.
A tuple is also immutable (cant be changed) unlike a list.

Example:
tup1 = ('physics', 'chemistry', 1997, 2000);

Example 2: tuples in python

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'>

Example 3: tuples

#!/usr/bin/python

tup1 = (12, 34.56);
tup2 = ('abc', 'xyz');

# Following action is not valid for tuples
# tup1[0] = 100;

# So let's create a new tuple as follows
tup3 = tup1 + tup2;
print tup3;