How to use Python tuple value

Creating and accessing tuples.

The following code uses tuple to create a time structure. The tuple has three integer values which represent hour, minute, and second.


hour = 1# from w  w  w.j  av  a2 s .c  om
minute = 2
second = 3

currentTime = hour, minute, second   # create tuple

print "The value of currentTime is:", currentTime

print "The number of seconds since midnight is", \
   ( currentTime[ 0 ] * 3600 + currentTime[ 1 ] * 60 + currentTime[ 2 ] )

The code above generates the following result.

Loop through tuple

The following code makes tables using lists of lists and tuples of tuples.


table2 = ( ( 1, 2 ), ( 3, ), ( 4, 5, 6 ) )
for row in table2:
   for item in row:
      print item,
   print# from ww w.  j a v a  2 s.  c  o m

The code above generates the following result.





















Home »
  Python »
    Data Types »




Data Types
String
String Format
Tuple
List
Set
Dictionary