2.2. Values and Data Types¶
A value is one of the fundamental things — like a word or a number — that a program manipulates.
Some values are
5 (the result when we add
2 + 3), and
"Hello, World!". These objects are classified into
different classes, or data types: 5 is an integer, and “Hello, World!” is a string, so-called because it contains a
string or sequence of letters. You (and the Python interpreter) can identify strings because they are enclosed in quotation
We can specify values directly in the programs we write. For example we can specify a number as a literal just by (literally) typing it directly into the program (e.g.,
4.32). In a program, we specify a word, or more generally a string of characters, by enclosing the characters inside quotation marks (e.g.,
During execution of a program, the Python interpreter creates an internal representation of any literals that have been specified in that program. It can then manipulate them, for example by multiplying two numbers. We call the internal representations objects or just values.
You can’t directly see the internal representations of values. You can, however, use the
The printed representation of a number uses the familiar decimal representation (reading Roman Numerals is a fun challenge in museums, but thank goodness the Python interpreter doesn’t present the number 2014 as MMXIV in the output window). Thus, the printed representation of a number shown in the console is the same as the literal that you specify in a program.
The printed representation of a character string, however, is not exactly the same as the literal used to specify the string in a program. For the literal in a program, you enclose the string in quotation marks. The printed representation, in the console, omits the quotation marks.
Numbers with a decimal point belong to a class called float, because these numbers are represented in a format called floating-point. At this stage, you can treat the words class and type interchangeably. You will gain a deeper understanding of what a class is in later Computer Science courses.
You will soon encounter other types of objects as well, such as lists. Each of these has its own special representation for specifying an object as a literal in a program, and for displaying an object when you print it to the console. For example, list contents are enclosed in square brackets
Check your understanding