Home > front end >  In python what type of thing is a list without square brackets?
In python what type of thing is a list without square brackets?

Time:08-18

I'm completely new to programming. I am using vscode and have python 3.10.5,I am following no starch press crash course in python (2nd edition) and in the section on lists it tells me that a list in python is written between square brackets, separated by commas, like the following:

list1 = ['a', 'b', 'c']

When I print this I get back the square brackets and the stuff inside, which is all fine. I was playing around and wrote the following (expecting an error)

list2 = 'a', 'b', 'c'

But when I print this, instead of an error I get back the right hand side but inside of round brackets.

What type of thing have I defined in list2?

CodePudding user response:

You can always run type(variable) to check the variable type. In this case, a "list" without brackets is not a list, it's a tuple.

From the docs:

A tuple consists of a number of values separated by commas.

Parentheses are not required, though you will have to use them when building complex data structures, with nested tuples for example.

Reference: https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences

  • Related