WebNovelPYTHON100.00%

methods

| Tuple methods in Python

1. len() method

This method returns number of elements in a tuple.

Syntax:

len()

refers to user defined tuple whose length we want to find.

Example:

>>> T1=(10,20,30,40) >>> len(T1) 4 #There are 4 element in tuple.

2. max()

This method returns largest element of a tuple. This method works only if the tuple contains all values of same type. If tuple contains values of different data types then, it will give error stating that mixed type comparison is not possible:

Syntax :

max()

refers to name of tuple in which we want to find maximum value.

Example 1:

>>> T1=[10,20,30,40] >>> max(T1) 40 # 40 is the maximum  value in tuple T1.

 Example 2:

>>> T2=['Sumit','Anil','Rahul'] >>> max(T2) 'Sumit' #'Sumit' appears as last member as per English dictionary.

 Example 3:

>>> T3=['Sumit',10,'Anil',11,'Rahul',12] >>> max(T3) Traceback (most recent call last): File "", line 1, in max(T3) TypeError: '>' not supported between instances of 'int' and 'str' #Maximum can't be found in mixed values.

Example 4:

>>> max([3,4],(5,6)) Traceback (most recent call last):   File "", line 1, in     max([3,4],(5,6)) TypeError: '>' not supported between instances of 'tuple' and 'tuple'   #Maximum can't be found among tuple and tuples.

 3. min()

This method returns smallest element of a tuple. This method works only if the tuple contains all values of same type. If tuple contains values of different data types then, it will give error stating that mixed type comparison is not possible:

Syntax :

min()

refers to name of tuple in which we want to find minimum value.

Example 1:

>>> T1=[10,20,30,40] >>> min(T1) 10 #10 is the minimum  value.

 Example 2:

>>> T2=['Sumit','Anil','Rahul'] >>> min(T2) 'Anil' #'Anil' appears first as per English dictionary.

 Example 3:

>>> T3=['Sumit',10,'Anil',11,'Rahul',12] >>> min(T3) Traceback (most recent call last): File "", line 1, in min(T3) TypeError: '<' not supported between instances of 'int' and 'str' #Minimum can't be found in mixed values.

Example 4:

>>> min([3,4],(5,6)) Traceback (most recent call last): File "", line 1, in min([3,4],(5,6)) TypeError: '<' not supported between instances of 'tuple' and 'tuple' #Minimum can't be found among tuple and tuples.

 4. index()

This method is used to find first index position of value in a tuple. It returns error if value is not found in the tuple.

Syntax:

Tuple.index ()

Tuple is user defined tuple.

 refers to the value whose index we want to find in Tuple.

Example 1:

>>> T1=[13,18,11,16,18,14] >>> print(T1.index(18)) 1 #Index of first occurance of 18 is shown i.e. 1.

Example 2:

>>> print(T1.index(10)) Traceback (most recent call last): File "", line 1, in T1.index(10) ValueError: 10 is not in tuple # Above example shows error as 10 is not in the tuple

5. count()

This function is used to count and return number of times a value exists in a tuple. If the given value is not in the tuple, it returns zero.

Syntax:

Tuple.count()

refers to the value whose count we want to find.

Example 1:

>>> T1=[13,18,11,16,18,14] >>> T1.count(18)  #18 appears twice in tuple T1. 2

 Example 2:

>>> T1=[13,18,11,16,18,14]

>>> T1.count(30)       

0 #0 is the output as 30 doesn't exist in the tuple T1.

 6. tuple()

This method is used to create a tuple from different types of values.

Syntax:

Tuple()

refers to a sequence type object that we want to convert to a tuple.

Example 1 – Creating empty tuple

>>> t=tuple() >>> t ()

Example 2 – Creating a tuple from a list

>>>t=tuple([1,2,3]) >>>t (1,2,3)

Example 3  – Creating tuple from a string

>>>t=tuple("abc") >>>t ('a','b','c')

Example 4  – Creating a tuple from keys of a dictionary

>>> t1=tuple({1:'a',2:'b'}) >>>t1 (1,2)

Installation

Installing Python IDLE

Using Python IDLE

Installing Anaconda

Using Anaconda

Basics

Python Intro

Literals

Data types

Working with variables in Python

Input/Output

print() function

input() function

eval() function

Operators

Basic operators

== and is

Control Structures

if Statement

Looping statements

Strings

string functions in python 3 with examples

List, Tuple, Dictionary

List

List functions & Methods

Tuple

Tuple functions & Methods

Dictionary

Dictionary Functions & Methods

Functions & Modules

Creating a Function

Types of function

Types of function arguments

Modules and Packages

Lesson tags: convert list into tuple, count number of values in tuple, covert string into tuple, find length of tuple, find maximum value in tuple, find minimum value in tuple, len() method of tuple in python, Tuple functions in Python, Tuple methods in Python

 Python Tuple with Examples | Tuple in Python 3

Python Dictionary with examples 

Back to: Python Programming Tutorial

Courses

Class 11 Computer Science Notes

Class 12 Computer Science notes

Study & Career Tips

Question Papers

Tips and Tricks

Python

C Language

SQL

Search ladderpytho

All Rights are reserved by ladderpython.com Theme by Scissor Themes Proudly powered by WordPress

Subscribe for Latest Updates