I am a noob to Python.
I constantly find myself looking at a piece of code and trying to work out what is inside a data structure such as for example a dictionary. In fact the first thing I am trying to work out is “what sort of data structure is this?” and THEN I try to work out how to see what is inside it. I look at a variable and say “is this a dict, or a list, or a multidict or something else I’m not yet familiar with?”. Then, “What’s inside it?”. It’s consuming vast amounts of time and I just don’t know if I’m taking the right approach.
So, the question is, “How do the Python masters find out what sort of data structure something is, and what techniques do they use to see what is inside those data structures?”
I hope the question is not too general but I’m spending ridiculous amounts of time just trying to fix issues with recognizing data structures and viewing their contents, let alone getting useful code written.
thanks heaps.
Using type() function for the variable will tell you the data type. For example:
will print
<class 'dict'>which means the variable inventory is a dictionary.
Other possible data types are ‘str’ for string, ‘int’ for integer, ‘float’ for float,’tuple’ for tuple, and ‘bool’ for boolean values.
To see what’s inside a collection, you can simply use print() function.
will output