Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What type of thing have I defined in list2?

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading