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

Conteúdo do Curso

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

Dictionaries (2/2)

Let's consider some useful methods for dictionaries:

  • len(d) - returns length of dictionary d (number of pairs key:value);
  • min(d), max(d) - minimum or maximum of keys of dictionary d;
  • d.copy() - returns copy of dictionary d;
  • d.items() - returns all pairs (key, value) in dictionary d;
  • d.keys() - returns all keys of dictionary d;
  • d.values() - returns all values of dictionary d;
  • d[k] = e - set value e to key k.

For example, traditionally, let's update our dictionary so it will contain two more countries:

CountryAreaPopulation
Brazil8515767212559417
India31663911380004385
123456
# old dictionary countries_dict = {'USA': (9629091, 331002651), 'Canada': (9984670, 37742154), 'Germany': (357114, 83783942)} # update dictionary with countries countries_dict["Brazil"] = (8515767, 212559417) countries_dict["India"] = (3166391, 1380004385) print(countries_dict)
copy

Tarefa

Update your dictionary with people so it will contain information for the next two people and then print the whole dictionary:

NameAgeHeight
John41185
Michelle35165

Tarefa

Update your dictionary with people so it will contain information for the next two people and then print the whole dictionary:

NameAgeHeight
John41185
Michelle35165

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 5. Capítulo 8
toggle bottom row

Dictionaries (2/2)

Let's consider some useful methods for dictionaries:

  • len(d) - returns length of dictionary d (number of pairs key:value);
  • min(d), max(d) - minimum or maximum of keys of dictionary d;
  • d.copy() - returns copy of dictionary d;
  • d.items() - returns all pairs (key, value) in dictionary d;
  • d.keys() - returns all keys of dictionary d;
  • d.values() - returns all values of dictionary d;
  • d[k] = e - set value e to key k.

For example, traditionally, let's update our dictionary so it will contain two more countries:

CountryAreaPopulation
Brazil8515767212559417
India31663911380004385
123456
# old dictionary countries_dict = {'USA': (9629091, 331002651), 'Canada': (9984670, 37742154), 'Germany': (357114, 83783942)} # update dictionary with countries countries_dict["Brazil"] = (8515767, 212559417) countries_dict["India"] = (3166391, 1380004385) print(countries_dict)
copy

Tarefa

Update your dictionary with people so it will contain information for the next two people and then print the whole dictionary:

NameAgeHeight
John41185
Michelle35165

Tarefa

Update your dictionary with people so it will contain information for the next two people and then print the whole dictionary:

NameAgeHeight
John41185
Michelle35165

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 5. Capítulo 8
toggle bottom row

Dictionaries (2/2)

Let's consider some useful methods for dictionaries:

  • len(d) - returns length of dictionary d (number of pairs key:value);
  • min(d), max(d) - minimum or maximum of keys of dictionary d;
  • d.copy() - returns copy of dictionary d;
  • d.items() - returns all pairs (key, value) in dictionary d;
  • d.keys() - returns all keys of dictionary d;
  • d.values() - returns all values of dictionary d;
  • d[k] = e - set value e to key k.

For example, traditionally, let's update our dictionary so it will contain two more countries:

CountryAreaPopulation
Brazil8515767212559417
India31663911380004385
123456
# old dictionary countries_dict = {'USA': (9629091, 331002651), 'Canada': (9984670, 37742154), 'Germany': (357114, 83783942)} # update dictionary with countries countries_dict["Brazil"] = (8515767, 212559417) countries_dict["India"] = (3166391, 1380004385) print(countries_dict)
copy

Tarefa

Update your dictionary with people so it will contain information for the next two people and then print the whole dictionary:

NameAgeHeight
John41185
Michelle35165

Tarefa

Update your dictionary with people so it will contain information for the next two people and then print the whole dictionary:

NameAgeHeight
John41185
Michelle35165

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Let's consider some useful methods for dictionaries:

  • len(d) - returns length of dictionary d (number of pairs key:value);
  • min(d), max(d) - minimum or maximum of keys of dictionary d;
  • d.copy() - returns copy of dictionary d;
  • d.items() - returns all pairs (key, value) in dictionary d;
  • d.keys() - returns all keys of dictionary d;
  • d.values() - returns all values of dictionary d;
  • d[k] = e - set value e to key k.

For example, traditionally, let's update our dictionary so it will contain two more countries:

CountryAreaPopulation
Brazil8515767212559417
India31663911380004385
123456
# old dictionary countries_dict = {'USA': (9629091, 331002651), 'Canada': (9984670, 37742154), 'Germany': (357114, 83783942)} # update dictionary with countries countries_dict["Brazil"] = (8515767, 212559417) countries_dict["India"] = (3166391, 1380004385) print(countries_dict)
copy

Tarefa

Update your dictionary with people so it will contain information for the next two people and then print the whole dictionary:

NameAgeHeight
John41185
Michelle35165

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 5. Capítulo 8
Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt