Python Data Types
There are different types of python data types. Every value in Python has a datatype. Since everything is an object in Python programming, data types are actually classes and variables are instance (object) of these classes. Some built-in python data types are below :

1. Python Data Type – Numbers :
Python supports both integers and floating point numbers. There’s no type declaration to distinguish them; Python tells them apart by the presence or absence of a decimal point.
>>> type(1)
<class 'int'>
>>> isinstance(1, int)
>>> 1+1
>>> 1 + 2.7
>>> type(3.7)
<class 'float'>

You can use the type() function to check the type of any value or variable. As you might expect, 1 is an int.

Similarly, you can use the isinstance() function to check whether a value or variable is of a given type.

Adding an int to an int yields an int.

Adding an int to a float yields a float. Python coerces the int into a float to perform the addition, then returns a float as the result.

2. Python Data Type – String :

String is a sequence of characters. Python supports unicode characters. Generally strings are represented by either single or double quotes.

>>> a = "string in a double quote"
>>> b = 'string in a single quote'
>>> print(a)
string in a double quote
>>> print(b)
string in a single quote
>>> # using ',' to concate the two or several strings
>>> print(a,"concated with",b)
string in a double quote concated with string in a single quote
>>> #using '+' to concate the two or several strings
>>> print(a+" concated with "+b)
string in a double quote concated with string in a single quote

3. Python Data Type – List :

List is a versatile data type exclusive in Python. In a sense it is same as array in C/C++. But interesting thing about list in Python is it can simultaneously hold different type of data. Formally list a ordered sequence of some data written using square brackets([]) and commas(,).

>>> #list of having only integers
>>> a = [1,2,3,4,5,6,7,8,9]
>>> print(a)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> #list of having only strings
>>> b=["hello","Free Time Learning"]
>>> print(b)
['hello', 'Free Time Learning']
>>> #list of having both integers and strings
>>> c= ["Hi","F T L",1,2,3,"Bye"]
>>> print(c)
['Hi', 'F T L', 1, 2, 3, 'Bye']
>>> #index are 0 based. this will print a single character
>>> print(c[1]) #this will print "you" in list c

4. Python Data Type – Tuple :

Tuple is another data type which is a sequence of data similar to list. But it is immutable. That means data in a tuple is write protected. Data in a tuple is written using parenthesis and commas.

>>> #tuple having only integer type of data.
>>> a=(1,2,3,4,5,6,7,8,9)
>>> print(a) #prints the whole tuple values
(1, 2, 3, 4, 5, 6, 7, 8, 9)
>>> #tuple having multiple type of data.
>>> b=("hello", 1,2,3,"ftl")
>>> print(b) #prints the whole tuple
('hello', 1, 2, 3, 'ftl')
>>> #index of tuples are also 0 based.
>>> print(b[4]) #this prints a single element in a tuple, in this case "flt"

5. Python Data Type – Set :

Set is an unordered collection of unique items. Set is defined by values separated by comma inside braces { }. Items in a set are not ordered.

>>> a = {5,2,3,1,4}
>>> # printing set variable
>>> a = {2,6,3,5,4,1}
>>> # printing set variable
>>> print("a = ", a)
a =  {1, 2, 3, 4, 5, 6}
>>> # data type of variable a
>>> print(type(a))
<class 'set'>

6. Python Data Type – Dictionary :

Python Dictionary is an unordered sequence of data of key-value pair form. It is similar to the hash table type. Dictionaries are written within curly braces in the form key:value. It is very useful to retrieve data in an optimized way among large amount of data.

>>> #a sample dictionary variable
>>> a = {1:"first name",2:"last name", "age":27}
>>> #print value having key=1
>>> print(a[1])
first name
>>> #print value having key=2
>>> print(a[2])
last name
>>> #print value having key="age"
>>> print(a["age"])

We see you’re using an ad blocker.

Our website is made possible by displaying ads to our visitors.

Please help us continue to provide you with free. So please disabling your ad blocker.