Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Unpacking | Packing and Unpacking
Intermediate Python: Arguments, Scopes and Decorators
course content

Conteúdo do Curso

Intermediate Python: Arguments, Scopes and Decorators

Intermediate Python: Arguments, Scopes and Decorators

1. Packing and Unpacking
2. Arguments in Function
3. Function as an Argument
4. Variable Scope
5. Decorators

bookUnpacking

Unpacking is a powerful and versatile concept. This feature allows you to extract values from various iterable data structures, such as tuples, lists, dictionaries, ranges, and even strings. The opposite action is packing. Often, these two concepts are used together simultaneously. Let's break down the topic step by step.

First of all, let's compare unpacking and packing.

Python can both pack and unpack values from iterables in a single assignment. To perform packing, use the * iterable unpacking operator before the variable name.

1234567
# unpacking a, b, c = (1, 2, 3) # a = 1, b = 2, c = 3 print(f'a = {a}, b = {b}, c = {c}') # packing a, b, *c = 1, 2, 3, 4, 5 # a = 1, b = 2, c = [3, 4, 5] print(f'a = {a}, b = {b}, c = {c}')
copy

You will encounter errors if you attempt to unpack a different no of values than variables.

12
a, b = 1, 2, 3 # ValueError: too many values to unpack a, b, c, d = 1, 2, 3 # ValueError: not enough values to unpack
copy

The value on the right side can be a tuple, list, generator, range, dictionary, set, or string. Let's explore each of these with examples.

Note

All values that are written in one line and separated by commas but without parentheses are one tuple. 1, 2, 3 same as (1, 2, 3)

The set is an unordered collection of data.

Tarefa

Let's practice a little bit.

  • There is a tuple and you need to unpack it as in the example above.
  • Use the name, age, career variables.
  • Print it out to see the result.

Click the button below the code to check solution.

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 1. Capítulo 1
toggle bottom row

bookUnpacking

Unpacking is a powerful and versatile concept. This feature allows you to extract values from various iterable data structures, such as tuples, lists, dictionaries, ranges, and even strings. The opposite action is packing. Often, these two concepts are used together simultaneously. Let's break down the topic step by step.

First of all, let's compare unpacking and packing.

Python can both pack and unpack values from iterables in a single assignment. To perform packing, use the * iterable unpacking operator before the variable name.

1234567
# unpacking a, b, c = (1, 2, 3) # a = 1, b = 2, c = 3 print(f'a = {a}, b = {b}, c = {c}') # packing a, b, *c = 1, 2, 3, 4, 5 # a = 1, b = 2, c = [3, 4, 5] print(f'a = {a}, b = {b}, c = {c}')
copy

You will encounter errors if you attempt to unpack a different no of values than variables.

12
a, b = 1, 2, 3 # ValueError: too many values to unpack a, b, c, d = 1, 2, 3 # ValueError: not enough values to unpack
copy

The value on the right side can be a tuple, list, generator, range, dictionary, set, or string. Let's explore each of these with examples.

Note

All values that are written in one line and separated by commas but without parentheses are one tuple. 1, 2, 3 same as (1, 2, 3)

The set is an unordered collection of data.

Tarefa

Let's practice a little bit.

  • There is a tuple and you need to unpack it as in the example above.
  • Use the name, age, career variables.
  • Print it out to see the result.

Click the button below the code to check solution.

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 1. Capítulo 1
toggle bottom row

bookUnpacking

Unpacking is a powerful and versatile concept. This feature allows you to extract values from various iterable data structures, such as tuples, lists, dictionaries, ranges, and even strings. The opposite action is packing. Often, these two concepts are used together simultaneously. Let's break down the topic step by step.

First of all, let's compare unpacking and packing.

Python can both pack and unpack values from iterables in a single assignment. To perform packing, use the * iterable unpacking operator before the variable name.

1234567
# unpacking a, b, c = (1, 2, 3) # a = 1, b = 2, c = 3 print(f'a = {a}, b = {b}, c = {c}') # packing a, b, *c = 1, 2, 3, 4, 5 # a = 1, b = 2, c = [3, 4, 5] print(f'a = {a}, b = {b}, c = {c}')
copy

You will encounter errors if you attempt to unpack a different no of values than variables.

12
a, b = 1, 2, 3 # ValueError: too many values to unpack a, b, c, d = 1, 2, 3 # ValueError: not enough values to unpack
copy

The value on the right side can be a tuple, list, generator, range, dictionary, set, or string. Let's explore each of these with examples.

Note

All values that are written in one line and separated by commas but without parentheses are one tuple. 1, 2, 3 same as (1, 2, 3)

The set is an unordered collection of data.

Tarefa

Let's practice a little bit.

  • There is a tuple and you need to unpack it as in the example above.
  • Use the name, age, career variables.
  • Print it out to see the result.

Click the button below the code to check solution.

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!

Unpacking is a powerful and versatile concept. This feature allows you to extract values from various iterable data structures, such as tuples, lists, dictionaries, ranges, and even strings. The opposite action is packing. Often, these two concepts are used together simultaneously. Let's break down the topic step by step.

First of all, let's compare unpacking and packing.

Python can both pack and unpack values from iterables in a single assignment. To perform packing, use the * iterable unpacking operator before the variable name.

1234567
# unpacking a, b, c = (1, 2, 3) # a = 1, b = 2, c = 3 print(f'a = {a}, b = {b}, c = {c}') # packing a, b, *c = 1, 2, 3, 4, 5 # a = 1, b = 2, c = [3, 4, 5] print(f'a = {a}, b = {b}, c = {c}')
copy

You will encounter errors if you attempt to unpack a different no of values than variables.

12
a, b = 1, 2, 3 # ValueError: too many values to unpack a, b, c, d = 1, 2, 3 # ValueError: not enough values to unpack
copy

The value on the right side can be a tuple, list, generator, range, dictionary, set, or string. Let's explore each of these with examples.

Note

All values that are written in one line and separated by commas but without parentheses are one tuple. 1, 2, 3 same as (1, 2, 3)

The set is an unordered collection of data.

Tarefa

Let's practice a little bit.

  • There is a tuple and you need to unpack it as in the example above.
  • Use the name, age, career variables.
  • Print it out to see the result.

Click the button below the code to check solution.

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