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

**. Other datatypes we will cover under Data Structure chapter 5.**

*bool*** **

**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:

var_int2 = 89 print( “Sum of two integer values are: “, var_int1+var_int2)## Output:#Sum of two integer values are: 145
var_float2 = 89.0 print( “Sum of two float values are: “, var_int1+var_int2)## Output:#Sum of two float values are: 145.0
var_complex2 = 5 – 6j print( “Multiplying 2 complex numbers give us: “, var_complex1+var_complex2)## Output:#Multiplying 2 complex numbers give us: (10+0j) |

**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.

Truevar_bool2 = Falseprint(“Performing OR operation on give two bool values: “, var_bool1 or var_bool2)#Output: #Performing OR operation on give two bool values: True |

** **

**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 = “Age: 14 yrs"print(var_str1, “n“, var_str2)#Output#Name: Sachin # Age: 14 yrs |

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 “Value of the data is {0} and Type of the data is {1}".format(a,type(a)))a = Trueprint(“Value of the data is {0} and Type of the data is {1}".format(a,type(a)))a = 3+8j print( “Value of the data is {0} and Type of the data is {1}".format(a,type(a))) |

** **

**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:

** **

“59"
var2 = float(var1) #explicit type castingprint(type(var2))print(var2) ### anything to integer: int()var1 = 5var2 = 3 var3 = var1/var2 #var1 and var2 are Integer but var3 is automatically (implicit) converted to floatprint(#float type conversion is done to save the decimal values “Value of var3 is {0:0.3} and the datatype is {1}".format(var3, type(var3))) |

** **

** **