Skip to main content

Section 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 marks.
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., 5 or 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., "Hello, World!").
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 print function to see a printed representation in the console.
The printed representation of a number uses the familiar decimal representation (reading Roman Numerals
 1 
http://en.wikipedia.org/wiki/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

Checkpoint 2.2.1.

    What appears in the console when the following statement executes?
    print("Hello World!")
    
  • Nothing is printed. It generates a runtime error.
  • "Hello World!" has a printed representation, so there will not be an error.
  • "Hello World!"
  • The literal in the program includes the quote marks, but the printed representation omits them.
  • Hello World!
  • The printed representation omits the quote marks that are included in the string literal.

Checkpoint 2.2.2.

    What is the data type of the literal in the program below?
    print("Twenty")
    
  • String
  • Yes, because it is enclosed in quotation marks, it is a string.
  • Integer
  • No, it is enclosed in quotation marks
  • Number
  • No, even though these letters spell out a number word, Python will not see this as a number

Checkpoint 2.2.3.

    What is the data type of the literal in the program below?
    print("17")
    
  • String
  • Yes, because it is enclosed in quotation marks, it is a string.
  • Integer
  • No, it is enclosed in quotation marks
  • Number
  • No, even though this is numerals, Python will not see this as a number because it is enclosed in quotation marks

Checkpoint 2.2.4.

    What is the data type of the literal in the program below?
    print(341)
    
  • String
  • No, this is not a String because it is not enclosed in quotation marks.
  • Integer
  • Yes, this is an integer (or whole number)
  • Number
  • No, number is not a specific data type. This is an integer (a type of number that is whole, without a decimal portion)
You have attempted of activities on this page.