Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Python Tuples | Other Data Types in Python
Introduction to Python (dev copy)

bookPython Tuples

A tuple is another data type in Python that can hold multiple values or variables. These values are separated by commas and wrapped in parentheses. At first, it might seem like tuples are pretty similar to lists, but there are some key differences you should be aware of.

The main distinction lies in mutability: lists can be modified, but tuples cannot. You might recall altering a list using the .extend() method. This method isn't available for tuples, meaning once a tuple is set, you can't change its contents without creating a new tuple. Plus, tuples are more efficient in terms of speed and memory usage compared to lists. Given their immutability, tuples are often chosen when dealing with sensitive data.

Let's keep working with the example data we've been using:

123
# Create tuple US_Info = ("USA", 9629091, 331002651) print(type(US_Info))
copy

Note

You can also transform an iterable object into a tuple with the tuple() function.

123456
# Create a list list_variable = [1, 2, 3] # Convert it into a tuple tuple_variable = tuple(list_variable) # Print the type of the converted variable print(type(tuple_variable))
copy

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 6

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Awesome!

Completion rate improved to 1.64

bookPython Tuples

Stryg for at vise menuen

A tuple is another data type in Python that can hold multiple values or variables. These values are separated by commas and wrapped in parentheses. At first, it might seem like tuples are pretty similar to lists, but there are some key differences you should be aware of.

The main distinction lies in mutability: lists can be modified, but tuples cannot. You might recall altering a list using the .extend() method. This method isn't available for tuples, meaning once a tuple is set, you can't change its contents without creating a new tuple. Plus, tuples are more efficient in terms of speed and memory usage compared to lists. Given their immutability, tuples are often chosen when dealing with sensitive data.

Let's keep working with the example data we've been using:

123
# Create tuple US_Info = ("USA", 9629091, 331002651) print(type(US_Info))
copy

Note

You can also transform an iterable object into a tuple with the tuple() function.

123456
# Create a list list_variable = [1, 2, 3] # Convert it into a tuple tuple_variable = tuple(list_variable) # Print the type of the converted variable print(type(tuple_variable))
copy

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 6
some-alt