Python Data Types

Python as programming involves a lot of data processing work. The data type features of programming language allow programmers to organize different kinds of data. Python provides a rich collection of data types that can be used to perform the various task the programmer does in another language. Python provides valuable and unique data types such as tuples and dictionaries and avoids the use of pointers that make programming confusing. Python is a dynamically typed language, meaning you don’t have to identify the data type when you initialize a variable explicitly. Data types represent the different types of data(like a numeric value, string, Boolean, etc.) that you can work with.

Python has several native data types:

The list of data types used in Python is as follows:-

  1. Numeric(Integer,Float,Complex Number).
  2. Sequence Type(Lists,Tuple,Strings).
  3. Boolean(bool).
  4. Set(set, frozenset).
  5. Dictionary(key:value pairs).

Python Numeric Data Types

In the Python language, numeric data types represent the type of data that consists of numbers. The number can be an integer, or it can be a float value or a complex one.

Python Integer

Integers are represented by the int keyword. It can contain positive as well as negative values.

Python Float

Float numbers are represented by the float keyword. It is also a numeric value, but it contains decimal values in it as well(like 1.1,2.3,5.0, etc.).

Complex Numbers

Complex numbers are represented by the complex keyword. It has a real part as well as an imaginary part.

To understand the different types of numeric values, let us take an example.

Here, you use a type() function that shows the data type.

			

a = 90
print("The value a represents: ", type(a))
b = 10.9
print("\nThe value b represents: ", type(b))
c = 7 + 3j
print("\nThe value c represents: ", type(c))

				

Output:

The value a represents: <class 'int'> The value b represents: <class 'float'> The value c represents: <class 'complex'>

Sequence Type

In the Python scripting language, the sequence is the data type that contains the ordered group of data types(similar or different). The sequence helps to reserve many values in an orderly manner.

There are different sequence types:-

Python String

A string is a sequence that contains characters. The str class represents a string. In the example below, you use a variable called it_is_a_string to store our sequence of characters.

			

it_is_a_string = 'Hi guys I am a Robot'
print(it_is_a_string)

				

Output:

Hi guys I am a Robot

How to access elements in a string

A string consists of many elements, and the index starts from 0. If you have a string “Earth” and the index would be 0->E,1->a,2->r,3->t,4->h i.e., consisting of 5 elements (0,1,2,3,4).

			

Planet_name = 'Earth'
print("First element of the string: ")
print(Planet_name[0])

				

Output:

First element of the string: E

Python Tuple

A Tuple is a collection of ordered strings or numbers. What makes Tuple different from a list is that you cannot modify or change elements of a tuple after you have created it. A Tuple is created by putting elements one after the other, separated by commas. You use () braces while making a tuple. And accessing an element in a Tuple is as simple. The index starts from 0 for the elements in a tuple. You can use print(A_tuple[0]) to display Earth.

			

A_Tuple = ('Earth','Venus','Mars','Mercury') 
print("Some planet names:") 
print (A_Tuple) 

				

Output:

Some planet names: ('Earth', 'Venus', 'Mars', 'Mercury')

Python List

A List is also a collection of data (strings, numbers, etc.). What makes a list different from Tuple is that you can modify or change elements in a list after you have created it. A List is created by putting elements one after the other separated by commas. You use [] braces while making a List and accessing an element in a list is easy. The index starts from 0 for the elements in a List. You can use print(A_List[0]) to display Earth.

			

A_List = ['Earth','Venus','Mars','Mercury']
print("Some planet names:") 
print (A_List)

				

Output:

Some planet names: ['Earth', 'Venus', 'Mars', 'Mercury']

Python Boolean

Boolean is a data type that can only display true or false. Boolean objects can only give true or false values. Those objects that are not boolean, i.e., non-boolean, can also be assessed in True or False. A boolean data type is case sensitive i.e., True and true are not the same.

			

print(type(True))
print(type(False))
#The output will show both belong to the class bool

				

Output:

<class 'bool'> <class 'bool'>

Python Set

A Python Set is a data type that also consists of a collection of data types (like strings, numbers, etc.), and in a set, the elements are unordered. You use the set()function in Python to make a set. It can contain different elements of different types together, and commas separate them. Set is a built-in function in Python. You use () braces while making a set, and accessing an element in a set is easy. The index starts from 0 for the elements in a set. You can use print(A_set[0]) to display Mercury.

			

A_set = set(['1','2','3','Mercury','Venus','Earth']) 
print("\nPlanet's and their number") 
print(A_set)

				

Output:

Planet's and their number {'Mercury', '3', 'Venus', 'Earth', '1', '2'}

Python Dictionary

A Python Dictionary is a data type that contains a collection of data types(like strings, numbers, etc.), and in a Dictionary, the elements are unordered. In a Dictionary, you have key-value pairs like key: value separated by commas. The keys you use in a Dictionary are case sensitive that means Object and object are not the same thing. You use {} braces while making a Dictionary. You use the dict() function to create a Dictionary in Python.

			

A_Dict = dict({1: 'Mercury', 2: 'Venus', 3:'Earth'}) 
print("\nDictionary containing planet names and their number: ") 
print(A_Dict)

				
Dictionary containing planet names and their number:

Output:

{1: 'Mercury', 2: 'Venus', 3: 'Earth'}