what are tuples used for in python 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: python tuple vs list

#tuples and lists are the same thing, but a tuple cannot be changed
tup = (1,'string',True)
lst = ['hiya',23545,None]

Example 3: what is a tuple in python

# A tuple is a sequence of immutable Python objects. Tuples are
# sequences, just like lists. The differences between tuples
# and lists are, the tuples cannot be changed unlike lists and
# tuples use parentheses, whereas lists use square brackets.
tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = "a", "b", "c", "d";

# To access values in tuple, use the square brackets for
# slicing along with the index or indices to obtain value
# available at that index.
tup1[0] # Output: 'physics'

Example 4: tuple in python

#a tuple is basically the same thing as a
#list, except that it can not be modified.
tup = ('a','b','c')

Example 5: pyhton tuple

#It's like a list, but unchangeable
tup = ("var1","var2","var3")
tup = (1,2,3)
#Error

Tags:

C Example