Py04: What Type of Data are you?

Py04: What Type of Data are you?


Everyone knows that any scripting language shootout that doesn't show Python as the best language is faulty by design. 

- Max M

In the previous post, we learned about various operators available in Python. We talked about various operators to perform functions on two number which were defined as shown below:

 >>> variable_one = 1
 >>> variable_two = 2

Here, we had described two variable but what is so special about them. This one of those types we are going to discuss in this post.

Python support various types of data (datatypes) which are directly built in the language. But for this post, we're going to focus on six of them. Why? We'll learn about other types in future posts where we can talk about them in detail.

Let's start with renaming our variable:

 >>> variable_one = 1
 >>> variable = variable_one
 >>> del variable_one

If you look closely, you might find the third line new and interesting. There is new keyword there del, looking like delete word short form. But it does not actually delete the value but the binding of name to it. So, our value 1 hasn't gone anywhere but the variable, variable_one, pointing to it is removed.
So, that is enough about del keyword let's move on to today's topic.

Like, I said we're going to talk about six datatypes. Let's start with them one by one:


  1. int
    • int, as the name indicates, it stands for integer. This datatype represents the integers in the language.
    • Every whole number you had declared or will declare in future is of int type.
    • But why you should trust me. You are right. Let me prove that any whole number will always of type integer.
    • Your disbelief on me can be removed with help of my friend, Python. Python provides a build in function, type,  which help us to find check the type of variable or object.
    • So, switch to console, declare a variable named variable and run following commands:
      •  >>> type(variable)
        • It should bring the following output, <class 'int'>, ignore everything except bold characters. It says int, which means our variable is of type integer.
           2. float
      • float type stands for floating type value. What is floating type value? It is a value with a decimal part like 1.2, 1024.2048, 0.24, etc.
      • Remember we had divided 1 by 2, and the answer was 0.5 which was indeed a float value.
      • Please try it and check its type with type function.
          3. str
      • str stands for the string datatype. A string is something which you have been reading this post.
      • Every character, word, and sentence is a type of string.
      • In Python, we declare string something like this:
        • >>> string_variable = "Python"
      • We've declared a string variable named string_variable with a value Python. Look carefully, the string Python is surrounded by quotes("). Remember, you've to start and close a string with a single (') or double quote.
      • Both starting and closing quotes should be same.
      • Strings are really special, you can add and multiply them in Python.
        • Let's declare two strings, 
          • string_one = "Infinity"
          • string_two = "Python"
        • Now, you have to create a string "Infinity Python". How to do it?
          • string_three = string_one + string_two
          • print(string_three)
        • Okay, well done, but now you want to have a string which says “Python Python Python”
          • You can do it in following way:
            • string_three = string_two + string_two + string_two
          • But think what if you have to print like 50 times, it would be really long Python command.
          • But don't worry, Python always takes care of you. It provides a better way. You can just multiply string and the number of times you want to copy the string.
          • Wait! Multiplying string and number, that is just wrong. No, it isn't in Python there are many things which are going to surprise you. Like this,



         4. complex

      • Python also supports complex datatype. The complex data type is a mathematical datatype, which has two parts, one real and one imaginary.
      • It looks something like this, 1 + 2j. Here, 1 is the real part and 2 is the imaginary part.
      • If you want to learn more about the complex number, read from here: https://en.wikipedia.org/wiki/Complex_number
      • You can perform mathematical operations like addition, subtraction, multiplication and division on complex datatype like
      • >>> a = 1 + 2j
        >>> b = 2 + 4j
        >>> a + b
        (3+6j)
        >>> a - b
        (-1-2j)
        >>> a * b
        (-6+8j)
        >>> a / b
        (0.5+0j)
    Now, four out of six datatypes have been discussed. Remaining two are bytes and bytearray which we'll discuss later on.

    Now, suppose you have a string which is of value "52" and you want to add 4 to make it "56" but we know we cannot add incompatible data types. So, what to do? Don't worry, Python is here for help. Python has a very simple type conversion facility which allows us to change to the datatype of variable.

    We know four datatypes, (int, float, str, complex), and we'll learn interconversion between them. Let's start with a problem, we have a string a_string = "5" and we want to add 20.5 to it and add an imaginary part 100 to it.


     >>> a_string = '5'
     >>> # converting string to integer
     >>> an_integer = int(a_string)    # converting string to integer
     >>> a_float = an_integer + 20.5    # automatic conversion to float
     >>> # can also be done by
     >>> a_float = an_integer + float("20.5")    # conversions conversions conversions
     >>>
     >>>
     >>> # creating a complex number
     >>> a_complex = complex(a_float, 100)
     >>> # this will equals to (25.5+100j)

    What is happening over here? This is called type conversion. Almost every language supports it but each may different syntax. For now, consider the highlighted commands just as BIF (built-in-functions) which are directly provided by the Python language.

    So, this was the introduction of some basic data types which Python provides. Next time, we'll meet some more datatypes provided by Python.

    Try It Yourself:
    Prepare a script for initializing a variable with a value "500", then add 2000 with it and store it in a variable named first_variable. Initialize two variables, one with value "25" and another with "00" and add them together and convert the result to int and store it in second_variable. Now, create final_variable of complex type, with real part equals to first_variable and imaginary part equals to second_variable.


    Happy Pythoning!!

    Comments