Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Creating a Tuple | Tuple
Python Data Structures
course content

Course Content

Python Data Structures

Python Data Structures

1. List
2. Dictionary
3. Tuple
4. Set
5. For deleting

bookCreating a Tuple

In Python, a tuple is a data structure that consists of a sequence of values (elements) enclosed in parentheses, with elements separated by commas. Tuples are similar to lists, but the key distinction is that tuples are immutable data structures.

Immutable data structures can't be modified after they're created. Tuples, just like lists, can hold any number of elements, and the data type of each element can vary. It's crucial to note that a tuple with only one element is defined with a comma following the element, as shown:

12
tuple_1 = (5,) print(tuple_1)
copy

Note

As demonstrated in the example, you need to add a comma after the single element to create a valid one-element tuple.

Let's explore how to create tuples.

Creating an empty tuple:

12
tuple_1 = () print(tuple_1)
copy

Creating a tuple with strings:

12
tuple_1 = ('one', 'two', 'three', 'four', 'five') print(tuple_1)
copy

Creating a tuple using the tuple() function:

12
tuple_1 = tuple('codefinity') print(tuple_1)
copy

Task

You need to create a tuple containing strings, such as:

'apple', 'apricot', 'banana', 'grape', 'mango', 'peach', 'pineapple'.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 1
toggle bottom row

bookCreating a Tuple

In Python, a tuple is a data structure that consists of a sequence of values (elements) enclosed in parentheses, with elements separated by commas. Tuples are similar to lists, but the key distinction is that tuples are immutable data structures.

Immutable data structures can't be modified after they're created. Tuples, just like lists, can hold any number of elements, and the data type of each element can vary. It's crucial to note that a tuple with only one element is defined with a comma following the element, as shown:

12
tuple_1 = (5,) print(tuple_1)
copy

Note

As demonstrated in the example, you need to add a comma after the single element to create a valid one-element tuple.

Let's explore how to create tuples.

Creating an empty tuple:

12
tuple_1 = () print(tuple_1)
copy

Creating a tuple with strings:

12
tuple_1 = ('one', 'two', 'three', 'four', 'five') print(tuple_1)
copy

Creating a tuple using the tuple() function:

12
tuple_1 = tuple('codefinity') print(tuple_1)
copy

Task

You need to create a tuple containing strings, such as:

'apple', 'apricot', 'banana', 'grape', 'mango', 'peach', 'pineapple'.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 1
toggle bottom row

bookCreating a Tuple

In Python, a tuple is a data structure that consists of a sequence of values (elements) enclosed in parentheses, with elements separated by commas. Tuples are similar to lists, but the key distinction is that tuples are immutable data structures.

Immutable data structures can't be modified after they're created. Tuples, just like lists, can hold any number of elements, and the data type of each element can vary. It's crucial to note that a tuple with only one element is defined with a comma following the element, as shown:

12
tuple_1 = (5,) print(tuple_1)
copy

Note

As demonstrated in the example, you need to add a comma after the single element to create a valid one-element tuple.

Let's explore how to create tuples.

Creating an empty tuple:

12
tuple_1 = () print(tuple_1)
copy

Creating a tuple with strings:

12
tuple_1 = ('one', 'two', 'three', 'four', 'five') print(tuple_1)
copy

Creating a tuple using the tuple() function:

12
tuple_1 = tuple('codefinity') print(tuple_1)
copy

Task

You need to create a tuple containing strings, such as:

'apple', 'apricot', 'banana', 'grape', 'mango', 'peach', 'pineapple'.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

In Python, a tuple is a data structure that consists of a sequence of values (elements) enclosed in parentheses, with elements separated by commas. Tuples are similar to lists, but the key distinction is that tuples are immutable data structures.

Immutable data structures can't be modified after they're created. Tuples, just like lists, can hold any number of elements, and the data type of each element can vary. It's crucial to note that a tuple with only one element is defined with a comma following the element, as shown:

12
tuple_1 = (5,) print(tuple_1)
copy

Note

As demonstrated in the example, you need to add a comma after the single element to create a valid one-element tuple.

Let's explore how to create tuples.

Creating an empty tuple:

12
tuple_1 = () print(tuple_1)
copy

Creating a tuple with strings:

12
tuple_1 = ('one', 'two', 'three', 'four', 'five') print(tuple_1)
copy

Creating a tuple using the tuple() function:

12
tuple_1 = tuple('codefinity') print(tuple_1)
copy

Task

You need to create a tuple containing strings, such as:

'apple', 'apricot', 'banana', 'grape', 'mango', 'peach', 'pineapple'.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 3. Chapter 1
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt