Kursinnhold
Learn Python from Scratch
Learn Python from Scratch
2. Arithmetic operations
3. Common data types
Tuples (3/3)
Like for lists, you can place tuples inside tuples! And then you can use double indexing to reach the element in tuple inside a tuple.
For example, for out five countries
Country | Area | Population |
---|---|---|
USA | 9629091 | 331002651 |
Canada | 9984670 | 37742154 |
Germany | 357114 | 83783942 |
Brazil | 8515767 | 212559417 |
India | 3166391 | 1380004385 |
# create two-dimensional tuple two_d_countries = (('USA', 9629091, 331002651), ('Canada', 9984670, 37742154), ('Germany', 357114, 83783942), ('Brazil', 8515767, 212559417), ('India', 3166391, 1380004385)) # information about India print(two_d_countries[4]) # area of Canada print(two_d_countries[1][1])
Oppgave
Swipe to start coding
Create two-dimensional tuple for people below in format (("name", age, height), ("name", age, height))
:
Name | Age | Height |
---|---|---|
Alex | 23 | 178 |
Noah | 34 | 189 |
Peter | 29 | 175 |
John | 41 | 185 |
Michelle | 35 | 165 |
- Print information for Peter.
- Print height of John.
- Print tuple converted into list (use
list
function)
Løsning
Alt var klart?
Takk for tilbakemeldingene dine!
Seksjon 5. Kapittel 6
Tuples (3/3)
Like for lists, you can place tuples inside tuples! And then you can use double indexing to reach the element in tuple inside a tuple.
For example, for out five countries
Country | Area | Population |
---|---|---|
USA | 9629091 | 331002651 |
Canada | 9984670 | 37742154 |
Germany | 357114 | 83783942 |
Brazil | 8515767 | 212559417 |
India | 3166391 | 1380004385 |
# create two-dimensional tuple two_d_countries = (('USA', 9629091, 331002651), ('Canada', 9984670, 37742154), ('Germany', 357114, 83783942), ('Brazil', 8515767, 212559417), ('India', 3166391, 1380004385)) # information about India print(two_d_countries[4]) # area of Canada print(two_d_countries[1][1])
Oppgave
Swipe to start coding
Create two-dimensional tuple for people below in format (("name", age, height), ("name", age, height))
:
Name | Age | Height |
---|---|---|
Alex | 23 | 178 |
Noah | 34 | 189 |
Peter | 29 | 175 |
John | 41 | 185 |
Michelle | 35 | 165 |
- Print information for Peter.
- Print height of John.
- Print tuple converted into list (use
list
function)
Løsning
Alt var klart?
Takk for tilbakemeldingene dine!
Seksjon 5. Kapittel 6