Python works with at least two basic kinds of data: numbers and strings.
Numbers are written as a sequence of digits (optionally preceded by a minus sign), and strings are enclosed in single quotes.
2
and '2'
are different objects: the first is an int
, the second is a str
.
The +
operator behaves differently for each: for numbers it performs addition, and for strings it performs concatenation.
Besides integers, there is another class of numbers: floats (real numbers), written with a decimal point, e.g. 2.0
.
In some sense 2
and 2.0
represent the same value, but they are different objects.
For example, you can compute 'ABC' * 10
(repeat the string ten times), but you cannot compute 'ABC' * 10.0
.
You can check an object’s type with the type()
function:
>> type(2)
>>> type('2')
>>> type(2.0)