How to Define Python Tuples
Originally published: 27/11/2018 12:48
Publication number: ELQ-69013-1
View all versions & Certificate

How to Define Python Tuples

Python tuples are another type of data sequences, but differently to lists, they are immutable...

  • Step n°1 |

    Python tuples

    Python tuples are another type of data sequences, but differently to lists, they are immutable. Tuples cannot be changed or modified; you cannot append or delete elements. The syntax that indicates you are having a tuple and not a list is that the tuple’s elements are placed within parentheses and not brackets.

    By the way, the tuple is the default sequence type in Python, so if I enlist three values here, the computer will perceive the new variable as a tuple. We could also say the three values will be packed into a tuple.

    For the same reason, we can assign a number of values to the same number of variables. Do you remember we went through that a few lectures ago? On the left side of the equality sign, we just added a tuple of variables, and on the right, a tuple of values. That’s why the relevant technical term for this activity is tuple assignment.

    In the same way we can do for lists, we can index values by indicating their position in brackets.

    In addition, we can also place Python tuples within lists. And then, each tuple becomes a separate element within the list.

    How to Define Python Tuples image
add_shopping_cartContinue reading for free (70% left)

0.0 / 5 (0 votes)

please wait...