robotsheepboy
robotsheepboy

Reputation: 3

In python what type of thing is a list without square brackets?

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?

Upvotes: 0

Views: 1052

Answers (2)

Jalaj Bankar
Jalaj Bankar

Reputation: 1

It's because the list1 you created is recognized by the compiler as list (because you used square brackets which is mandatory) but list2 is recognized as a tuple because in python the parentheses are optional while creating a tuple, however, it is a good practice to use them.

Upvotes: 0

captainslocum
captainslocum

Reputation: 46

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

Upvotes: 1

Related Questions