Page 82 - Modular v1.1 Pyhton
P. 82

08                        TUPLE IN PYTHON




















                          Your Aim

                          to learn about:
                                   Creating a Tuple                        Accessing a Value in a Tuple
                                     Unpacking Tuple                         Traversing Elements in a Tuple
                                     Python Tuple Methods                      Python Functions
                                       Operations on a Tuple                    Changing a Tuple
                                       Deleting a Tuple                            Difference between List and Tuple
                                        Some More Programs


                  In the previous chapter, you have learned about list. Similar to list, a tuple is a sequence of
                  values enclosed in parentheses and its indices start with 0. Unlike list, tuple cannot be changed

                  which means it is immutable.  In this chapter, we will discuss how to create, access and perform
                  operations on tuples.

                      CREATING A TUPLE

                  To create  a tuple, write different values and separate  them with comma. You can put the
                  comma-separated values between the parentheses ‘( )’. Parentheses (round bracket) indicate the

                  start and end of the tuple.
                  Tuple contains any number of elements of heterogeneous type (integer, float, string, list, etc).

                  Example of tuple:

                  tup1=('sst', 'chemistry', 2010, 2022); #tuple with dissimilar items
                  tup2=(1, 2, 3, 4, 5 );                               #tuple with similar items
                  tup3=(1, 2.5, 3.6, 4, 5 );                             #t 50, tup1 is treated to be of
                                                                       type int.

                  Program 1: To create a tuple.












                   80     Touchpad MODULAR (Version 1.0)
   77   78   79   80   81   82   83   84   85   86   87