omics.co.in

Tuples

Tuples

Tuple is an ordered, immutable collection of elements. Tuples are useful for storing data that should not be modified after it has been created, such as DNA sequences or gene expression data.

 

Here are some basic operations that can be performed on tuples in Python:

 

Creating a tuple:

You can create a tuple by enclosing a list of elements in parentheses (), like this:

Pyton
				# Create a tuple using parentheses
my_tuple = (1, 2, 3)

			

Accessing elements of a tuple:

You can access individual elements of a tuple using indexing, like this:

Pyton
				# Access the first element of the tuple
first_element = my_tuple[0]

			

In this example, we access the first element of the tuple my_tuple using indexing and assign it to the variable first_element.

 

Slicing a tuple:

You can extract a subset of elements from a tuple using slicing, like this:

Pyton
				# Create a tuple
my_tuple = (1, 2, 3, 4, 5)

# Extract a slice of the tuple
slice_tuple = my_tuple[1:4]

			

In this example, we extract a slice of the tuple my_tuple that includes elements 2, 3, and 4 and assign it to the variable slice_tuple.

 

Combining tuples:

You can combine two or more tuples using the + operator, like this:

Pyton
				# Create two tuples
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)

# Combine the two tuples
combined_tuple = tuple1 + tuple2

			

In this example, we create two tuples tuple1 and tuple2 and then combine them using the + operator to create a new tuple combined_tuple that contains all the elements from both tuples.

 

Tuples are a useful tool for storing ordered, immutable collections of elements in Python. With tuples, you can create and access ordered collections of data, extract subsets of elements using slicing, and combine multiple tuples into a single collection.

← Return to Home Hub Scroll to Top ↑