I am new to Python. I referred to different tutorials on it and found some contradictions.
Some authors say that in Python, numbers (int, float, complex), lists, sets, tuples, dictionaries, strings are data types.
Some authors call them data structures, few authors say classes. I am confused about which one is correct.
I'm doing an essay on Python, and wondering if anyone could clarify and justify the answer.
A "data type" is a description of a kind of data: what kinds of values can be an instance of that kind, and what kind of operations can be done on them.
A "class" is one way of representing a data type (although not the only way), treating the operations on the type as "methods" on the instances of the type (called "objects"). This is a general term across all class-based languages. But Python also has a specific meanings for "class": Something defined by the class statement, or something defined in built-in/extension code that meets certain requirements, is a class.
So, arbitrary-sized integers and mapping dictionaries are data types. in Python, they're represented by the built-in classes int and dict.
A "data structure" is a way of organizing data for efficient or easy access. This isn't directly relevant to data types. In many languages (like C++ or Java), defining a new class requires you to tell the compiler how an instance's members are laid out in memory, and things like that, but in Python you just construct objects and add members to them and the interpreter figures out how to organize them. (There are exceptions that come up when you're building extension modules or using ctypes to build wrapper classes, but don't worry about that.)
Things get blurry when you get to higher-level abstract data structures (like pointer-based nodes) and lower-level abstract data types (like order-preserving collection of elements that can do constant-time insertion and deletion at the head). Is a linked list a data type that inherently requires a certain data structure, or a data structure that defines an obvious data type, or what? Well, unless you major in computer science in college, the answer to that isn't really going to make much difference, as long as you understand the question.
So, mapping dictionaries are data types, but they're also abstract data structures—and, under the covers, Python's dict objects are built from a specific concrete data structure (open-chained hash table with quadratic probing) which is still partly abstract (each bucket contains a duck-typed value).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With