Exact Science

Curriculum

Data Types

So, we see that Python can work with at least two types of data - numbers and strings. Numbers are written as a sequence of digits; there may also be a minus sign in front of the number, and strings are written in single quotes. 2 and '2' are different objects, the first object is a number and the second is a string. The + operation works differently for integers and for strings: for numbers it is addition, and for strings it is concatenation.

Subject: ProgrammingCourse: Intro To Python With TurtleAges: Junior, Intermediate, Senior

Theory

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)

Problems