tuple & sets (1)
tuple & sets (1)
>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)
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.
> 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.
6.all() : the all() function return true if all the boolean value
in the tuple are true else false.
Syntax: all(tuple_name)
>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.
List tuple
List have serval built in Tuple does not have many built
function in function because of immutable
Tuple 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