Skip to main content

Section 2.2 Values and Data Types

Let’s look at a program that calculates a 27-year-old person’s approximate age in days:
Each of the things we are printing is a value, one of the fundamental things that a program manipulates. The values in this program are "My age in days is about", 27, and 365. 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: 27 is an integer, and "My age in days is about" is a string, so-called because it contains a string, or sequence, of letters. You (and Python) can identify strings because they are enclosed in quotation marks.
Numbers with a decimal point belong to a class called float, because these numbers are represented in a format called floating-point. (See this video 1  to find out where the term floating-point comes from.)
If you are not sure what class a value falls into, Python has a function called type which can tell you.
What about values like "17" and "3.2"? They look like numbers, but they are in quotation marks like strings.
They’re strings!
When you type a large integer, you might be tempted to use commas between groups of three digits, as in 42,000. This is not a legal integer in Python, but it does mean something else, which is legal:
Well, that’s not what we expected at all! Because of the comma, Python chose to treat this as a pair of values. In fact, the print function can print any number of values as long as you separate them by commas. Notice that the values are separated by spaces when they are displayed.
Remember not to put commas or spaces in your integers, no matter how big they are. Also revisit what we said in the previous chapter: programming languages are strict, the notation is concise, and even the smallest change might mean something quite different from what you intended.

Checkpoint 2.2.1.

    How can you determine the type of a value?
  • Print out the value and determine the data type based on the value printed.
  • You may be able to determine the data type based on the printed value, but it may also be deceptive. For example, when a string prints, there are no quotes around it.
  • Use the type function.
  • Use it in a known equation and print the result.

Checkpoint 2.2.2.

    What is the data type of "this is what kind of data"?
  • Character
  • Integer
  • Float
  • String
www.youtube.com/watch?v=R7Qb3LoSo84