Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Tuples (2/3) | Other data types
Learn Python from Scratch
course content

Зміст курсу

Learn Python from Scratch

Learn Python from Scratch

1. The basics
2. Arithmetic operations
3. Common data types
4. Conditional statements
5. Other data types
6. Loops
7. Functions

Tuples (2/3)

But you might be wondered - why do we there use tuples instead of lists? Yes, now it makes no difference, but remember, that tuple is immutable, so it is better in terms of memory. But as for now, we shouldn't pay attention to this, as we work with small data.

You can refer to elements inside the tuple the same way as for lists. But at the same time, you can't simply append an element to your tuple, as you need to "rewrite" it.

  • len() - length of tuple (number of elements);
  • tuple1 + tuple2 - concatenation (both must be tuples);
  • tuple1 * n - n copies of tuple1;
  • tuple1.count(x) - counts number of x in tuple1.

For example, let's modify our tuple with new information:

CountryAreaPopulation
Brazil8515767212559417
India31663911380004385

Pay attention, that for concatenation both must be tuples!

1234567
# old countries tuple countries_tuple = ("USA", 9629091, 331002651, "Canada", 9984670, 37742154, "Germany", 357114, 83783942) # new information new_data = ("Brazil", 8515767, 212559417, "India", 3166391, 1380004385) # create new updated tuple and print it upd_countries = countries_tuple + new_data print(upd_countries)
copy

Завдання

Update your tuple from the previous exercise with new information below. Then print updated tuple.

NameAgeHeight
John41185
Michelle35165

Завдання

Update your tuple from the previous exercise with new information below. Then print updated tuple.

NameAgeHeight
John41185
Michelle35165

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Секція 5. Розділ 5
toggle bottom row

Tuples (2/3)

But you might be wondered - why do we there use tuples instead of lists? Yes, now it makes no difference, but remember, that tuple is immutable, so it is better in terms of memory. But as for now, we shouldn't pay attention to this, as we work with small data.

You can refer to elements inside the tuple the same way as for lists. But at the same time, you can't simply append an element to your tuple, as you need to "rewrite" it.

  • len() - length of tuple (number of elements);
  • tuple1 + tuple2 - concatenation (both must be tuples);
  • tuple1 * n - n copies of tuple1;
  • tuple1.count(x) - counts number of x in tuple1.

For example, let's modify our tuple with new information:

CountryAreaPopulation
Brazil8515767212559417
India31663911380004385

Pay attention, that for concatenation both must be tuples!

1234567
# old countries tuple countries_tuple = ("USA", 9629091, 331002651, "Canada", 9984670, 37742154, "Germany", 357114, 83783942) # new information new_data = ("Brazil", 8515767, 212559417, "India", 3166391, 1380004385) # create new updated tuple and print it upd_countries = countries_tuple + new_data print(upd_countries)
copy

Завдання

Update your tuple from the previous exercise with new information below. Then print updated tuple.

NameAgeHeight
John41185
Michelle35165

Завдання

Update your tuple from the previous exercise with new information below. Then print updated tuple.

NameAgeHeight
John41185
Michelle35165

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Секція 5. Розділ 5
toggle bottom row

Tuples (2/3)

But you might be wondered - why do we there use tuples instead of lists? Yes, now it makes no difference, but remember, that tuple is immutable, so it is better in terms of memory. But as for now, we shouldn't pay attention to this, as we work with small data.

You can refer to elements inside the tuple the same way as for lists. But at the same time, you can't simply append an element to your tuple, as you need to "rewrite" it.

  • len() - length of tuple (number of elements);
  • tuple1 + tuple2 - concatenation (both must be tuples);
  • tuple1 * n - n copies of tuple1;
  • tuple1.count(x) - counts number of x in tuple1.

For example, let's modify our tuple with new information:

CountryAreaPopulation
Brazil8515767212559417
India31663911380004385

Pay attention, that for concatenation both must be tuples!

1234567
# old countries tuple countries_tuple = ("USA", 9629091, 331002651, "Canada", 9984670, 37742154, "Germany", 357114, 83783942) # new information new_data = ("Brazil", 8515767, 212559417, "India", 3166391, 1380004385) # create new updated tuple and print it upd_countries = countries_tuple + new_data print(upd_countries)
copy

Завдання

Update your tuple from the previous exercise with new information below. Then print updated tuple.

NameAgeHeight
John41185
Michelle35165

Завдання

Update your tuple from the previous exercise with new information below. Then print updated tuple.

NameAgeHeight
John41185
Michelle35165

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

But you might be wondered - why do we there use tuples instead of lists? Yes, now it makes no difference, but remember, that tuple is immutable, so it is better in terms of memory. But as for now, we shouldn't pay attention to this, as we work with small data.

You can refer to elements inside the tuple the same way as for lists. But at the same time, you can't simply append an element to your tuple, as you need to "rewrite" it.

  • len() - length of tuple (number of elements);
  • tuple1 + tuple2 - concatenation (both must be tuples);
  • tuple1 * n - n copies of tuple1;
  • tuple1.count(x) - counts number of x in tuple1.

For example, let's modify our tuple with new information:

CountryAreaPopulation
Brazil8515767212559417
India31663911380004385

Pay attention, that for concatenation both must be tuples!

1234567
# old countries tuple countries_tuple = ("USA", 9629091, 331002651, "Canada", 9984670, 37742154, "Germany", 357114, 83783942) # new information new_data = ("Brazil", 8515767, 212559417, "India", 3166391, 1380004385) # create new updated tuple and print it upd_countries = countries_tuple + new_data print(upd_countries)
copy

Завдання

Update your tuple from the previous exercise with new information below. Then print updated tuple.

NameAgeHeight
John41185
Michelle35165

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 5. Розділ 5
Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt