Skip to main content

Section 3.9 Data Types

If you are not sure what class (data type) 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 3.9.1.

When we show the value of a string using the print function, such as in the third example above, the quotes are not present in the output. 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.
In programming, a function is a subroutine, designed to perform a specific task. When you "call" a function, you’re asking it to execute its task. Functions can take in information, known as arguments, work with that information, and then often return a result. For example, print() is a function which takes in a single string argument between the parentheses and performs the task: Printing the string argument we provided to the terminal.
Let’s take a closer look at the print statements above.
print(type("Hello, World!"))
What we have here is a function call inside a function call. In most, if not all programming languages, when you have a function call inside another function call, the following steps are taken:
  1. Start by evaluating the function call(s) that are the most deeply nested. Process any arguments and return their results
  2. Continue this process, passing results from each function call to the next level up, until you reach the outermost function.
  3. Perform the operation of the outermost function with the final result from the previous step.
Going back to the examples, we first call the type() function with the argument "Hello, World!". The type() function returns the data type of the given value. In this case, "Hello, World!" is a string, so type("Hello, World!") will return < class ’str’ >, indicating that the value is of type str (short for string). The print() function then outputs this result to the console, so when you run this line, you’ll see < class ’str’ >. print(type(17)): Similarly, this line calls the type() function with the argument 17. Since 17 is an integer, type(17) will return < class ’int’ >, indicating that the value is of type int (short for integer). The print() function then outputs this result to the console, so running this line will display < class ’int’ >.
Don’t worry if this seems challenging. As we progress in the course you’ll get more familiar with functions and even design your own! Each new concept and challenge is a chance to develop your problem-solving skills and enhance your programming abilities. Every mistake and difficulty is a valuable part of the learning process. Stay engaged and keep asking questions.
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.
So the Python language designers usually chose to surround their strings by single quotes. What do you think would happen if the string already contained single quotes?
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, a print statement 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: formal languages are strict, the notation is concise, and even the smallest change might mean something quite different from what you intended.

Note 3.9.2.

The examples in this online text describe how print works in Python 3. If you install Python 2.7 on your machine, it will work slightly differently. One difference is that print is not called as a function, so there are no parentheses around the values to be printed.
Check your understanding

Checkpoint 3.9.3.

How can you determine the type of a variable?
  • 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, like when a string prints, there are no quotes around it.
  • Use the type function.
  • The type function will tell you the class the value belongs to.
  • Use it in a known equation and print the result.
  • Only numeric values can be used in equations.
  • Look at the declaration of the variable.
  • In Python variables are not declared. Values, not variables, have types in Python. A variable can even take on values with different types during a program’s execution.

Checkpoint 3.9.4.

What is the data type of β€˜this is what kind of data’?
  • 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.
You have attempted of activities on this page.