0% found this document useful (0 votes)
4 views

tuple & sets (1)

The document explains tuples in Python, highlighting their characteristics as ordered and immutable collections of items, created using parentheses. It covers tuple operations, indexing, slicing, and comparisons with lists and dictionaries, emphasizing the differences in mutability and functionality. Additionally, it discusses built-in functions for tuples and the zip() method for merging iterables.

Uploaded by

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

tuple & sets (1)

The document explains tuples in Python, highlighting their characteristics as ordered and immutable collections of items, created using parentheses. It covers tuple operations, indexing, slicing, and comparisons with lists and dictionaries, emphasizing the differences in mutability and functionality. Additionally, it discusses built-in functions for tuples and the zip() method for merging iterables.

Uploaded by

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

> What is tuple : tuple are used to store multiple items in single variable.

tuple is one of 4 built in


data types in python is used to store collection of data the other 3 are list,set and dictionary all
with different qualities and usage.a tuple is a collection which is ordered and
unchangeable.tuple are withen with round brackets.tuple can be created by placing the element
in parenthesis ().each value inside a tuple is called an element or item.the element in the tuple
are separated by the comma(,)
The syntax tuple_name = ()

> tuples is immutable


1.​ Maintaining order : tuples are mainly defined in python as a way to show order for
example when you retrieve data from a database in form of a list of tuple all tuple are in
the order of the yield you fetched
2.​ Copy efficiency : rather than copying an immutable object you can alias it
3.​ Comparing efficiency : you can compare two variable by comparing position rather than
content when using copy by reference.
4.​ Constant correctness : some value should nt be allow to change.

>indexing (access tuple element by index number) : tuples are ordered collection of element
which means each element has a specific position or index. You can access the element of
tuple using their index number with the index starting 0 for the first element.

# Defining a tuple
my_tuple = (10, 20, 30, 40, 50)

# Accessing elements using index numbers


first_element = my_tuple[0] # Accessing the first element (index 0)
second_element = my_tuple[1] # Accessing the second element (index 1)
last_element = my_tuple[-1] # Accessing the last element using negative index

print(first_element) # Output: 10
print(second_element) # Output: 20
print(last_element) # Output: 50

< negative indexing : the negative index start from the last element and denoted by -1.negative
indices are used when we want to access the element of the tuple right to left starting from right
hand side. The first element has the index -1 and the last element has the index n where n is
the length of tuple.
Ex
thistuple = ("apple", "banana", "cherry")
print(thistuple[-1])
>Slicing: the concept of slicing is similar to string and list
slicing tuple slice means a part of tuple to access some part of
tuple we use a method called slicing the slicing operator
[start:end] is used for slicing.this can be done by specifying an
index range.

< negative slicing : the negative indexing in tuple start from the
rightmost or last element present in the tuple in negative slicing
we have to write negative indexes of tuple inside the slice
operator and between coln operator to print the part of tuple.

<specifying steps in slice operator : a third arguments called step


which is an optional can be specified along with the start and end
index number. This step refers to the number of element in the
tuple tuple that can be skipped after the start indexing element in
the tuple the default value of step is one.

>basic tuple operation : same as rest of other.

> nested tuple : tuple written inside the another tuple is known as
a nested tuple.

Ex : tuple = (10,20,30,40,(23,43,45))
The last element consisting of 3 element written within perenthese
is called a nested tuple as it inside another tuple.

> built in function tuple


1.len() : this method is used to calculate the total length of
tuple.
Syntax: len(tuple_name)

2.sum() : this method is used to calculate the sum of all the


element in the tuple.
Syntax: sum(tuple_name)

3.max() : it is used to return the maximum element in the tuple


Syntax: max(tuple_name)

4.min() : it is used to return the minimum element in the tuple


Syntax: min(tuple_name)
5.any() : the any() function returns true if any of the boolean
value in the tuple is true.
Syntax : any(tuple_name)

6.all() : the all() function return true if all the boolean value
in the tuple are true else false.

Syntax: all(tuple_name)

7.sorted(): the sorted function return sorted list of the specified


iterable object.we can specify ascending and descending order.

>tuple method
1.count(): the count() method return the number of times the
specified element appear in the tuple.the count() method takes a
single arguments
.the element to be counted
Syntax : tuple_name.count(element)

2.index(): the index() method searches for the given element form
the start of the list and return its index.if the value appear
more than once it will return the index of first one.if the item is
not present in the list it will give value error.
The list index() method takes a maximum three element
.the element to be searched.
.start searching from this index.
.search the element till end-1 index

syntax
list.index(element,start,end)

> relation between tuple and list : list and tuple are data
structure in python. Both lists and tuple are used for storing
object in python. Let us understand the relationship between tuple
and list with some examples.tuples are immutable and usually,
contain a heterogeneous sequence of element which is accessed via
unpacking or indexing. List are mutable and their items are
accessed via indexing item cannot be added,removed or replaces in
tuple.
1.converting tuple to list and list to tuple : we may need to
modify or change the content of tuple and in such situation the
list can be helpful.

,the tuple can be converted to list using list() function by


passing tuple as an arguments.
,once it is converted to the list the content in the list can be
modified or altered

2.a list as an element in tuple : we know that tuple can hold


object of any type.it means that we can have list,string,dictionary
as an item inside a tuple.if an item within a tuple is mutable then
we can change it. Consider the presence of a list as an item in a
tuple then any changes to the list get reflected on the overall
item in the tuple.

> difference between list and tuple

List tuple

List is a group of Tuple is group of comma-seprated


comma-seprated value within value within the parenthesis()
square brackets[] and square and parentheses is optional.
brackets is mandatory.

List is mutable it can be Tuple is immutable it cannot be


altered or changed after its altered or changed after its
creation creation

List have serval built in Tuple does not have many built
function in function because of immutable

List are better for insertion Tuple are appropriate for


and deletion operator accessing the element.

A unexpected change or error is In a tuple changes and error


more likely to occur in a list dont usually occur because of
immutablity.

List can be copied Tuple cannot copied


> Relational between tuple and dictionary
1.tuple can be used as key:value pair to build dictionaries : the
tuple can be converted to dictionary by passing the tuple name to
the dict() function this is achieved by nesting tuples within tuple
wherein each nested tuple item should have two item the first item
become key and second item as its value when the tuple gets
converted to a dictionary.

2.the method item() in a dictionary return a list of dictionary :


the method items() in a dictionary return a list of tuple where
each tuple corresponds to a key value pair of the dictionary.

>difference between tuple and dictionary

Tuple Dictionary

Tuple is a group of comma Dictionary is collection of data


separated value within that store key value pair
parentheses()and parenthese is
optional

Tuple is an ordered collection Dictionary is ordered from


of element python 3.7 and above

Tuple is enclosed within the Dictionary is enclosed within


parentheses the curly {} brackets in form of
key value pair

Tuple allow duplicate value Dictionary keys do not allow


duplicate value the value can
have duplicate value

Tuple can be created using Dictionary can be created using


tuple() function dict() fucntion

Tuple is immutable it cannot be Dictionary is mutable it can be


altered,change or deletion after altered,change or deletion after
its creation its creation.

Negative slicing and indexing is No concept of negative slicing


possible and indexing in dictionary

Tuple is immutable so data Keys are sorted in dictionary by


cannot be sorted in a tuple using sorted() fucntion
>sets : a set is unordered collection of unique element.a set is
iterable,mutable and does not allow any type of duplicate as all
the element in set is unique.sets are used to store multiple
element in single element.the curly{} brackets are used to
represent set.the value in set are called item or element. Set is
mutable data type thus adding,removing,or searching for item in the
set is possible.

>list vs dictionary vs tuple vs set

Feature List Tuple Set dictionary

Define A A A A
collection collection collection collection
of ordered of ordered unordered Of key
and mutable and and unique value pair
element immutable element unordered
with no mutable
duplicate

Mutablity Mutable Immutable Mutable Mutable

order Ordered Ordered Unordered ordered

Indexing Support Support Does not Support


indexing indexing suppor indexing
indexing

Add / Support Does not Support Support


remove adding/remo support adding/remo adding/remo
ving adding/remo ving ving
element ving element element
element

Duplicate Allowed Allowed Not allowed Does not


support
duplicate
for keys
but value
can be
duplicate

syntax Square Parenthesis Curly Curly


brakets brackets brackets {}
with a coln
separating
key and
value.

>the zip() method : the zip()method in python takes one or more


iterable(list,string etc) as arguments and merge each of them
element wise to create a single iterable. The return value is a zip
object which is also an iterable and consist of tuple,which result
form the merger between the element of the input iterable.this
means that the first element of all the input iterable are zipped
together to create a tuple which become the first element of the
output zip object.
Syntax zip(*iterable)

You might also like