Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Python

Idiom #249 Declare and assign multiple variables

Define variables a, b and c in a concise way.
Explain if they need to have the same type.

a, b, c = 42, 'hello', 5.0

a, b and c may have different types.
from decimal import Decimal
a, b, c = Decimal(1.23).as_tuple()
a, b, *c = '110000'

This syntax is very useful in parsing "block code" data.
a, b, c = 'xyz'
*a, b, c = '000011'

'... If the target list contains one target prefixed with an asterisk, called a “starred” target: The object must be an iterable with at least as many items as there are targets in the target list, minus one.'
var (a, b, c) = (42, "hello", 5.0);

a, b, and c may have different types.

New implementation...
programming-idioms.org