munotes®

Data and Its Types: What a Type Actually Settles

Get access to whole semester resourcesSemester Pass

Chapter Three

Syllabus topic Module 1, "Abstract Data Type: Different Data Types"

Pages 8 to 10 of 411

In one line

A type is three promises at once: which values are allowed, which operations are allowed, and how the value is stored.

Data, before types

Memory is bytes. A byte is eight bits and holds a number from 0 to 255, and that is all a machine has. The same eight bits 01000001 are the number 65, the letter A, or part of something larger. Nothing in the bits says which.

A type is the answer to that question. It is the label that tells the machine, and the reader, how to understand the bits.

The three promises

1. The set of values. An integer type allows whole numbers; a boolean allows exactly two things. Asking for a value outside the set is an error the type can catch.

2. The operations. You may add two integers. You may add two strings in Python, and it means joining them. You may not subtract one string from another, and the type is what makes that a refusal rather than nonsense.

3. The representation. How many bytes, in what arrangement. This is the promise students forget, and it is the one that decides what things cost.

Here are all three, observed rather than asserted:

x = 65
c = "A"
b = True

print("value      :", x, "| type:", type(x).__name__)
print("value      :", c, "| type:", type(c).__name__)
print("value      :", b, "| type:", type(b).__name__)
print()
print("65 + 1     :", x + 1)
print("'A' + '1'  :", c + "1")
print("True + True:", b + b, "  (a bool is a kind of int in Python)")
print()
try:
    print(c - c)
except TypeError as e:
    print("'A' - 'A'  : refused, and the type is why:", e)
value      : 65 | type: int
value      : A | type: str
value      : True | type: bool

65 + 1     : 66
'A' + '1'  : A1
True + True: 2   (a bool is a kind of int in Python)

'A' - 'A'  : refused, and the type is why: unsupported operand type(s) for -: 'str' and 'str'

Two things in that run are worth stopping on.

'A' + '1' gives A1, not 66. The plus sign means different operations on different types, and the type is what selects which. This is why "what does + do" has no answer without a type.

True + True gives 2. In Python a boolean really is a kind of integer, which is convenient and is also the sort of detail that makes a wrong answer in an examination if you assume every language does it.

The representation, which is the part that costs

Ask Python how big a value actually is:

import sys

for value, name in [(0, "int 0"), (255, "int 255"), (10**30, "int 10^30"),
                    (3.5, "float"), ("", "empty str"), ("A", "str 'A'"),
                    (True, "bool"), ([], "empty list"), ([1, 2, 3], "list of 3")]:
    print("%-12s %4d bytes" % (name, sys.getsizeof(value)))
munotes.in8

Data and Its Types: What a Type Actually Settles

int 0          28 bytes
int 255        28 bytes
int 10^30      40 bytes
float          24 bytes
empty str      41 bytes
str 'A'        42 bytes
bool           28 bytes
empty list     56 bytes
list of 3      88 bytes

Those numbers are this Python's, on this machine. Another build gives different ones and the book is not asking you to memorise them. What they show is the point: a value has a size, the size depends on the type, and an empty list is not free.

Notice that int 0 and int 255 are the same size, but 10**30 is bigger. Python's integers grow to fit the number, which is unusual and generous. In C or Java an int is a fixed number of bytes and a number too big for it wraps around silently. That difference is exactly why the next chapter exists.

Static and dynamic typing, in one paragraph

In C and Java you declare the type and it is fixed: int n; and n holds integers for ever. That is static typing, checked before the program runs. In Python the value carries the type and a name can point at anything, which is dynamic typing, checked as it runs.

This paper's structures are built in Python, so the types are dynamic. But every structure here is a structure in any language, and where a language's typing changes the design, this book says so.

Why a type is not a data structure

A type says what one value is. A data structure says how many values are arranged.

Put the other way: int is a type. "Ten thousand integers, in a row, in order" is a data structure. The paper is about the second, and it starts with the first because a structure is made of typed values and you cannot reason about the cost of the arrangement without knowing the cost of one item.

Quick revision

  • Memory is bytes; the bits do not say what they mean. A type is what says.
  • A type settles three things: the allowed values, the allowed operations, and the representation.
  • The same operator means different things on different types: + adds integers and joins strings.
  • Every value has a size, the size depends on the type, and an empty container is not free.
  • Python integers grow to fit; C and Java integers are fixed width and can overflow.
  • Static typing fixes the type of a name before the run (C, Java); dynamic typing carries the type on

the value (Python).

  • A type describes one value; a data structure describes an arrangement of many.
munotes.in9

Data and Its Types: What a Type Actually Settles

Test yourself

1. What three things does a type settle? The set of allowed values, the operations allowed on them, and how the value is represented in memory.

2. Why does 'A' + '1' not give 66? Because + is selected by the type. On strings it joins; on integers it adds. The characters are not being read as their numeric codes.

3. Which of the three promises decides what a structure costs? The representation. The set of values and the operations decide what is legal; the representation decides what is cheap.

4. Give one difference between a Python integer and a C integer that matters. A Python integer grows to fit any whole number; a C int is fixed width and overflows silently when the value is too large.

5. State the difference between static and dynamic typing. Static: the type belongs to the name and is fixed and checked before the run. Dynamic: the type belongs to the value and is checked while the program runs.

6. Is int a data structure? Explain. No. It is a type, describing one value. A data structure describes how many values are arranged, such as an array of integers.

munotes.in10

The rest of this subject

These notes are cut from the University's printed syllabus. Open the syllabus itself, or the past papers, for the same subject.

Issue
Done!