Conteúdo do Curso
Learn Python from Scratch
Learn Python from Scratch
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])
Tarefa
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)
Obrigado pelo seu feedback!
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])
Tarefa
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)
Obrigado pelo seu feedback!
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])
Tarefa
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)
Obrigado pelo seu feedback!
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])
Tarefa
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)