tuple in python it is a type in Python.Tuple means ordered collection.
example = (‘one’,’two’,’three’);
the output will be one, two, three
output be will
example = 1 error tuple cannot be changed and the list can be changed.
difference between tuple and lists in python:
In list’s element can be changed but in a tuple, it cannot be changed.
In tuple, you can make a loop and can make mixed tuple,
examples of a tuple in python:
mixed = (1,2,3,4.9,'Asad') for i in mixed: print(i) the result will be 1 2 3 4.9, Asad
2nd Example of tuple in python:
mixed = (1,2,3,4.9)
the result will be 4 because len function will print the length of items.
it will print 0 1 2 3 4
because length of mixed is 5 so it will only print 5 items because it will be greater than the length of mixed so loop will be stop.
number = (1)
it will print the datatype of number
result of this tuple will be int
name = (‘Rashid’,)
it will print tuple if you remove comma then it will print string.
Python_class = ‘ Umer’,’Aslam’, 23,
it will print tuple
tuples are faster than list. In tuple you cannot change the data.
you can also define tuple like this
This will assign one tuple value to other tuple value
Ik, Do, Teen = (example)
it will print one two three
List_Within_tuple = (21,4.50,’Rashid’,[‘this’,2.90,’Python_class’])
list() it will create list e.g nums = list(nums)
nums1 = str(num)
it will convert number into string.
max = 1,2,3,4.9