⊗pyPmTpOET 64 of 208 menu

Tuple of one element in Python

A tuple can consist of only one element.

However, when working with such tuples, there is a nuance: after the element, you must put a hanging comma:

tpl = ('a',)

If you leave the element without a comma, the brackets will denote mathematical grouping brackets:

test1 = ('a',) # tuple test2 = ('a') # string test3 = (1) # number

If you create a tuple of several elements, then you don’t need to put a comma after the last one (but you can):

tpl = ('a', 'b', 'c')

Given a variable:

tst = ('1')

Tell me what type of data it is.

Given a variable:

tst = (1,)

Tell me what type of data it is.

Given a variable:

tst = (True)

Tell me what type of data it is.

byenru