Javaexercise.com

What are the Data Types in Python, and How many Python supports?

Python is a high-level, interpreted programming language, designed and developed by Guido van Rossum in the 1990s.

It is known for its simplicity and easy to understand capability. It is one of the most popular programming languages present in the software industry.

One of the most important but often overlooked concepts in python is that of data types. Let us have a look at them in detail.

What are Data Types?

In python, variables can store different types of information and the type of information is determined by its data type.

Since Python 3 is a dynamically typed language, we do not need to specify the type of variables when we assign data to them. The binding of value with data type is done implicitly by the python interpreter.

There are different categories of data types in Python. Let us have a look at some of them:

Numeric Data Types in Python

There are basically three numeric data types available in python. They are integer, float, and complex.

The integer data type is used to represent integer numeric values, the float is used for decimal numeric values, and the complex is used for representing complex numbers.

Some important points about these data types are - There is no limit on the length of a value of integer data type in python, float stands for floating-point numbers and is usually precise up to 15 decimal places, complex numbers must have the format a + bj where a denotes the real part and b denotes the imaginary part of the number.

To ascertain the type of a variable, we use the type() function. The variable of interest is passed as a parameter here. The integer data type is denoted by the class int. The float data type is denoted by the class float. The complex data type is denoted by the class complex.

Let us look at a few examples of some variables containing data of these types, the python code, and corresponding output:

# integer data type value
x = 2
print(type(x))

# float data type value
y = 2.1
print(type(y))

# complex data type value
z = 2 + 3i
print(type(z))

Output

<class 'int'>

<class 'float'>

<class 'complex'>

Sequence Data Types in Python

There are basically four sequence data types available in python. They are string, list, tuple, and dictionary.

The string type is a commonly used one and represents a sequence of characters. It is denoted by the class str.

A list in python has similar functionality to that of arrays in other programming languages. Lists contain comma-separated values. Lists are enclosed within square brackets []. It is denoted by the class list.

A tuple is similar to a list, the only difference is that a tuple is a non-modifiable read-only data type and it is enclosed in round brackets (). It is denoted by the class tuple.

A dictionary in python consists of a set of pairs of keys and values that are unordered in nature. In python, dictionaries have comma-separated pairs and are enclosed within curly brackets {}. It is denoted by the class dict.

To ascertain the type of a variable, we use the type() function. The variable of interest is passed as a parameter here.

Let us look at a few examples of some variables containing data of these types, the python code, and the corresponding output.

#String type in Python
w = "hello"
print(w)
print(type(w))

#List type in Python
x = [1,2,3]
print(x)
print(type(x))

#Tuple type in Python
y = (1,2,3)
print(y)
print(type(y))

#Dictionary type in Python
z = {'a':1, 'b':2, 'c':3}
print(z)
print(type(z))

Output

<class 'str'>

<class 'list'>

<class 'tuple'>

<class 'dict'>

Boolean Data Type in Python

A boolean data type in python is very useful. It can take only two values - True or False.

It is denoted by the class bool.

To ascertain the type of a variable, we use the type() function. The variable of interest is passed as a parameter here.

Let us look at a few examples of some variables containing data of this type, the python code, and corresponding output:

# boolean type in python
x = True
print(type(x))

y = False
print(type(y))

Output

<class 'bool'>

<class 'bool'>

Set Data Type in Python

The set data type is used to store different values in an unordered manner. The values are comma-separated and enclosed in curly brackets {}.

It is denoted by the class set. These individual values in turn can be of any data type. To ascertain the type of a variable, we use the type() function. The variable of interest is passed as a parameter here.

Let us look at a few examples of some variables containing data of these types, the python code, and the corresponding output.

# Set in Python
x = {1,2,'a'}
print(x)
print(type(x))

Output

<class 'set'>

Conclusion

In this topic, we have learned the use and advantages of data types in a Python program, following a running example of a simple conditional printing program, thus giving us an intuition of how this concept could be applied in real-world situations. Feel free to reach out to info.javaexercise@gmail.com in case of any suggestions.