Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Tuple In Python

The document provides a comprehensive overview of tuples in Python, including their creation, deletion, and operations such as concatenation and membership. It explains the characteristics of tuples, such as immutability and indexing, and discusses built-in functions and methods related to tuples. Additionally, it includes examples for better understanding of tuple manipulation and usage.

Uploaded by

alamk765432
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Tuple In Python

The document provides a comprehensive overview of tuples in Python, including their creation, deletion, and operations such as concatenation and membership. It explains the characteristics of tuples, such as immutability and indexing, and discusses built-in functions and methods related to tuples. Additionally, it includes examples for better understanding of tuple manipulation and usage.

Uploaded by

alamk765432
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Tuples in Python

 Creating Tuples
 Deleting Tuples
 Accessing Elements in Tuple
 Tuples Operations: Concatenation, Repetation, Membership, Iteration.
 Built in Tuples functions and methods.

Introduction to Tuple in Python


 Python used 4 built-in data types to store collections of data like Tuple, List, Set, and Dictionary. These all
build in types has different qualities and usage.
 A tuple is created by placing all the items (elements) inside parentheses (), separated by commas.
 The parentheses are optional, however, it is a good practice to use them.
 A tuple can have any number of items and they may be of different types (integer, float, list, string, etc.)
 A tuple is a collection of objects which is ordered and immutable (unchangeable).
 Tuples are sequences of elements, just like lists.
 Tuples are used to store multiple items in a single variable.
 When we create tuple with a single element, make sure to add a comma after the element.
 The tuple are immutable, means we cannot change the elements of a tuple once assigned.
 An element in a tuple cannot be removed or replaced (change).
 Tuple is stored in a single block of memory, so creating a tuple is faster than creating a list.
 Tuple makes your code safer because data in tuple is “write-protected”.
 Tuples are iterable so it contains an index and an item from the original iterable.
 Tuples can be used + (concatenation) and * (repetition) operators and returns a new tuple.
 Benefits of Tuple
o It is very easy to create tuple and store data.
o Tuple can give the programmer and the interpreter a hint that the data should not be changed.
o Tuples are commonly used as the equivalent of a dictionary without keys to store data.
o Reading data is simpler when tuples are stored inside a list.
 Slicing and Indexing of Tuple
o To index or slice a tuple you need to use the [] operator on the tuple.
o When indexing a tuple.
o Provice Positive Indexing: if you provide a positive integer, it fetches that index from
the tuple counting from the left.
o Negative indexing: if you provide a negative integer, It fetches that index from the tuple counting
from the right.
 Tuples can be nested.
o Tuples can contain other compound objects, including lists, dictionaries, and other tuples.
Hence, tuples can be nested inside of other tuples.
Subject: Data Structure Print Date: [22/Apr/21], Page 1 of 8
 Basic operations with Tuples.
o Accessing Values in Tuples: To access values in tuple, use the square brackets for slicing
along with the index or indices to obtain the value available at that index.
o Updating Tuples: Tuples are immutable, which means you cannot update or change the values
of tuple elements. But we can concatenate 2 tuples and it returns 3rd Tuple.
o Delete Tuple: We can delete the tuple.

Creating tuple in python?


 Example to create empty tuple.

 Example to create tuple with single element (with and without paranthesis).

 Example to create tuple with multiple element (with and without paranthesis).

 Example to create Nesting of Touples.

Subject: Data Structure Print Date: [22/Apr/21], Page 2 of 8


 Example to show we can not change the values of Tuple.

 Example of Creating Tuple with Slicing.


o Positive Indexing

o
o Negative Indexing

Subject: Data Structure Print Date: [22/Apr/21], Page 3 of 8


 Deleting Tuple in Python.

 Example to access individual element using positive or negative indexing.

TUPLE OPERATIONS
 Tuple Concatenation:

Subject: Data Structure Print Date: [22/Apr/21], Page 4 of 8


 Example to create new tuple by concatenating Touples.

 Tuple Repetation
 Example to create Repetition of Touples.

 Membership Operation on Tuple,


o Membership operators are used to validate the membership of a value in a sequence such as
strings, lists or tuples.
o Python has two membership operators – “in” and “not in”.
o Membership Operator ( in ):
o The in operator is used to check if an element is present in a sequence or not. It returns True if
element is pesent, otherwise return False.
o Example to find 100 in Tuple1.

o Example to find 30 in Tuple1

Subject: Data Structure Print Date: [22/Apr/21], Page 5 of 8


o Membership Operator ( not in ):
o The not in operator is used to check if an element is present in a sequence or not. It returns True
if element is not pesent, otherwise return False.

 Tuple Iteration.
o We can iterate the tuple using for loop .

 Built in Tuples functions.


o len() function :
 This function returns the number of elements in a tuple.
 Syntax: len( tuple )
o max() function :
 This function returns the maximum number from tuple.
 Syntax: max( tuple )
o min() function :
 This function returns the minimum number from tuple.
 Syntax: min( tuple )
o sum() function :
 This function returns the sum of all elements of tuple.
 Syntax: sum( tuple )
Subject: Data Structure Print Date: [22/Apr/21], Page 6 of 8
Built In Tuple Methods…
 count() method :
 This method returns the number of times a specified value appears in the tuple.
 Syntax: tuple1.count( value )

 index() method :
 This method returns the first occurance of specified value. If value found, then it returns its index
position, and if not found, then it raised an exception.
 Syntax: tuple1.index( value )

Subject: Data Structure Print Date: [22/Apr/21], Page 7 of 8


 Exception details if item not found in tuple when we call index method.

Subject: Data Structure Print Date: [22/Apr/21], Page 8 of 8

You might also like