Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Python Tuples | Other Data Types in Python
Introduction to Python(ihor)
course content

Kursinnehåll

Introduction to Python(ihor)

Introduction to Python(ihor)

1. First Acquaintance with Python
2. Variables and Types in Python
3. Conditional Statements in Python
4. Other Data Types in Python
5. Loops in Python
6. Functions in Python

book
Python Tuples

The key difference is mutability: lists can be modified, while tuples cannot. The methods like .extend() and .append() works on lists but not on tuples, because once created, a tuple's contents cannot be changed without creating new one. Tuples are also faster and more memory-efficient, making them ideal for fixed or sensitive data.

123456
# Create a tuple with fixed data: coordinates of a location location = (37.7749, -122.4194) # Print the tuple type print("Location:", location) print("Type of location:", type(location))
copy

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

1234567
# Convert a list to a tuple list_location = [37.7749, -122.4194] tuple_location = tuple(list_location) # Print the converted tuple print("Location:", tuple_location) print("Converted type:", type(tuple_location))
copy
question mark

What is the primary difference between lists and tuples?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 4. Kapitel 6

Fråga AI

expand
ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

course content

Kursinnehåll

Introduction to Python(ihor)

Introduction to Python(ihor)

1. First Acquaintance with Python
2. Variables and Types in Python
3. Conditional Statements in Python
4. Other Data Types in Python
5. Loops in Python
6. Functions in Python

book
Python Tuples

The key difference is mutability: lists can be modified, while tuples cannot. The methods like .extend() and .append() works on lists but not on tuples, because once created, a tuple's contents cannot be changed without creating new one. Tuples are also faster and more memory-efficient, making them ideal for fixed or sensitive data.

123456
# Create a tuple with fixed data: coordinates of a location location = (37.7749, -122.4194) # Print the tuple type print("Location:", location) print("Type of location:", type(location))
copy

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

1234567
# Convert a list to a tuple list_location = [37.7749, -122.4194] tuple_location = tuple(list_location) # Print the converted tuple print("Location:", tuple_location) print("Converted type:", type(tuple_location))
copy
question mark

What is the primary difference between lists and tuples?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 4. Kapitel 6
Vi beklagar att något gick fel. Vad hände?
some-alt