Jan 9, 2020

TUPLE IN PYTHON

TUPLE IN PYTHON

THIS POST CONSIST OF TUPLE IN PYTHON AND HOW TO USE TUPLE IN PYTHON LANGUAGE WITH EXAMPLES AND WRITTEN CODES IN PYTHON LANGUAGE FOR BETTER UNDERSTANDING OF PYTHON TUPLE.

Tuple is the same as the list but here we cannot change the values as the values in the tuple are fixed and we cannot performs a function like extend, append, insert, etc in this list.

Why it is important to use a tuple.this is the most obvious question coming into your mind as you know there are certain projects or tasks we perform in our life when we want the values that we have assign should not be manipulated by anyone that time we can use Tuple as this function didn't allows users to change the values from the list.

Syntax of Tuple in python

"In the list, we used square brackets [] but in the tuple, we are using the circular brackets ()."
>>> tup=(12,131,45,46,15,54,35,11) tuple list
>>> tup[3] checking the list by entering index number
46               At index number 3 we have 46
>>> tup[6]
35
>>> tup[0]  At index number 0 we have number 12
12 


let's see one more example and this time we are going to use the string instead of numbers so that you can understand better.


>>> planet=("earth","jupiter","mars","mercury")

>>> planet[2]

'mars'
>>> print(planet)  by using the print function we are printing the all the values present in the tuple list.

('earth', 'jupiter', 'mars', 'mercury')
As you can see from above that the output is showing all the values in the tuple list.

Using different functions in Tuple

we can perform certain arithmetic operations on the tuple to show the desired element from the tuple list or performing some functions like printing the desired numbers of results like first 5 elements or the last 3 elements like that.


If you don't know how to perform these arithmetic operations, What is the syntax for this, then you can refer to my post in which I have explained all the points related to arithmetic operations in python.


>>> planet
('earth', 'jupiter', 'mars', 'mercury')

>>> print(planet[1:3])
('jupiter', 'mars')

Assigning the range to print.

How to find length of the tuple in python

to find the length of the tuple means finding the number of elements in the list of tuple we are using the function called "Length" and how to use that function is written below,

>>> planet

('earth', 'jupiter', 'mars', 'mercury')

>>> print(len(planet))
4

As you can see from the above example we are using the function Length and to use that function we have to provide the parameter to function called len by using this function we can find the length of the tuple or any other list in terms of an integer that is a number of elements in the list.

tuple have various applications as it is used to define the values which are not going to change throughout the process so that any operation cannot change the values present in this tuple list.

thanks for reading for complete tutorials for python you can visit my page "python tutorials" at the top of this blog thanks for reading.






No comments:

Post a Comment

Popular Blog Posts