Ad Code

Responsive Advertisement

Tuples in Python Class 11 Notes | Tuple and Dictionaries

  

Tuple in Python

  • Tuple is built in sequence data type in python.
  • Stores multiple values
  • Tuple item can be of different data types
  • All the items are comma separated and enclosed in parenthesis ‘()’.
  • Individual item in a Tuple can be accessed using index which begins from 0.
  • In case of single item present in tuple, it should also be followed by a comma.
  • A sequence without parenthesis is treated as tuple by default.

Examples

Accessing elements in a Tuple

  • Individual item in a Tuple can be accessed using indexes.
  • Indexes are unique numbers assigned to each item in a Tuple to identify them
  • Index always begins from 0 and written in square brackets “[]”.

Example:

Tuple is Immutable

  • Yes Tuple is Immutable.
  • Content of the Tuple cannot be changed after it has been created.

Example:

Tuple operations

Tuple supports following operations:

  • Concatenation
  • Repetition
  • Membership
  • Slicing

Concatenation

  • Concatenation refers to joining two Tuple.
  • Plus (‘+’) is used as concatenation operator.
  • Concatenation operator can also be used for extending an existing tuple.

Example:

Repetition

  • Repetition as it name implies used to repeat elements of  a Tuple at specified no of times.
  • Asterisk (‘*’) is used as repetition operator.

Membership

  • Membership operation refers to checking an item exists in Tuple or not.
  • Python uses ‘in’ and ‘not in’ as membership operator.
  • ‘in’ returns true if the item specified present in the Tuple.
  • ‘not in’ returns true if the item specified present in the Tuple.

Example:

Slicing

  • Extracting a subset of items from given Tuple is called slicing
  • Subset occurred after slicing contains contiguous items.
  • Slicing is done using index range like Tuple[start_index : end_index : step_value]
  • End index is always excluded in resultant subset of Tuple.
  • Negative index can also be used for slicing.

Examples:

Traversing a Tuple

  • Traversing a Tuple refers to accessing each item a given Tuple sequentially.
  • for or while loop can be used for traversing a Tuple.

Examples:

Tuple Methods and Built in Functions


Nested Tuple

One Tuple appears as an element inside another Tuple.

Example:

Tuple Assignment

Tuple Assignment allows elements of tuple on the left side of assignment operator to be assigned respective values from a tuple on the right side.


Post a Comment

0 Comments