Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




PYTHON - DATA TYPES


We have seen a variable is used to hold a value. Be it a number or a name or even a sentence.


i.e.


x = 5

Or


y = "Hello World"

But how will Python come to know, if the value is a number or a name?


Well ! Once again, that's the beauty of Python. Python makes an intelligent guess, if the value is a number or a name or a sentence or something else and create the variable accordingly.


But ! Just think for a moment. Python must be using some mechanism by which it can determine if the value assigned to a variable is a number or a name or a sentence or something else.


And thus 'Data Types' comes into picture.


Note : In other languages, you have to know all the Data Types to declare a variable. But in Python, it is not necessary for you to know the Data Types. You can just assign the values and Python will do the task of determining the Data Types.


Data Types


'Data Types' are the hidden gem that is used by Python to determine if the variable is going to hold a number or a name.


But why does Python needs to determine if the variable is going to hold a number or a name? Can it not create one type of variable and store all the values there.


It cannot do that. Because if you want to store a number. It would take less space.


Python Data types

On the other hand, if you want to store a String like 'Hello World'. It would take a larger space.


Python Data types

So, whenever you want to create assign a String to a variable.


y = "Hello World"

Python makes an intelligent guess and finds out, it is a String type value and creates the storage space accordingly.


Python Data types

The above 'Data Type' for String is called 'str' 'Data Type'.


And similarly for a number, for a decimal value e.t.c. Python has different 'Data Types'.


Note : We will keep the explanation of all the Data Types as short as possible. And will be having separate tutorials for all the Data Types.


List of all Data Types used in Python


  1. String

    As we have seen in the above example, a String is a set of letters/alphabets.


    1. str Data Type

      The 'Data Type' for String is 'str'.

      When Python finds data inside Double Quotes ("") of Single Quotes (''). It creates a String type variable for that value.

      y = "Hello World"


      Or

      y = 'Hello World'


      So, the 'Data Type' of a variable is determined by Python and is hidden from us. Now, if we want to see the 'Data Type' of a variable, Python provides a function called 'type(...)' that tells us the 'Data Type' of a variable.

      Let us look at the 'type(...)' function with the below example :

      y = "Hello World"
      print(type(y))

      Output :



        str


      In the above code we have initialised the variable 'y' with the String 'Hello World'.

      y = "Hello World"


      Then in the 'print(...)' statement, we have used the 'type(...)' function to get the 'Data Type' of the variable 'y'.

      print(type(y))


      And the 'type(...)' function had given us the output as 'str'.
  2. Numbers

    Numbers could be a floating point number (i.e. 5.3322) or a whole number (i.e. 7 or 5).

    In addition to floating point and whole number, Python also supports a third type of Number Data Type called Complex Data Type. Let us see them below :


    1. int Data Type

      The 'Data Type' for a whole number is 'int'.

      Example :


      x = 5
      print(type(x))

      Output :



        int


    1. float Data Type

      The 'Data Type' for a floating point number is 'float'.

      Example :


      x = 5.987
      print(type(x)) 
      					

      Output :



        float


    1. complex Data Type

      In Mathematics, you can find lots of formulas where there is an imaginary part.

      Say, for example
      x = 2j


      Where 'j' is the imaginary part.

      Example :


      x = 2j
      y = 2 + 5j
      print(type(x)) 
      print(type(y))


      Output :



        complex
        complex


      So , both the expressions,
      x = 2j


      And
      y = 2 + 5j


      Are 'complex' 'Data Types' and can be represented in Python.
  3. Collections

    So far we have seen, how to store a Name, a sentence or numbers in separate variables.

    But what if , we want to store a name and a number in the same variable ?

    Well ! Collections is the Solution. Collections , as the name suggests is going to collect something. It is going to store multiple values in one variable.

    There are four Data Types for Collections in Python. They are 'List' , 'Set' , 'Tuple' and 'Dictionary' .


    1. list Data Type

      A 'list' 'Data Type' is a Collection that holds multiple values. And the speciality of a 'list' 'Data Type' is the elements are ordered (We will explain it), the values are changeable and allows duplicate values.

      The declaration of a 'list' 'Data Type' is super easy. You can place the multiple values inside square brackets '[ ]' and Python will understand.

      Example :



      x = [5, "John", "Python"]
      print(type(x))
      print(x) 


      Output :



        <class 'list'>
        [5, 'John', 'Python']


      So , if you look at the output. The 'Data Type' of 'list' is '<class 'list'>' . We will explain 'class' later. Just remember ,

      x = [5, "John", "Python"]


      Is of Type 'list' .

      And when we print the 'list' 'x' .

      [5, 'John', 'Python']


      We can see that the values are displayed in the same order it was inserted. And this is why a 'list' is ordered.


    1. set Data Type

      A 'set' 'Data Type' is a Collection that can also hold multiple values. And in the 'set' 'Data Type' , the elements are unordered and doesn't allow duplicate values.

      The declaration of a 'set' 'Data Type' is also quite simple. You can place the multiple values inside braces '{ }' and Python will understand.

      Example :



      x = {8, 4 ,1, 9}
      print(type(x))
      print(x) 	


      Output :



        <class 'set'>
        {8, 1, 4, 9}


      So , if you look at the output. The 'Data Type' of 'set' is

      <class 'set'>


      Is of Type 'set' .

      And when we print the 'list' 'x' .

      {8, 1, 4, 9}


      We can see that the values are not displayed in the same order it was inserted. That is why a 'set' 'Data Type' is unordered.


    1. tuple Data Type

      A 'tuple' 'Data Type' is a Collection that also holds multiple values. And in the 'tuple' 'Data Type' , the elements are ordered, unchangeable and allows duplicate values.

      The declaration of a 'tuple' 'Data Type' is also quite simple. You can place the multiple values inside brackets '( )' and Python will understand.

      Example :



      x = ("Ram", "John", 7)
      print(type(x))
      print(x)


      Output :



        <class 'tuple'>
        ('Ram', 'John', 7)


      So , if you look at the output. The 'Data Type' of 'tuple' is

      <class 'tuple'>


      Is of Type 'tuple' .

      And when we print the 'tuple' 'x' .

      ('Ram', 'John', 7)


      We can see that the values are displayed in the same order it was inserted. That is why a 'tuple' 'Data Type' is ordered.


    1. dict Data Type

      The 'dict' is abbreviated as dictionary. As the name suggests, a 'dict' 'Data Type' is almost same as any English Dictionary.

      Just like in an English Dictionary, you search for any word and find the definition for it.

      Similarly , in 'dict' 'Data Type' , data is stored in 'key' and 'value' format.

      Say for example , there are just three students in the class, with roll numbers 1 , 2 and 3 .



      Roll Name
      1 John
      2 Sam
      3 Lohit


      Now if the Roll is the 'key' and Name is the 'value' . You can search with the 'key' (i.e. The Roll) and get the 'value' (i.e. The Name) .

      Now to define 'dictionary' 'Data Type' , we can say that a 'tuple' 'Data Type' is a Collection that also holds multiple values.

      And in the 'tuple' 'Data Type' , the elements are unordered, changeable and does not allow duplicate values.

      The declaration of a 'dictionary' 'Data Type' is also quite simple. You can place multiple key and value pairs inside braces '{ }' and Python will understand.

      Example :



      x = {
          "1": "John",
          "2": "Sam",
          "3": "Lohit"
          }
      print(x)


      Output :



        {'1': 'John', '2': 'Sam', '3': 'Lohit'}
  4. Booleans

    Boolean represents just two values , i.e. 'True' or 'False' .


    1. bool Data Type

      A 'bool' 'Data Type' can accept only 'True' or 'False' .

      Example :



      x = True
      y = False
      print(x) 
      print(type(x))
      print(y)  
      print(type(y))  


      Output :



        True
        <class 'bool'>
        False
        <class 'bool'>


      Just remember the first letter of 'True' and 'False' should be in upper case.

      x = true is invalid.