In this tutorial, we will discuss all Data Types used in Python.
Python Data Types
Python is a pure object-oriented programming language, which means that it is made up of Classes and Objects, which you will learn later in this tutorial. For now, to understand, it is defined as a Class structure of data that stores information. To use that information, we use objects, which are the instance of a class.
As in Python, everything is made up of classes, and we use their objects for specific operations. The data type is one of the classes present in Python to use them, we take variables (objects) as their instance. In General, if we define a data type, it is a kind of data item as defined by the values it can take. To find the data type of an object or variable in Python, use the
type()
function.
E.g.
k=10
print(type(k))
#Output
<class 'int'>
There are six types of Data types in Python.
- Numbers
- String
- List
- Tuples
- Dictionary
- Sets
Python Numbers
All the digits, whether accurate or imaginary, come in the Python number data types. There are three types of Numbers in Python Data types: int, float, and complex numbers.
int
Every integer, whether negative or positive, comes in int category, but it does not contain any decimal value. e.g.
var = 10
print(type(var))
#Output
<class 'int'>
Float
Every number which contains a decimal value even .0 is termed the float. e.g.
var=10.0
print(type(var))
Output
<class 'float'>
Complex numbers
A number that has an additional imaginary (a+ib) number comes in the category of complex numbers. e.g.
var=10+10j
print(type(var))
Output
<class 'int'>
Python String
Everything which is written in between double style inverted comma (“ ”) or single inverted comma (‘) is considered as the Python string . It does not matter whether it is a word or digit or a special character except backslash (\) ( it is used to escape the character present in the string). The variable stores the given string sequentially and use indexing to call the specific value present in that variable. Indexing is a method that is used to call the sequentially stored data efficiently.
When Python stores data in sequential order, it gives each value an index number starting with 0, so we can easily grab the specific value. There is a lot more about string which you will learn in the upcoming tutorials. Let’s understand the string with an example:
srting_1="44"
string_2= "This is a String"
print(srting_1[0]) # Here we are using indexing
print(srting_1)
print(string_2)
print(string_2[0:6]) # Here we are using String slicing calling the string_2 values from 0 to 6
#Output
4
44
This is a String This i
Python List
Python list is an ordered collection of different data types which stored in sequential order. The string is mutable which means you can change the values present in a list. To assign a list we use square brackets and to separate the values we use a comma (,). List store the values in sequential order so it uses indexing to call the specific value present inside the list. The indexing in the list start with 0 and end one less than the total number of values present in the list. Let’s understand it with an example:
list1 = [1, 2, 3, 4, 5, 6, 'Hello', 'world', True]
print(list1)
print(list1[1])
print(list1[2])
print(list1[:])
#Output
[1, 2, 3, 4, 5, 6, 'Hello', 'world', True]
1
2
[1, 20, 3, 4, 5, 6, 'Hello', 'world', True]
Python Tuple
Like the Python list, a Python tuple is also a collection of different data types that stores the value in sequential order and use the index to grab the specific values present in the tuple. The only difference between a tuple and a list is tuples are immutable and use parenthesis instead of square brackets. The immutability of the tuple signifies that once the values assigned inside the tuple parenthesis cannot be altered using the assignment operator.
e.g.
tuple_1= (1,2,3,"tuple_1","is", "here")
print(tuple_1)
print(tuple_1[3]) # tuple indexing
print(type(tuple_1))
#Output
(1, 2, 3, 'tuple_1', 'is', 'here')
tuple_1
<class 'tuple'>
Python Dictionary
A Python dictionary is an unordered collection of different data types with keys and value pairs. The Python dictionary does not store the values in sequential order, so we use keys to grab the corresponding values. To store a dictionary in a variable, we use {} curly braces, and like Python, list dictionaries are mutable.
e.g.
dictionary = {"key1": "value1", "key2":"value2", "key3":"value3", "key4":"value4"}
print(dictionary["key1"])
print(dictionary["key2"])
dictionary["key4"] = 4 # dictionaries are mutable
print(dictionary)
#Output
value1
value2
{'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 4}
Python Sets
Python sets are unordered collection of different or unique data types and in the set, you can not assign two repetitive value, if you do so the set itself delete the other one. Sets do not use indexing. e.g.
set_1={1,2,3,4,5,6,6,6,6,6,6,6,7,}
print(set_1)
#Output
{1, 2, 3, 4, 5, 6, 7}
In the above example, the set deletes all the repetitive six and shows only one.
Python Data Conversion
In Python, you get the particular function to change the variable's data type explicitly. When you assign or declare a variable, it automatically assigns a data type to the variable according to the assigned value. Still, with the help of some function, you can change the data type of the declared variable. Here are the following conversions that can be performed in Python.
From integer to float
You can change an integer to a float using the float() function.
print(float(10))
#Output
10.0
From float to an integer using int() function
print(int(10.2653))
#Output
10
From an integer or float to a string using str() function
print(str(400.002))
#Output
‘400.002’
From string to a float and integer using float() and int() functions
k="400"
print(int(k))
#Output
400
print(float(k))
#Output
400.0
From a string to a list using list() function
say="Hello world"
list(say)
#Output
['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']