In this post here discussing about data types in python. How, it helps python to take decision on what type of data the variable can access.
A variable is a name of a location in a memory where values are stored. Each variable should be initialised or defined with a data storage format to continue with the operations in the program. These data storage formats are known as data types.
Python has the following data types built-in by default, in these categories:
for text type: | str(string) | ||
for numeric type: | int(integer) | float | complex |
for sequence type: | list | tuple | range |
for mapping type: | dict(dictionary) | ||
for set type: | set | frozenset | |
for boolean type: | bool | ||
for binary type: | bytes | bytearray | memoryview |
Let us see how to initialize data type in python:
Python allows auto-assignment to variables when data is assigned to the variable.
Note: when a variable is initialized with an integer variable, it is by default assigned as str or string. So, for integer variables we need to assign them as integers.
For example:
(str) type a ="lingarajtechhub" (complex) type a=2a (list) type a= [20, 10, 5] (tuple) type a= (20, 10, 5) (dictionary) type a= {"value":20, "value1":10, "value2":5} (boolean) type a= true
We can also assign specific data type in python, for example:
a= str("lingarajtechhub") a=int(10) a=float(10.8) a=list((20, 10, 5)) a=tuple((20, 10, 5)) a=frozenset(("Bhubaneswar", "Kolkata", "Delhi")
We can also check the data type of any variable using the type function, which returns us the type of the variable initialized.
Let us see some examples:
Input:
x="lingarajtechhub" type(x)
Output:
Input:
x=100.88 type(x)
Output:
Here we come to the end of the topic on data types in python. Hope we were able to clear all your doubts.
Please comment and share this article if you find helpful and wants to improve WhatsApp us.