In every language we use variable to store data in memory location , same in python we use variables to store data, unlinke in any other language in python we not used to define datatype of variable prior to save data in variable, it will automatically identify datatype used for variable.

>>> a="string"
>>> type(a)
<type 'str'>

>>> b=10
>>> type(b)
<type 'int'>

>>> c=10.90
>>> type(c)
<type 'float'>

See above, variables show datatype used to stored without any pre-defined information. In python we can also do multiple assignation simultaneously in variables like below.

>>> a=b=c="string"
>>> type(a)
<type 'str'>
>>> type(b)
<type 'str'>
>>> type(c)
<type 'str'>
>>> a
'string'
>>> b
'string'
>>> c
'string'
>>> 

Same way we could also do sequences, so all variables will store different values like below.

>>> a,b,c="string",10,10.90
>>> a
'string'
>>> b
10
>>> c
10.9

In python we, have some basic data types, which we should study and understand. These basic data types are used to manage various variables and operations with same. Below are are basic data types used in Python

  • Numbers
  • Strings
  • List
  • Tuple
  • Dictionary
  • Let’s do some description and discussion on same Data types one by one:-

    Numbers

    Numbers are data types used for numeric values and variables used in python codes like age, number of years etc.

    >>> var1=90
    >>> var2=238
    >>> var3=var2-var1
    >>> var3
    148
    >>> type(var3)
    <type 'int'>
    >>> type(var1)
    <type 'int'>
    >>> type(var2)
    <type 'int'>
    

    There are basic two types of Numbers in Python, Int and Float. In above code we have used int Numbers. In python, unlike other languages we need not to mentioned any details for int and float type of numbers. See in below examples.

    >>> var1=78
    >>> var2=78.9
    >>> type(var1)
    <type 'int'>
    >>> type(var2)
    <type 'float'>
    

    Strings

    String is contiguous characters which represent in quotation marks (single, double or triple).like below .

    >>> str1="string"
    >>> str2='string'
    >>> str3="""string"""
    >>> type(str1)
    <type 'str'>
    >>> type(str2)
    <type 'str'>
    >>> type(str3)
    <type 'str'>
    

    Strings indexes could access with indexes numbers in strings. Indexes start from 0 index and in case need to access last index we can used -1 index.

    >>> str1[0]
    's'
    >>> str1[-1]
    'g'
    >>> str1
    'string'
    

    In case we need to access range of indexes called slicing strings then we could specify like 0:2 , this wll include only 0,1 exclude last mentioned index , like below.

    >>> str1[0:2]
    'st'
    >>> str1[2:]
    'ring'
    

    So this is index for python string, but what if we need to same thing from reverse index.

    >>> str1[-3:]
    'ing'
    

    So to represent it properly we should draw a diagram for same.

    0   1   2   3   4   5
    ---------------------
    s   t   r   i   n   g
    ---------------------
    -6 -5  -4  -3  -2  -1
    

    List

    In python list are most flexible and used data type. List contains number of items separated through comma and enclosed with square brackets[].
    Values in list could be access though indexing, that start with 0 in begin and with -1 from end. Python list are changeable or mutable data types that has various operation to work on them which make it famous to use.

    see examples below.

    >>> lst=[1,2,3,4,5,6,7,8,9,10]
    >>> type(lst)
    <type 'list'>
    >>> lst[9]
    10
    >>> lst[-1]
    10
    >>> lst[0]
    1
    >>> lst[-10]
    1
    >>> lst
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    >>> print lst
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    

    Above we have create lst list with range of number from 1 to 10, we tried to access first and last index through both ways. As mentioned we could various operations with list data types, see a example below.

    >>> lst
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    >>> lst.append(11)
    >>> lst
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
    >>> lst.pop()
    11
    >>> lst
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    

    Tuple

    Python has one another data type which also has same sequence of items separated through comma like list but enclosed parentheses () and tuples are unchangeable and immutable or read only. we can’t do any write operation on same. See below examples

    >>> tpl=(1,2,3,4,5,6,7,8,9,10)
    >>> type(tpl)
    <type 'tuple'>
    >>> len(tpl)
    10
    
    >>> tpl[0]
    1
    

    We can only query data mentioned in tuple data type variable. No way to update or change data inside it.

    Dictionary

    Dictionary are hash-table kind data type, which has unordered key values pairs.A key can be almost any Python data type, mostly used numbers or strings. Values, can be any arbitrary Python object. This data type enclosed with curly braces.

    values could access through Dictionary key. see examples below.

    >>> mnt={1:"Jan",2:"Feb",3:"Mar",4:"Apr",5:"May",6:"Jun",7:"Jul",8:"Aug",9:"Sep",10:"Oct"}
    >>> mnt
    {1: 'Jan', 2: 'Feb', 3: 'Mar', 4: 'Apr', 5: 'May', 6: 'Jun', 7: 'Jul', 8: 'Aug', 9: 'Sep', 10: 'Oct'}
    >>> mnt[4]
    'Apr'
    >>> mnt[11]="Nov"
    >>> mnt[12]="Dec"
    >>> mnt
    {1: 'Jan', 2: 'Feb', 3: 'Mar', 4: 'Apr', 5: 'May', 6: 'Jun', 7: 'Jul', 8: 'Aug', 9: 'Sep', 10: 'Oct', 11: 'Nov', 12: 'Dec'}
    >>> 
    

    In above examples, we create one dictionary which contains months from Jan to Oct and further add Nov,Dec on same.
    We could also return keys or values only from Dictionary

    >>> mnt.keys()
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
    >>> mnt.values()
    ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']