Python tuples tutorial

In these Python tuples tutorial lern Tuples. Accessing Values in Tuples. To access values in tuple, use the square brackets for slicing along with the index. A tuple is like a list of a sequence of immutable python objects. Tuple is collection of ordered and unchangeable they are written in round brackets. They are separated by commas in python. Similar like the list, sequencing indexing and repetition but tuple is immutable and list are mutable..The difference between the list and the tuple is the list is declared in square brackets and tuple are declared in circle brackets. The values to be stored in the tuple must be of any type and index by the integers. The tuple()b function is also called as build in function used to create a tuple. This function accepts the single parameter.

The Advantages of tuple verses list:-

The elements of list are mutable and in tuple are immutable.
The elements of a list are mutable whereas the elements of a tuple are immutable.
Elements in tuple are enclosed in parenthesis and in list are enclosed in square brackets.
 If we want to change the data over time tuple is preferred to the data types and when needed to change data in future list is the option.
The Iteration over the elements of a tuple is faster compared to iterating over a list.
Creating Tuple:-

Create an empty list in tuple:-

#An creation of empty tuple
e.g:-
Empty tuple=()
Print(empty_tuple)
O/p:-
()

Syntax of tuple:-

tup=(‘jan’,’march’)
You need to write as two parentheses containing nothing to write an empty tuple,
e.g:-
tup2= ();
For writing a single value:-
For writing tuple for a single value, need to include a comma, even there is a single value. Also at the end you need to write semicolon as shown
Tup1=(50,);.
Accessing Tuple:-  
Concatenation of Tuples:-
e.g:-
tuple1= (‘java’,’python’)
tuple1= (‘ram’,’sita’)
#concatenation is done
Print (tuple1+tuple2)
O/p:-
(‘java’,’python’,’ram’,’sita’)

Nesting of Tuples:-

The following is the code for creating nested tuple
e.g:-

tuple1= (‘java’,’python’)
tuple2= (‘ram’,’sita’)
#nesting of tuple is done
tuple3=(tuple1+tuple2)
Print (tuple3)
O/p:-

((‘java’,’python’),(’ram’,’sita’))
Slicing in Tuples:-

tuple1 = (1, 2, 3, 4)
Print (tuple1 [1 :])

O/p:-
2

Deleting a Tuple:-

e.g:-
tuple1 = (1, 2, 3, 4)
del tuple1
Print (tuple1)

O/p:-
(1, 2, 3, 4)

Finding Length of a Tuple:-

e.g:-
tuple1= (‘ram’,’sita’,’savita’)
Print (len(tuple1))

O/p:-
3

Converting list to a Tuple:-

e.g:-
List1= [‘ram’,’sita’,’savita’]
Print (tuple(list1))
Print (tuple('java')

O/p:-
(‘ram’,’sita’,’savita’)
(‘p’,’y’,’t’,’h’,’o’,’n’)

It will take the single parameter which may be list string or dictionary and convert it into the tuple.
e.g:-
thistuple =("apple", "banana", "cherry")
print(thistuple[1])

Additional Services : Refurbished Laptops Sales, Python Classes, Share Market Classes And SEO Freelancer in Pune, India