Sitemap

Python Tuple & Tuple Methods (,)

2 min readJun 22, 2022

--

Press enter or click to view image in full size

Tuples are collection are same or same or different data that are enclosed in parenthesis ( ) separated by comma , Tuple is Data structure in Python

Press enter or click to view image in full size

Creating Tuple

For creating a list you need set of values that should be enclosed in parenthesis () and separated by comma ,

alpha = (“a”,”b”,”c”,”d”)
num = (1,2,3,4,5)
#List in tuples
lint = [1,(2,3),4]

Accessing Tuples

Accessing a tuple mean how you gonna to use or access a set of value that may be all tuple elements or individual element on tuple but when you access particular element the result will be in List.


print(alpha)
print(num)
print(lint)
print(type(alpha))
print(alpha[2:5])
print(alpha[3:])
print(alpha[:4])
print(alpha[:])
print(alpha[:-2])

Tuple Methods

Press enter or click to view image in full size

Adding elements in Tuple

You can add tuple element using ‘+’ operator but basically tuple is unmutable which means once you set a value you cant alter back

alpha += (“e”,”f”)
print(alpha)

Counting in Tuple

This method help how much element present on particular Tuple.

print(alpha.count(‘a’))
print(alpha.count(‘z’))
print(num.count(5))

Copying in Tuple

This is not core method you can use variable to access that value.

#This is not core python tuple method
b = alpha
print(b)

Indexing in Tuple

To find on which place that element is presented in Tuple.

print(alpha.index(“c”))

Length in Tuple

To find the length of Tuple.

print(len(alpha))

Min and Max

To find minimum and maximum value on Tuple.

print("Maximum value in tuple is ",max(num))
print("Minimum value in tuple is ",min(num))

Sum in Tuple

To find sum value on Tuple.

print(sum(num))

Sort in Tuple

To sort all element in tuple you need to use another method to sort the tuple element.

abc = (“m”,”p”,”n”,”h”,”k”,”s”,”y”)
print(sorted(abc))

Looping in Tuple

Looping all elements in Tuple.

for value in alpha:
print(value)

I hope everyone learn through on Tuple and its methods. Follow for more topics :-)

Get Narayanan’s stories in your inbox

Join Medium for free to get updates from this writer.

Follow us on
Instagram:- https://www.instagram.com/Coder_033/
Github:- https://github.com/Coder033
Skillshare:- https://www.skillshare.com/profile/Coder033/26410885 Youtube:-https://www.youtube.com/channel/UCjTUcwOms1NJDo2UiIvGfyg

--

--

Narayanan
Narayanan

Written by Narayanan

Front-end Development | Back-end development | Data Science | Data Visualization | Hodophile | Booklover