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 |
9
1
2
3
4
5
6
7
# 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])
1234567# 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])
Tâche
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)
Solution
9
1
2
3
4
5
6
7
8
# create two-dimensional tuple
two_d_tuple = ('Alex', (23, 178), 'Noah', (34, 189), 'Peter', (29, 175), 'John', (41, 185), 'Michelle', (35, 165))
# information about Peter
print(two_d_tuple[5])
# height of John
print(two_d_tuple[7][1])
# print converted into list tuple
print(list(two_d_tuple))
Tout était clair ?
Merci pour vos commentaires !
Section 5. Chapitre 6
single
9
1
2
3
4
5
6
7
8
# create two-dimensional tuple
two_d_tuple = (('Alex', 23, 178), _ _ _
# information about Peter
print(_ _ _)
# height of John
print(_ _ _)
# print converted into list tuple
print(_ _ _)
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion