Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 135 - 2615_KEC
P. 135

    Program 1: A program to demonstrate the creation of a list.

                         Program1.py

                      File  Edit  Format   Run    Options   Window    Help

                      # Tuple of strings: Days of the week
                      week_days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
                      print("Week Days:", week_days)

                      # Tuple of integers: Roll numbers
                      roll_numbers = (101, 102, 103, 104, 105)
                      print("Roll Numbers:", roll_numbers)

                      # Tuple of characters: Grade symbols
                      grades = ('A', 'B', 'C', 'D')
                      print("Grades:", grades)

                      # Tuple of floats: Temperature readings
                      temperatures = (36.5, 37.0, 38.2, 36.8)
                      print("Temperatures:", temperatures)

                         Output

                      Week Days: ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday')
                      Roll Numbers: (101, 102, 103, 104, 105)
                      Grades: ('A', 'B', 'C', 'D')
                      Temperatures: (36.5, 37.0, 38.2, 36.8)

                     EMPTY TUPLE

                     In Python, an empty tuple is a tuple that does not contain any elements. You can create it by using
                     round brackets ( ) with nothing inside. It’s very simple!

                     The syntax for creating a tuple is as follows:

                     Program 2: A program to create an empty tuple.

                         Program2.py
                      File  Edit  Format   Run   Options   Window    Help

                      empty_tuple = ()                                            Output
                      print(empty_tuple)
                                                                               ()

                     ACCESSING A TUPLE

                     Accessing a tuple means retrieving its elements or values. You can access individual elements in
                     a tuple by using indexing.

                                                                                                  Tuples and Dictionary  133

| 130 131 132 133 134 135 136 137 138 139 140 |