Skip to main content

Mastering the Art of Tuples in Python

Tuple

Like an array, Tuple is a collection of data (of different data types). Tuples are specified using round brackets. 

Unlike Lists, Data in the tuple cannot be altered once a tuple is created. Below are some of the features of Tuples.  
  • Tuples can hold data of different data types. 
  • Tuples can not be amended.
  • Data in the tuple can be accessed by using index. Index of an element will remain same once defined.
  • Tuples can hold duplicate data. 

In this post, we will see how to access the data in Tuples and couple of tuple methods.

Creating a Tuple in Python

Like any other data type in Python, Tuple can be created by defining the data inside round brackets. 

Creating tuple in Python

Tuple can be created with data of different data types (or same type). 

In the above example,
  • 'a_tuple' is created with numeric data.
  • 'b_tuple' is created with character data.
  • 'c_tuple' is created with boolean data. 
  • 'd_tuple' is created with data of different data types.
All the tuples defined above has more than one element. Let's see how to define a tuple with just one element.  

Should it just be mentioning one element within round brackets? No, Just mentioning one element with in round brackets would consider the variable as the type of the data mentioned within round brackets. 

This can be easily understood with below example. 

Create tuple with just one element

In the above example, 
  • 'a_tuple' is defined just by mentioning a string with in round brackets. This wouldn't be considered as a tuple instead variable 'a_tuple' would be defined as a string.
  • 'b_tuple' is defined by mentioning comma (',') after the first element. This would define the variable as a tuple. 
print type of tuple

Accessing the data in a Tuple

Elements in a tuple can be accessed using index. Index starts with '0' and incremented by '1'.

Index for a tuple

Elements of a Tuple can be accessed from the last element with the negative index value. As we can see above, Index of last element would be '-1'. 

If we need to retrieve the second element ("TWO"), index '1' or '-4' needs to be mentioned within square brackets. 

E.g.: 

print(a_tuple[1]) #prints the second element of a list

print(a_tuple[-4]) #prints the fourth element from end of a list

In the above example, index is mentioned with in square brackets. Yes, Index needs to be mentioned with in square brackets for a tuple (not in round brackets like it is defined). 

Retrieve Index of an element

We have seen how to access the elements using an index, If we know the element and need to know the index, Method 'index()' would be helpful. One thing to note is, it always returns the first index if the element is present multiple times in the list. 

Retrieve index of an element in a tuple

In the above example, '2' is present twice in the list. But, only one index (first occurrence '1') will be returned.

Print list in Python

By using the index() method we would know the first occurrence of an element. Let's say if there are duplicate entries like in above example, we would need to pass the starting index along with the value. 

Retrieve index of an element in Tuple

In the above example (Line - 4), 'index(2, 2)' indicates to check for the value '2' (first argument) from the index '2' (second argument) and returns the index.


Count the number of occurrences

We have seen how to retrieve the index of an element. Let's say we are only interested in knowing how many times a value is present in the list (and not interested in the index), method 'count()' is helpful. 

Method 'count()' accepts the value to be checked as an arguments and returns the number of occurrences the data is present in a tuple. 

Count the number of occurrences in a Tuple

In the above example, '2' is present '3' times. So, count() should return '3'. 

print tuple

Hope the above details were a bit of help to you in understanding more about Tuples in Python.


If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form.

Comments

Popular posts from this blog

All about READ in RPGLE & Why we use it with SETLL/SETGT?

READ READ is one of the most used Opcodes in RPGLE. As the name suggests main purpose of this Opcode is to read a record from Database file. What are the different READ Opcodes? To list, Below are the five Opcodes.  READ - Read a Record READC - Read Next Changed Record READE - Read Equal Key Record READP - Read Prior Record READPE - Read Prior Equal Record We will see more about each of these later in this article. Before that, We will see a bit about SETLL/SETGT .  SETLL (Set Lower Limit) SETLL accepts Key Fields or Relative Record Number (RRN) as Search Arguments and positions the file at the Corresponding Record (or Next Record if exact match isn't found).  SETGT (Set Greater Than) SETGT accepts Key Fields or Relative Record Number (RRN) as Search Arguments and positions the file at the Next Record (Greater Than the Key value). Syntax: SETLL SEARCH-ARGUMENTS/KEYFIELDS FILENAME SETGT  SEARCH-ARGUMENTS/KEYFIELDS FILENAME One of the below can be passed as Search Arguments. Key Fiel

What we need to know about CHAIN (RPGLE) & How is it different from READ?

CHAIN READ & CHAIN, These are one of the most used (& useful) Opcodes by any RPG developer. These Opcodes are used to read a record from file. So, What's the difference between CHAIN & READ?   CHAIN operation retrieves a record based on the Key specified. It's more like Retrieving Random record from a Database file based on the Key fields.  READ operation reads the record currently pointed to from a Database file. There are multiple Opcodes that start with READ and all are used to read a record but with slight difference. We will see more about different Opcodes and How they are different from each other (and CHAIN) in another article. Few differences to note.  CHAIN requires Key fields to read a record where as READ would read the record currently pointed to (SETLL or SETGT are used to point a Record).  If there are multiple records with the same Key data, CHAIN would return the same record every time. READE can be used to read all the records with the specified Ke

Extract a portion of a Date/Time/Timestamp in RPGLE - IBM i

%SUBDT Extracting Year, Month, Day, Hour, Minutes, Seconds or Milli seconds of a given Date/Time/Timestamp is required most of the times.  This can be extracted easily by using %SUBDT. BIF name looks more similar to %SUBST which is used to extract a portion of string by passing from and two positions of the original string. Instead, We would need to pass a value (i.e., Date, Time or Timestamp ) and Unit (i.e., *YEARS, *MONTHS, *DAYS, *HOURS, *MINUTES, *SECONDS or *MSECONDS) to %SUBDT.  Valid unit should be passed for the type of the value passed. Below are the valid values for each type. Date - *DAYS, *MONTHS, *YEARS Time - *HOURS, *MINUTES, *SECONDS Timestamp - *DAYS, *MONTHS, *YEARS, *HOURS, *MINUTES, *SECONDS, *MSECONDS Syntax: %SUBDT(value : unit { : digits { : decpos} }) Value and Unit are the mandatory arguments.  Digits and Decimal positions are optional and can only be used with *SECONDS for Timestamp. We can either pass the full form for the unit or use the short form. Below i