Skip to content

Informatics Practices · Ch 3 — Brief Overview of Python

Data Types

3.5

Data Types

Every value that appears in a Python program — a number, a name, a price, a list of cities — belongs to a specific data type. A data type does two jobs at once: it identifies the kind of data a variable can hold, and it decides which operations can be performed on that data. This second half is the part beginners often miss. The type is not just a label; it is a contract about behaviour. Numbers can be added and multiplied; a piece of text cannot be, even if it looks numeric. Knowing a value's type tells you, in advance, what you are allowed to do with it.

Figure 3.6 lays out the data types available in Python as one organised family. At the top level, Python's values fall into a few broad groups — Numbers, Sequences, Sets, None and Mappings. Numbers subdivide into integers, floating point numbers and complex numbers (with Boolean values attached to the integer family); Sequences cover strings, lists and tuples; Mappings contain the dictionary type. The sections that follow in this chapter take up these groups one by one — numbers first, then sequences, then mappings — so this figure is best read as a map of where the chapter is heading.

Alongside the idea of data types, the book introduces a small but essential programming habit: comments.

  • A comment adds a remark or a note in the source code. It is written for humans, not for the machine — the interpreter does not execute comments at all.
  • The purpose of a comment is to make source code easier for people to understand. Comments primarily document the meaning and purpose of the code: why a variable exists, what a calculation is for, what a program as a whole does.
  • In Python, a single line comment starts with the hash sign, #. Everything following the # up to the end of that line is treated as a comment, and the interpreter simply ignores it while executing the statement.

You have already seen comments at work in the earlier programs of this chapter:

#To find the sum of two given numbers
num1 = 10
num2 = 20
result = num1 + num2
print(result)     #print function in python displays the output

The first line is a full-line comment describing the program's purpose. The last line shows the second common style — a comment placed after a statement on the same line, explaining that one statement. In both cases the interpreter runs the code exactly as if the # portions were not there.

Tip

Comment the why, not the obvious what. A note like #To find the area of a rectangle at the top of a program tells a reader the intent instantly. Writing #assign 10 to length above length = 10 adds nothing the code does not already say. …

Figure 3.6Different Data Types in Python

Figure 3.6 is a box hierarchy diagram that organises all of Python's data types into a single family tree. At its root sits one box labelled "Data Types in Python", and from it lines run downward to five boxes, one for each broad category of value the language recognises: Numbers, Sequences, Sets, None and Mappings. Reading just this top row already answers the question "what kinds of data can a Python variable hold?" — every value in a program belongs to one of these five branches.

Three of the branches subdivide further. The Numbers box splits into three children — Integer, Floating Point and Complex — covering whole numbers, numbers with a decimal part, and complex numbers respectively. Attached below the Integer box is a Boolean box, a placement that carries meaning: the Boolean type, with its two values True and False, is drawn as belonging under integers rather than as an independent top-level category. The Sequences box branches into three stacked children — Strings, Lists and Tuples — the three ordered collection types the chapter goes on to describe. The Mappings box has a single child, Dictionaries, since the dictionary is Python's one standard mapping type.

The remaining two top-level boxes, Sets and None, stand alone without children in the diagram — each is a category in its own right. …