In programming, data type is an important concept. Data types are the classification or categorization of data items. Data types represent a kind of value which determines what operations can be performed on that data. Variables can store data of different types, and different types can do different things.
Python has the following data types built-in by default:
· Text Type: str
· Numeric Types: int, float, complex
· Sequence Types: list, tuple, range
· Mapping Type: dict
· Set Types: set, frozenset
· Boolean Type: bool
· Binary Types: bytes, bytearray, memoryview
Primitive Versus Non-Primitive Data Structures
Primitive data structures are the building blocks for data manipulation and contain pure, simple values of a data. Python has four primitive variable types:
• Integers
• Float
• Strings
• Boolean
Non-primitive data structures don’t just store a value, but rather a collection of values in various formats. In Python, you have the following non-primitive data structures:
• Lists
• Tuples
• Dictionary
• Sets
In this chapter, we will look at the basic or primitive datatypes : str, int, float, complex and bool. Other datatypes we will cover under Data Structure chapter 5.
Numeric
A numeric value is any representation of data which has a numeric value. Python identifies three types of numbers:
· Integer: Positive or negative whole numbers (without a fractional part)
· Float: Any real number with a floating point representation in which a fractional component is denoted by a decimal symbol or scientific notation
· Complex number: A number with a real and imaginary component represented as x+yj. x and y are floats and j is -1(square root of -1 called an imaginary number)
In the below example, let’s look at the usage of numeric types:
##Examples of Integer (int) ##Examples of Floating Point (float) ##Examples of Complex (complex) |
Boolean
Data with one of two built-in values True or False. Notice that ‘T’ and ‘F’ are capital. true and false are not valid booleans and Python will throw an error for them. In the below example, we see example of applying OR operator. This has been discussed in detail in the next chapter.
##Bool type |
Strings
Strings are sequences of character data. The string type in Python is called str. String literals may be delimited using either single or double quotes. All the characters between the opening delimiter and matching closing delimiter are part of the string. If you open the string with single quote, then ending also needs to be single quote. Likewise for double quote.
var_str1 = ‘Name: Sachin’ |
We have dedicated a complete chapter for working with Strings and we will discuss more there.
type() function
Python has an in-built function type() to ascertain the data type of a certain value. For example, enter type(1234) in Python shell and it will return
a = 1234 |
Type Conversion
Converting one type of data to another is possible in Python and its possible only when the conversion can result in a valid data. For example, “5” string type can be converted into Integer but “5a” will throw error when we attempt to convert into integer. There are two types of conversion: Implicit and Explicit conversion.
Implicit Type Conversion is automatically performed by the Python interpreter. Python avoids the loss of data in Implicit Type Conversion. Explicit Type Conversion is also called Type Casting, the data types of objects are converted using predefined functions by the user.
For example, when we divide two integers then the resultant value is automatically saved as float to save the decimal values. Look at below examples to understand it better:
#Type Conversion # implicit (by itself or automatically) conversion to String |