Skip to main content

Python 3 Part 4 - Understanding tuple data structure

 Understanding Tuple data structure

Table of content

  1. Tuple
  2. How to define tuple
  3. Access tuple elements
  4. Tuple containing mutation
  5. Tuple methods

Tuple

Tuple is an immutable inbuilt data structure, once created can't be modified. We can define tuple using () round Bracktes in Python.

How to define tuple?

Access tuple elements

We index the tuple to access element

Tuple cannot be modified.

Tuple containing mutation

Tuple are immutable but can contain mutable data elements like list. We can loop on tuple using for loop.

Methods

1) tuple.count(x) : returns count of occurrence of element. The number of times the element is present in the tuple.

2) tuple.index(x) : returns index of value. (first occurrence from left)


Thank you folks. If you like this Python 3 post, please do checkout my other post on  Django with Python 3 series

Most viewed

Ruby on Rails Part 4 - Exception Handling

  Ruby on Rails Part 4 - Exception Handling  Table of content Exception Handling retry raise ensure else  catch and throw Exception classes Exception Handling Enclose the code that could raise an exception with a begin/end block and use rescue clauses to tell Ruby the types of exceptions that you want to handle. The syntax for exception handling : 
 begin  
      #- statements
 rescue OneTypeOfException       #-
 handle the exception rescue AnotherTypeOfException       #- 
handle the exception else       
# Other exceptions
 ensure 
      # ensure block is always executed
 end 
 Everything from begin to rescue is protected 

in the block. 
If an exception occurs during the execution of this block of code, control is passed to the 

block between rescue and end.
 
For each rescue clause in the begin block, Ruby compares the raised Exception against each 

of the parameters of the...