Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lists (2/3) | 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

bookLists (2/3)

There are several operations available for manipulating lists, same as for string, such as:

  • len() - length of list (i.e. number of elements);
  • list1 + list2 - concatenation (both must be lists);
  • list1 * n - n copies of list1;
  • list.append(x) - add x (one element!) to the end of list (x can not be list) - this one rewrites your list;
  • list.extend((x, y, ...)) - add x,y, ... to the end of list - also rewrites your list;
  • list.copy() - creates a copy of your list;
  • list.count(x) - counts number of x in list.

For example, we can modify a bit our list with several more countries.

12345678910
countries = ["USA", 9629091, "Canada", 9984670, "Germany", 357114] # list with new countries countries_new = ["Brazil", 8515767, "India", 3166391] # add new data (list) to our list using concatenation print(countries + countries_new) # using list method countries.extend(("Brazil", 8515767, "India", 3166391)) print(countries)
copy

Tarefa

Modify your last list with new data. Experiment with both methods and print the result.

NameAge
John41
Michelle35

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

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

bookLists (2/3)

There are several operations available for manipulating lists, same as for string, such as:

  • len() - length of list (i.e. number of elements);
  • list1 + list2 - concatenation (both must be lists);
  • list1 * n - n copies of list1;
  • list.append(x) - add x (one element!) to the end of list (x can not be list) - this one rewrites your list;
  • list.extend((x, y, ...)) - add x,y, ... to the end of list - also rewrites your list;
  • list.copy() - creates a copy of your list;
  • list.count(x) - counts number of x in list.

For example, we can modify a bit our list with several more countries.

12345678910
countries = ["USA", 9629091, "Canada", 9984670, "Germany", 357114] # list with new countries countries_new = ["Brazil", 8515767, "India", 3166391] # add new data (list) to our list using concatenation print(countries + countries_new) # using list method countries.extend(("Brazil", 8515767, "India", 3166391)) print(countries)
copy

Tarefa

Modify your last list with new data. Experiment with both methods and print the result.

NameAge
John41
Michelle35

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

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

bookLists (2/3)

There are several operations available for manipulating lists, same as for string, such as:

  • len() - length of list (i.e. number of elements);
  • list1 + list2 - concatenation (both must be lists);
  • list1 * n - n copies of list1;
  • list.append(x) - add x (one element!) to the end of list (x can not be list) - this one rewrites your list;
  • list.extend((x, y, ...)) - add x,y, ... to the end of list - also rewrites your list;
  • list.copy() - creates a copy of your list;
  • list.count(x) - counts number of x in list.

For example, we can modify a bit our list with several more countries.

12345678910
countries = ["USA", 9629091, "Canada", 9984670, "Germany", 357114] # list with new countries countries_new = ["Brazil", 8515767, "India", 3166391] # add new data (list) to our list using concatenation print(countries + countries_new) # using list method countries.extend(("Brazil", 8515767, "India", 3166391)) print(countries)
copy

Tarefa

Modify your last list with new data. Experiment with both methods and print the result.

NameAge
John41
Michelle35

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

There are several operations available for manipulating lists, same as for string, such as:

  • len() - length of list (i.e. number of elements);
  • list1 + list2 - concatenation (both must be lists);
  • list1 * n - n copies of list1;
  • list.append(x) - add x (one element!) to the end of list (x can not be list) - this one rewrites your list;
  • list.extend((x, y, ...)) - add x,y, ... to the end of list - also rewrites your list;
  • list.copy() - creates a copy of your list;
  • list.count(x) - counts number of x in list.

For example, we can modify a bit our list with several more countries.

12345678910
countries = ["USA", 9629091, "Canada", 9984670, "Germany", 357114] # list with new countries countries_new = ["Brazil", 8515767, "India", 3166391] # add new data (list) to our list using concatenation print(countries + countries_new) # using list method countries.extend(("Brazil", 8515767, "India", 3166391)) print(countries)
copy

Tarefa

Modify your last list with new data. Experiment with both methods and print the result.

NameAge
John41
Michelle35

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 5. Capítulo 2
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
some-alt