Values and Data TypesΒΆ
A value is one of the fundamental things — like a word or a number —
that a program manipulates. The values we have seen so far are 5
(the
result when we added 2 + 3
), and "Hello, World!"
. We often refer to these values as objects and we will use the words value and object interchangeably.
Note
Actually, the 2 and the 3 that are part of the addition above are values(objects) as well.
- 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.
If you are not sure what class a value falls into, Python has a function called type which can tell you.
Not surprisingly, strings belong to the class str and integers belong to the class int.
Note
When we show the value of a string using the print
function, such as in the third example above, the quotes are no longer present. The
value of the string is the sequence of characters inside the quotes. The quotes are only necessary to help Python know what the value is.
- Numbers with a decimal point belong to a class called float,
- because these numbers are represented in a format called floating-point.
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 ("
), or three of each ('''
or """
)
- 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!"'
. - Strings enclosed with three occurrences of either quote symbol are called triple quoted strings. They can contain either single or double quotes:
Triple quoted strings can even span multiple lines:
- Python doesn’t care whether you use single or double quotes or the three-of-a-kind 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.