Tuples

A tuple is a collection that cannot be modified.

Variables can be of the datatype tuple. A tuple is defined using parenthesis.
If you want to change the data during program execution, use a list instead of a tuple.

Related Courses

Example

A tuple with one item ( a comma is needed in the end ):

A tuple with multiple items:

Note: Even with one element, the comma is needed.

 

Accessing tuples

To access individual elements, we use square brackets. To print the first element (Python starts counting from zero):

To print the second element:

To print the last element, you can count from the back using the minus sign.

 

Previous Post
Next Post

2 Replies to “Tuples”

Leave a Reply

Your email address will not be published. Required fields are marked *