Tuples
Tuples
• Tuples are works exactly like lists.
• Tuple is similar to lists since the value of the items
stored in the list can be changed whereas the tuple is
immutable and the value of the items stored in the tuple
can not be changed.
• There fore in Python “Tuple” is used to store the
sequence of immutable python objects.
Creating tuple
• A tuple can be written as the collection of comma-
separated values enclosed with the small brackets.
Accessing tuple elements
tuple() function
• An alternative way of creating a tuple is by using
tuple() function.
• The tuple() function is a built-in function in
Python that can be used to create a tuple.
• Syntax :
tuple(iterable)
This function accepts a single parameter iterable
(optional). It is an iterable(list, range etc..) or an
iterator object.
Inbuilt functions for tuple
• len(tuple):It calculates the length of the tuple.
Inbuilt functions for tuple
• max(tuple) :It returns the maximum element
of the tuple.
Inbuilt functions for tuple
• min(tuple) :It returns the minimum element of
the tuple.
Inbuilt functions for tuple
• Sum() : Returns the sum of all the elements in
the tuple
Inbuilt functions for tuple
• index(x): Returns the index of the element x
Inbuilt functions for tuple
• count(x) : Returns the number of occurrences
of element x
Indexing and slicing
• Tuples are like lists , so indexing and slicing of
tuple is similar to that of lists.
• The indexing operator “[ ]” is uses to access
elements of a tuple.
• The index must be an integer; so we cannot use
float or other types. This will result in “type error”.
• Python allows negative indexing for its sequences.
• The index of -1 refers to the last item, -2 to the
second last item and so on.
Example
Operations on tuples
• Operators can be used to concatenate or
multiply tuples.
• Concatenation is done with the ”+” operator
• Multiplication is done with the ”*” operator.
Concatenation(using “+” operator)
Repetition operator
• “*” : The multiplication operator is used to
replicate the elements of the tuple.
Passing variable length arguments to tuples
• The special syntax ”*args” in function definitions in
python is used to pass a variable number of arguments
to a function
• syntax to create a function that can take variable length
arguments.
def function_name(*args) :
#
# body of the function
#
• *args: holds variable length arguments.
Finding the sum of the given tuple using *
args
List to tuples
• A tuple can also be created from a list.
Soring tuples
• A tuple does not contains any method named
sort.
• In order to sort a tuple, first convert a tuple in
to a list, after conversion use sort() method for
lists and again convert it into a tuple.
Soring tuples
Traverse tuples from a list
• A tuple assignment can be used in the for loop
to traverse a list of tuple.
zip() function in python
• The zip() is an inbuilt function in python.
• The zip() function takes items in a sequence
from a number of collections(list, tuple, set ,
etc …) and aggregates them in a tuple, and
return it.
Zip() example
Zip() example
• If the sequences are not of the same length
then the result of the zip() function has the
length of the shorter sequence.
Inverse zip(*) function
• The * operator is used with in the zip()
function.
• The * operator unpacks a sequence into
positional arguments.

Tuples in pyhton programming using simple codes.pptx

  • 1.
  • 2.
    Tuples • Tuples areworks exactly like lists. • Tuple is similar to lists since the value of the items stored in the list can be changed whereas the tuple is immutable and the value of the items stored in the tuple can not be changed. • There fore in Python “Tuple” is used to store the sequence of immutable python objects.
  • 3.
    Creating tuple • Atuple can be written as the collection of comma- separated values enclosed with the small brackets.
  • 4.
  • 5.
    tuple() function • Analternative way of creating a tuple is by using tuple() function. • The tuple() function is a built-in function in Python that can be used to create a tuple. • Syntax : tuple(iterable) This function accepts a single parameter iterable (optional). It is an iterable(list, range etc..) or an iterator object.
  • 8.
    Inbuilt functions fortuple • len(tuple):It calculates the length of the tuple.
  • 9.
    Inbuilt functions fortuple • max(tuple) :It returns the maximum element of the tuple.
  • 10.
    Inbuilt functions fortuple • min(tuple) :It returns the minimum element of the tuple.
  • 11.
    Inbuilt functions fortuple • Sum() : Returns the sum of all the elements in the tuple
  • 12.
    Inbuilt functions fortuple • index(x): Returns the index of the element x
  • 13.
    Inbuilt functions fortuple • count(x) : Returns the number of occurrences of element x
  • 14.
    Indexing and slicing •Tuples are like lists , so indexing and slicing of tuple is similar to that of lists. • The indexing operator “[ ]” is uses to access elements of a tuple. • The index must be an integer; so we cannot use float or other types. This will result in “type error”. • Python allows negative indexing for its sequences. • The index of -1 refers to the last item, -2 to the second last item and so on.
  • 15.
  • 16.
    Operations on tuples •Operators can be used to concatenate or multiply tuples. • Concatenation is done with the ”+” operator • Multiplication is done with the ”*” operator.
  • 17.
  • 18.
    Repetition operator • “*”: The multiplication operator is used to replicate the elements of the tuple.
  • 19.
    Passing variable lengtharguments to tuples • The special syntax ”*args” in function definitions in python is used to pass a variable number of arguments to a function • syntax to create a function that can take variable length arguments. def function_name(*args) : # # body of the function # • *args: holds variable length arguments.
  • 21.
    Finding the sumof the given tuple using * args
  • 22.
    List to tuples •A tuple can also be created from a list.
  • 23.
    Soring tuples • Atuple does not contains any method named sort. • In order to sort a tuple, first convert a tuple in to a list, after conversion use sort() method for lists and again convert it into a tuple.
  • 25.
  • 26.
    Traverse tuples froma list • A tuple assignment can be used in the for loop to traverse a list of tuple.
  • 27.
    zip() function inpython • The zip() is an inbuilt function in python. • The zip() function takes items in a sequence from a number of collections(list, tuple, set , etc …) and aggregates them in a tuple, and return it.
  • 28.
  • 29.
    Zip() example • Ifthe sequences are not of the same length then the result of the zip() function has the length of the shorter sequence.
  • 30.
    Inverse zip(*) function •The * operator is used with in the zip() function. • The * operator unpacks a sequence into positional arguments.