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 example values are the
number 5
and the text "Hello, World!"
. We often refer to
these values as objects and we will use the words value and object
interchangeably.
These objects are classified into different classes, or data
types: 4
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.
Not surprisingly, strings belong to the class str and integers belong to the class int.
Continuing with our discussion of data types, 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. We’ll come back to a deeper understanding of what a class is in later chapters.
What about values like "17"
and "3.2"
? They look like numbers,
but they are in quotation marks like strings.
They’re strings!
Strings in Python can be enclosed in either single quotes ('
) or
double quotes ("
- the double quote character).
Double quoted strings can contain single quotes inside them, as in
"Bruce's beard"
, and single quoted strings can have double quotes
inside them, as in 'The knights who say "Ni!"'
.
Python doesn’t care whether you use single or double quotes to surround your strings. Once it has parsed the text of your program or command, the way it stores the value is identical in all cases, and the surrounding quotes are not part of the value.
Knowing the type of a value is important because operations may
require a particular type. For example, the turtle.forward
function requires inputs of type float:
import turtle
turtle.forward("one hundred") # Error: input must be a float
A type is also required when declaring a variable:
The type declaration is used to ensure that you don’t accidentally use a value with the wrong type. For example, you can’t assign a string to a float variable, otherwise it might cause an error later in the program when you try to use the variable:
import turtle
length: float
length = "one hundred" # Error: value type must match variable type
turtle.forward(length)
Check your understanding
- Character
- It is not a single character.
- Integer
- The data is not numeric.
- Float
- The value is not numeric with a decimal point.
- String
- Strings can be enclosed in single quotes.
data-2-1: What is the data type of 'this is what kind of data'
?
- Character
- It is not a single character.
- Integer
- Integers have no decimal point and no quotes.
- Float
- The value is not numeric with a decimal point.
- String
- The value is not enclosed in quotes.
data-2-2: What is the data type of 2300
?
- Character
- It is not a single character.
- Integer
- The data is not numeric.
- Float
- The value is not numeric with a decimal point.
- String
- Strings can be enclosed in single quotes.
data-2-3: What is the data type of '99'
?