Tuples (1/3)
Tuples - is one more type of data available in Python. Tuples are usually used when we have a description of some object (such as point coordinates, characteristics of a car, etc...). Tuples may contain data of different types inside, but unlike lists - they are immutable. If you can modify the list by using, for example, list.append()
or list.extend()
, if you want to modify a tuple, you have to reassign it to a new variable (or the same).
To create tuple use (elem1, elem2, ...)
notation or function tuple(x)
, where x
is iterable.
For example, let's create tuple with countries areas and populations.
Country | Area | Population |
---|---|---|
USA | 9629091 | 331002651 |
Canada | 9984670 | 37742154 |
Germany | 357114 | 83783942 |
123# create tuple countries_tuple = ("USA", 9629091, 331002651, "Canada", 9984670, 37742154, "Germany", 357114, 83783942) print(countries_tuple)
Swipe to start coding
Create the tuple (people_tuple) for the people below and print it:
Name | Age | Height |
---|---|---|
Alex | 23 | 178 |
Noah | 34 | 189 |
Peter | 29 | 175 |
Lösning
Tack för dina kommentarer!
single
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Awesome!
Completion rate improved to 2.33Awesome!
Completion rate improved to 2.33
Tuples (1/3)
Tuples - is one more type of data available in Python. Tuples are usually used when we have a description of some object (such as point coordinates, characteristics of a car, etc...). Tuples may contain data of different types inside, but unlike lists - they are immutable. If you can modify the list by using, for example, list.append()
or list.extend()
, if you want to modify a tuple, you have to reassign it to a new variable (or the same).
To create tuple use (elem1, elem2, ...)
notation or function tuple(x)
, where x
is iterable.
For example, let's create tuple with countries areas and populations.
Country | Area | Population |
---|---|---|
USA | 9629091 | 331002651 |
Canada | 9984670 | 37742154 |
Germany | 357114 | 83783942 |
123# create tuple countries_tuple = ("USA", 9629091, 331002651, "Canada", 9984670, 37742154, "Germany", 357114, 83783942) print(countries_tuple)
Swipe to start coding
Create the tuple (people_tuple) for the people below and print it:
Name | Age | Height |
---|---|---|
Alex | 23 | 178 |
Noah | 34 | 189 |
Peter | 29 | 175 |
Lösning
Tack för dina kommentarer!
single
Awesome!
Completion rate improved to 2.33
Tuples (1/3)
Svep för att visa menyn
Tuples - is one more type of data available in Python. Tuples are usually used when we have a description of some object (such as point coordinates, characteristics of a car, etc...). Tuples may contain data of different types inside, but unlike lists - they are immutable. If you can modify the list by using, for example, list.append()
or list.extend()
, if you want to modify a tuple, you have to reassign it to a new variable (or the same).
To create tuple use (elem1, elem2, ...)
notation or function tuple(x)
, where x
is iterable.
For example, let's create tuple with countries areas and populations.
Country | Area | Population |
---|---|---|
USA | 9629091 | 331002651 |
Canada | 9984670 | 37742154 |
Germany | 357114 | 83783942 |
123# create tuple countries_tuple = ("USA", 9629091, 331002651, "Canada", 9984670, 37742154, "Germany", 357114, 83783942) print(countries_tuple)
Swipe to start coding
Create the tuple (people_tuple) for the people below and print it:
Name | Age | Height |
---|---|---|
Alex | 23 | 178 |
Noah | 34 | 189 |
Peter | 29 | 175 |
Lösning
Tack för dina kommentarer!