Table of Contents » Chapter 2 : Data (Input) : Data Types
Data Types
Contents
- Overview
- Data Types
- Examples
- The type() Function
- Conclusion
Overview
In programming, data types are an essential concept that define the type of data that can be stored and manipulated within a program. They determine the nature of the data (such as numeric, textual, or more complex structures) and the operations that can be performed on that data. Basic data types include integers, floating-point numbers, characters, strings, and booleans. Languages like Python offer more complex types like lists, tuples, and dictionaries. Each data type has its own attributes and behaviors; for instance, integer types can store whole numbers, while floating-point types are used for numbers with decimal points. Choosing the appropriate data type is crucial for efficient memory use and ensuring that operations on the data are performed correctly. Python is a dynamically typed language, which means the interpreter infers the data type automatically. Understanding data types is fundamental to programming, affecting how data is stored, retrieved, and manipulated.
Python supports a variety of built-in data types that provide different data-handling scenarios. We will begin by exploring four fundamental types:
- Strings: Strings are alphanumeric (textual) (sometimes called text) data which are sequences of characters that can include any character (alphabetic characters, numbers, and symbols) found on the keyboard. In Python, strings are identified by the abbreviation str.
- Integer Numbers: Whole (sometimes called counting) numbers that can be positive, negative, or zero, and do not include any fractional or decimal parts. In Python, integers are identified by the abbreviation int.
- Floating point Numbers: Real numbers (sometimes called decimals) are values that can have fractional or decimal parts. In Python, floating point values are identified by the abbreviation float.
- Booleans: This data type can have one of two (and only two) possible values: True and False. This data type is used to represent truth conditions and make decisions. In Python, booleans are identified by the abbreviation bool.
Let's consider a couple of concepts that are key to understanding data types:
Consider the following practical example of the data types listed above.
We need to write a program that processes data entered by a user. The user fills out a form (Figure 1.) and clicks a Save button.
This form represents information about the user, Bob Smith. How many datum items does this form contain? It prompts the user for their first name, last name, age, employment status, job title, and hourly wage, so six datum. There would be seven, however the Employed? question would be stored as a single value based on their answer.

Figure 1: Example of a data entry form.
What are the data types of these items? See Figure 2. The first name, last name, and job title are all expected to be alphanumeric (strings), so they would be str's. As we saw earlier, age is an integer (whole counting number with no decimal or fractional portion) or int in Python. The employment status is a yes or no value. As the above chart indicates, we call this boolean, or bool in Pthon. And the hourly wage is a number, but not an integer because it has scale. So hourly is a decimal number, which in Python programming we call a floating-point number, or float.

Figure 2: Example of a data entry form.
The type() function in Python is used to determine an object's data type. It returns the type of the given object. The syntax of the type function is as follows:
type(object)
The object parameter represents the object whose type needs to be determined. The function's return value is called the type object, which reports the type or class of the given object. We will now use the type() function to check the data type of constant and literal values. We will expand the use of the type() function soon when discussing variables.
Code Examples
print(type(10))
print(type(3.14))
print(type('A'))
print(type("Bob Smith"))
print(type(10 / 20))
print(type(10 > 20))
Output
< class 'int' >
< class 'float' >
< class 'str' >
< class 'str' >
< class 'float' >
< class 'bool' >
Code Details
- Code Line 1: This line uses the type() function to report on the data type of the value 10, which is an integer. Output Line 1 shows the result of printing the outcome of the type function on 10, as an 'int', meaning an integer type value.
- Code Line 2: This line uses the type() function to report on the data type of the value 3.14, which is a decimal float value. Output Line 2 shows the result of printing the outcome of the type function on 3.14, as a 'float', meaning a decimal float type value.
- Code Line 3: This line uses the type() function to report on the data type of the value 'A', which is a string. Output Line 3 shows the result of printing the outcome of the type function on 'A', as an 'str', meaning a string type value.
- Code Line 4: This line uses the type() function to report on the data type of the value 'Bob Smith', which is a string. Output Line 4 shows the result of printing the outcome of the type function on 'Bob Smith', as an 'str', meaning a string type value.
- Code Line 5: This line uses the type() function to report on the data type of the expression (10 / 20). This expression, dividing 10 by 20, results in a float value 0.05, so the result of using the type() function on this expression means to report on the data type of the result of the expression (remember our order of operations). Output Line 5 shows the result of printing the outcome of the type function on this expression is a 'float', meaning the result of the expression is a decimal float type value.
- Code Line 6: This line uses the type() function to report on the data type of the expression (10 > 20). This expression asks the question - is the value 10 greater than the value 20? The result of this expression is False. Remember, though, the type() function reports the data type of that result, so Output Line 6 shows the type of 'bool', which means boolean.
On this page we learned basic ideas about data and data types. There is much more to learn about these topics, which we will continue to explore as we learn Python. Our next step is to learn how to store data in variables.